pygame sprite赢得了移动,当按键按下时退出游戏
因此,当按下钥匙时,我无法让我的精灵跳跃。我已经解决了一些错误,但是现在我有一个说明:文件“/users/josh/desktop/fresh/main.py”,第52行,in screen.blit(player,player_rect) TypeError:闪电的无效目的地位置
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('Shooter Mania')
text = pygame.font.Font(None, 50)
clock = pygame.time.Clock()
#
bg = pygame.image.load('background.png')
#
player = pygame.image.load('player.png').convert_alpha()
player_rect = player.get_rect(midbottom=(80,300))
playerX = 0
playerY = 0
#
score_surf = text.render('SCORE:', False, 'white')
score_rect = score_surf.get_rect(center=(400,50))
#
enemy = pygame.image.load('enemy.png').convert_alpha()
enemy_rect = enemy.get_rect(bottomright=(600,300))
#
#
#
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player_rect = player_rect.move_ip(0,5)
print(f'Jump')
screen.blit(bg, (0,0))
pygame.draw.rect(screen, 'Pink',score_rect)
screen.blit(score_surf,score_rect)
enemy_rect.x -= 4
if enemy_rect.right <= 0:
enemy_rect.left = 800
screen.blit(enemy,enemy_rect)
# if player_rect.y <= 400:
# player_rect.y = 300
# playerX +=1
# player_rect.y += playerX
screen.blit(player, player_rect)
pygame.display.update()
clock.tick(60)
So I can't get my sprite to jump when the key is pressed. I've fixed a few errors but now I have one that says: File "/Users/josh/Desktop/fresh/main.py", line 52, in
screen.blit(player, player_rect)
TypeError: invalid destination position for blit
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((800,400))
pygame.display.set_caption('Shooter Mania')
text = pygame.font.Font(None, 50)
clock = pygame.time.Clock()
#
bg = pygame.image.load('background.png')
#
player = pygame.image.load('player.png').convert_alpha()
player_rect = player.get_rect(midbottom=(80,300))
playerX = 0
playerY = 0
#
score_surf = text.render('SCORE:', False, 'white')
score_rect = score_surf.get_rect(center=(400,50))
#
enemy = pygame.image.load('enemy.png').convert_alpha()
enemy_rect = enemy.get_rect(bottomright=(600,300))
#
#
#
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player_rect = player_rect.move_ip(0,5)
print(f'Jump')
screen.blit(bg, (0,0))
pygame.draw.rect(screen, 'Pink',score_rect)
screen.blit(score_surf,score_rect)
enemy_rect.x -= 4
if enemy_rect.right <= 0:
enemy_rect.left = 800
screen.blit(enemy,enemy_rect)
# if player_rect.y <= 400:
# player_rect.y = 300
# playerX +=1
# player_rect.y += playerX
screen.blit(player, player_rect)
pygame.display.update()
clock.tick(60)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
移动矩形时,您会使用 更新到适当的rect(因此名称)并没有返回,即
none
。然后将其分配到player_Rect
变量,然后传递到screen.blit()
,失败。解决方案不是使用移动的原位版本:这将返回更新的rect并将其分配给
player_Rect
。另外,您可以不用分配使用现场版本:When you move your rect, you use
move_ip()
which updates the rect in place (hence the name) and returns nothing, i.e.None
. This then gets assigned to theplayer_rect
variable and passed toscreen.blit()
, which fails. The solution is not to use the in-place version of move:This will return the updated rect and assign it to
player_rect
. Alternatively, you could use the in-place version without assignment: