角色跳跃时向另一边看,弹丸也从另一边出来
Character Looks the Other way when jumping, and projectiles also come out the other way
如果有人愿意帮助我解决两个简单的问题(我很恼火地找不到解决方案),我将永远感激不已。我已经从互联网上尝试了几种解决方案,但它们似乎给我提供了更多错误。
基本上,我有一个 2d 平台游戏,可以让你左右移动、跳跃和射击。然而,当你向右移动然后跳跃时,它会面向相反的方向[向左行走和跳跃非常好]。我假设它与“DeprecationWarning:需要一个整数(类型为 float)。不推荐使用 int 隐式转换为整数”错误有关,但我可能是错的。一旦你向右移动并跳跃并且它朝向你的相反方向,子弹也会朝 'right' 方向移动,尽管精灵看起来朝向 window 的左侧。如果任何愿意帮助我的人在解决问题时遇到困难,请与我联系,我会尽我所能提供帮助,同时我也会尝试自己解决问题。非常感谢。安东尼
拜托,如果有人可以查看我的代码并指导我如何解决此问题,我将再次感激不尽。
完整代码:https://pastebin.pl/view/0bb0b42c
疑似错误部分代码:
def draw(self,screen):
if self.walkCount + 1 >= 21:
self.walkCount = 0
if not(self.standing):
if self.left:
screen.blit(self.walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
screen.blit(self.walkRight[self.walkCount//3],(self.x,self.y))
self.walkCount += 1
else:
if self.right:
screen.blit(self.walkRight[0], (self.x, self.y))
else:
screen.blit(self.walkLeft[0], (self.x, self.y))
def move(self,x,y): #creating the move object so we can call it later and move our sprite
if x != 0:
self.move_single_axis(x,0)
if y != 0:
self.move_single_axis(0,y)
def move_single_axis(self, x, y):
self.rect.x += x
def update(self):
if self.y > H - height:
self.y += 10
&
for bullet in bullets:
if bullet.x < 800 and bullet.x > 0: #Making sure the bullet isn't off the screen.
bullet.x += bullet.vel #move our bullet according to velocity
else:
bullets.pop(bullets.index(bullet)) #Deleting our bullet by popping (removing) from list.
keys = pygame.key.get_pressed() # creating a variable to check for key presses
if keys [pygame.K_UP]:
if player.left:
facing = -1 #determining direction of bullet (left = -1)
else:
facing = 1 #determining direction of bullet (right = +1)
if len(bullets) < 6: #no. of bullets on screen at once.
bullets.append(Bullet(round(player.x + player.width // 2), round(player.y + player.height // 2), 6, (0,0,0), facing))
if keys[pygame.K_LEFT] and player.x > player.vel:
player.x -= player.vel
player.right = False
player.left = True
player.standing = False
elif keys[pygame.K_RIGHT]:
player.x += player.vel
player.right = True
player.left = False
player.standing = False
if player.x >= W-width:
player.x = W-width
else:
player.standing = True
player.walkCount = 0
if not (player.isJump):
if keys[pygame.K_SPACE]:
player.isJump = True
player.right = False
player.left = False
player.walkCount = 0
else:
if player.jumpCount >= -11.5:
neg = 1
if player.jumpCount < 0:
neg = -1
player.y -= (player.jumpCount ** 2) * 0.5 * neg
player.jumpCount -= 1
else:
player.isJump = False
player.jumpCount = 11.5
问题是在按下 SPACE 时设置 player.right = False
和 player.left = False
引起的。由于方向信息存储在player.right
和player.left
中,因此信息将丢失。跳跃时保持player.right
和player.left
的状态:
if not (player.isJump):
if keys[pygame.K_SPACE]:
player.isJump = True
# player.right = False <--- DELETE
# player.left = False <--- DELETE
player.walkCount = 0
如果有人愿意帮助我解决两个简单的问题(我很恼火地找不到解决方案),我将永远感激不已。我已经从互联网上尝试了几种解决方案,但它们似乎给我提供了更多错误。
基本上,我有一个 2d 平台游戏,可以让你左右移动、跳跃和射击。然而,当你向右移动然后跳跃时,它会面向相反的方向[向左行走和跳跃非常好]。我假设它与“DeprecationWarning:需要一个整数(类型为 float)。不推荐使用 int 隐式转换为整数”错误有关,但我可能是错的。一旦你向右移动并跳跃并且它朝向你的相反方向,子弹也会朝 'right' 方向移动,尽管精灵看起来朝向 window 的左侧。如果任何愿意帮助我的人在解决问题时遇到困难,请与我联系,我会尽我所能提供帮助,同时我也会尝试自己解决问题。非常感谢。安东尼
拜托,如果有人可以查看我的代码并指导我如何解决此问题,我将再次感激不尽。
完整代码:https://pastebin.pl/view/0bb0b42c
疑似错误部分代码:
def draw(self,screen):
if self.walkCount + 1 >= 21:
self.walkCount = 0
if not(self.standing):
if self.left:
screen.blit(self.walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
screen.blit(self.walkRight[self.walkCount//3],(self.x,self.y))
self.walkCount += 1
else:
if self.right:
screen.blit(self.walkRight[0], (self.x, self.y))
else:
screen.blit(self.walkLeft[0], (self.x, self.y))
def move(self,x,y): #creating the move object so we can call it later and move our sprite
if x != 0:
self.move_single_axis(x,0)
if y != 0:
self.move_single_axis(0,y)
def move_single_axis(self, x, y):
self.rect.x += x
def update(self):
if self.y > H - height:
self.y += 10
&
for bullet in bullets:
if bullet.x < 800 and bullet.x > 0: #Making sure the bullet isn't off the screen.
bullet.x += bullet.vel #move our bullet according to velocity
else:
bullets.pop(bullets.index(bullet)) #Deleting our bullet by popping (removing) from list.
keys = pygame.key.get_pressed() # creating a variable to check for key presses
if keys [pygame.K_UP]:
if player.left:
facing = -1 #determining direction of bullet (left = -1)
else:
facing = 1 #determining direction of bullet (right = +1)
if len(bullets) < 6: #no. of bullets on screen at once.
bullets.append(Bullet(round(player.x + player.width // 2), round(player.y + player.height // 2), 6, (0,0,0), facing))
if keys[pygame.K_LEFT] and player.x > player.vel:
player.x -= player.vel
player.right = False
player.left = True
player.standing = False
elif keys[pygame.K_RIGHT]:
player.x += player.vel
player.right = True
player.left = False
player.standing = False
if player.x >= W-width:
player.x = W-width
else:
player.standing = True
player.walkCount = 0
if not (player.isJump):
if keys[pygame.K_SPACE]:
player.isJump = True
player.right = False
player.left = False
player.walkCount = 0
else:
if player.jumpCount >= -11.5:
neg = 1
if player.jumpCount < 0:
neg = -1
player.y -= (player.jumpCount ** 2) * 0.5 * neg
player.jumpCount -= 1
else:
player.isJump = False
player.jumpCount = 11.5
问题是在按下 SPACE 时设置 player.right = False
和 player.left = False
引起的。由于方向信息存储在player.right
和player.left
中,因此信息将丢失。跳跃时保持player.right
和player.left
的状态:
if not (player.isJump):
if keys[pygame.K_SPACE]:
player.isJump = True
# player.right = False <--- DELETE
# player.left = False <--- DELETE
player.walkCount = 0