我的python角色一次只移动1个像素
发布时间:2020-12-20 13:38:46 所属栏目:Python 来源:网络整理
导读:我正在制作一个小小的跑酷迷你游戏,角色从平台跳到平台,但我似乎无法让角色正常移动.我做了另一个游戏,它工作正常,但这个没有.当我按住左/右箭头键时,它一次只移动1个像素.这是V Parkour_MoveLeft=Parkour_MoveRight=Parkour_Jump='no' Parkour_Speed=1 Park
我正在制作一个小小的跑酷迷你游戏,角色从平台跳到平台,但我似乎无法让角色正常移动.我做了另一个游戏,它工作正常,但这个没有.当我按住左/右箭头键时,它一次只移动1个像素.这是V
Parkour_MoveLeft=Parkour_MoveRight=Parkour_Jump='no' Parkour_Speed=1 Parkour_X=0 Parkour_Y=0 Parkour_Rows=0 Parkour_Col=0 现在这里是我遇到问题的游戏部分的代码: if location=='Parkour': Window.fill(Black) WindowW = 700 WindowH = 700 Window=pygame.display.set_mode((WindowW,WindowH),32) pygame.draw.rect(Window,Blue,Parkour_Character) num=0 for point in Parkour_Grids: mat=Parkour_Lvl_1[num] num+=1 if mat=='a': point['collide']='no' if mat=='p': pygame.draw.rect(Window,Green,point['rect']) point['collide']='yes' for point in Parkour_Grids: if point['collide']=='yes': if Parkour_Character.colliderect(point['left']): Parkour_MoveRight='no' if Parkour_Character.colliderect(point['right']): Parkour_MoveLeft='no' if Parkour_Character.colliderect(point['bottom']): Parkour_MoveUp='no' if Parkour_Character.colliderect(point['top']): Parkour_MoveDown='no' 跑酷运动 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_RIGHT: Parkour_MoveRight='yes' if event.key == K_LEFT: Parkour_MoveLeft='yes' if event.type == KEYUP: if event.key == K_RIGHT: Parkour_MoveRight='no' if event.key == K_LEFT: Parkour_MoveLeft='no' if Parkour_MoveLeft=='yes': Parkour_Character.right-=Parkour_Speed if Parkour_MoveRight=='yes': Parkour_Character.right+=Parkour_Speed 等级地图 Parkour_Lvl_1=['a','a','p','a'] while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() Parkour_Grids.append({'rect':pygame.Rect(Parkour_X,Parkour_Y,80,30),'right':1,'left':1,'top':1,'bottom':1,'type':'air','collide':'yes'}) Parkour_X+=80 Parkour_Col+=1 if Parkour_Col==40: Parkour_Col=0 Parkour_X=0 Parkour_Y+=70 Parkour_Rows+=1 if Parkour_Rows==10: break for point in Parkour_Grids: point['right']=pygame.Rect(point['rect'].left+70,point['rect'].top,6,70) point['left']=pygame.Rect(point['rect'].right-76,70) point['top']=pygame.Rect(point['rect'].left+6,point['rect'].top-15,58,6) point['bottom']=pygame.Rect(point['rect'].left+6,point['rect'].bottom+6,6) 任何人对我能做什么有任何帮助?这是我在另一个游戏(使用不同的变量)时完全相同的代码,但这个似乎不起作用. 解决方法
你需要提高Parkour_Speed.它被设置为1,因为它看起来不像你曾经乘以它或实现了两次运动,这就是它将移动多少像素.提高此数字将使其一次移动更多像素.但是,虽然没有明确说明这是否是您的问题,但按下按钮时也可能无法连续移动.如果事件查找程序没有被持续运行,这肯定会发生,但是因为检查事件是同时实现的,所以可能不是这种情况(使用更多的代码会更容易知道).您必须反复实施的另一件事就是在画面上连续移动精灵,将Parkour_X和Y与屏幕同步.我在代码中看不到你在哪里这样做,但我认为使用move_ip函数来解决你的问题应该是最容易的.如果屏幕正在更新,对代码的这种修改应该可以解决您的问题:
for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() if event.type == KEYDOWN: if event.key == K_RIGHT: Parkour_MoveRight='yes' if event.key == K_LEFT: Parkour_MoveLeft='yes' if event.type == KEYUP: if event.key == K_RIGHT: Parkour_MoveRight='no' if event.key == K_LEFT: Parkour_MoveLeft='no' if Parkour_MoveLeft=='yes': Parkour_Character.move_ip(-Parkour_Speed,0) if Parkour_MoveRight=='yes': Parkour_Character.move_ip(Parkour_Speed,0) 如果这也无法解决您的问题,那么其他一切似乎都很好,因此包含更多代码可能会很有用.我知道你已经有了很多代码,但是像这样的图形程序中的简单功能往往是非常相互关联的. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |