【发布时间】:2020-12-01 16:58:52
【问题描述】:
如果有人愿意帮助我解决两个简单的问题(我很烦人地找不到解决方案),我将永远感激不尽。我已经尝试了几种来自互联网的解决方案,但它们似乎给我提供了更多错误。
基本上,我有一个 2d 平台游戏,可以让你左右移动、跳跃和射击。但是,当您向右移动然后跳跃时,它会以相反的方向面向您[向左走和跳跃都很好]。我认为它与“DeprecationWarning:需要一个整数(类型为浮点数)。不推荐使用 int 隐式转换为整数”错误有关,但我可能错了。一旦你向右移动并跳跃并且它以相反的方向面对你,子弹也会朝“右”方向移动,尽管精灵看起来面向窗口的左侧。如果任何可以帮助我的人在解决问题时遇到困难,请与我联系,我会尽我所能提供帮助,同时我也会自己解决问题。非常感谢提前。安东尼
如果有人可以查看我的代码并指导我如何解决这个问题,我将再次感激不尽。
完整代码: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
【问题讨论】:
标签: python pygame pycharm projectile