import pygame
import sys
def run_game():
pygame.init()
screen=pygame.display.set_mode((1200,600))
pygame.display.set_caption("game over")
image=pygame.image.load('selfimages/airplane.bmp')
rect=image.get_rect()
screen_rect=screen.get_rect()
rect.centerx=screen_rect.centerx
rect.centery=screen_rect.centery
xcenter=float(rect.centerx)
ycenter=float(rect.centery)
moving_right=False
moving_left=False
moving_down=False
moving_up=False
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_RIGHT:
moving_right=True
elif event.key==pygame.K_LEFT:
moving_left=True
elif event.key==pygame.K_UP:
moving_up=True
elif event.key==pygame.K_DOWN:
moving_dowm=True
elif event.type==pygame.KEYUP:
if event.key==pygame.K_RIGHT:
moving_right=False
elif event.key==pygame.K_LEFT:
moving_left=False
elif event.key==pygame.K_UP:
moving_up=False
elif event.key==pygame.K_DOWN:
moving_dowm=False
if moving_right and rect.right<screen_rect.right:
xcenter+=1.5
if moving_left and rect.left>0:
xcenter-=1.5
if moving_up and rect.top<screen_rect.top:
ycenter+=1.5
if moving_down and rect.bottom>0:
ycenter-=1.5
rect.centerx=xcenter
rect.centery=ycenter
screen.fill((255,255,255))
screen.blit(image,rect)
pygame.display.flip()
run_game()
麻烦帮我看看问题出在哪,谢谢大家
import pygame
import sys
def run_game():
pygame.init()
screen=pygame.display.set_mode((1000,700))
pygame.display.set_caption("game over")
image=pygame.image.load('images/ship1.bmp')
rect=image.get_rect()
screen_rect=screen.get_rect()
rect.centerx=screen_rect.centerx
rect.centery=screen_rect.centery
xcenter=float(rect.centerx)
ycenter=float(rect.centery)
moving_right=False
moving_left=False
moving_down=False
moving_up=False
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_RIGHT:
moving_right=True
elif event.key==pygame.K_LEFT:
moving_left=True
elif event.key==pygame.K_UP:
moving_up=True
elif event.key==pygame.K_DOWN:
moving_down=True
elif event.type==pygame.KEYUP:
if event.key==pygame.K_RIGHT:
moving_right=False
elif event.key==pygame.K_LEFT:
moving_left=False
elif event.key==pygame.K_UP:
moving_up=False
elif event.key==pygame.K_DOWN:
moving_down=False
if moving_right and rect.right<screen_rect.right:
xcenter+=0.5
if moving_left and rect.left>0:
xcenter-=0.5
if moving_up and rect.top>0:
ycenter-=0.5
if moving_down and rect.bottom>screen_rect.top:
ycenter+=0.5
rect.centerx=xcenter
rect.centery=ycenter
screen.fill((255,255,255))
screen.blit(image,rect)
pygame.display.flip()
run_game()
moving_dowm=True 你写错字母了
虽然没能帮楼主解决你的问题,但是看到你的代码我就明白为什么我的不能同时上下移动了,谢谢!
你的y轴边框位置写反了,在Pygame,原点(0, 0)位于屏幕左上角,向右下方移动时,坐标值将增大
if moving_down and rect.bottom<screen_rect.bottom:
ycenter+=1.5
if moving_up and rect.top>0:
ycenter-=1.5