我写了这段代码,但它不起作用,我的意思是窗口:(没有响应。请帮忙!!Python_Army;( 问题是我的笔记本电脑还是我的笔记本电脑还是代码,如果您可以改进我的代码,请尽快回答我!!
这是代码:
import pygame
pygame.init()
display_width = 800
display_height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
background = pygame.image.load('C:/Users/H.S/Desktop/background.png')
player = pygame.image.load('C:/Users/H.S/Desktop/player.png')
file = pygame.image.load('C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg')
pygame.display.set_icon((file))
pygame.display.set_caption('a bit racey')
clock = pygame.time.Clock()
gameDisplay.blit(background, (0,0))
x = 300
y = 500
c = input()
if c == a:
x-=1
y = 500
pygame.display.flip()
pygame.display.update()
gameDisplay.blit(player, (x,y))
elif c == d:
x+=1
y = 500
pygame.display.update()
pygame.display.flip()
gameDisplay.blit(player, (x,y))
elif c == w:
y+=1
x = 300
pygame.display.flip()
pygame.display.update()
gameDisplay.blit(player, (x,y))
elif c == s:
y-=1
x = 300
pygame.display.flip()
pygame.display.update()
gameDisplay.blit(player, (x,y))
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
您必须在应用程序循环中移动和绘制播放器。当然,您必须更新应用程序循环中的显示。主应用程序循环必须:
- 按
pygame.event.pump()
或pygame.event.get()
处理事件。 - 根据输入事件和时间(分别为帧(更新对象的游戏状态和位置
- 清除整个显示器或绘制背景
- 绘制整个场景(
blit
所有对象( - 按
pygame.display.update()
或pygame.display.flip()
更新显示
使用pygame.key.get_pressed()
获取密钥的状态:
import pygame
pygame.init()
display_width, display_height = 800, 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
gameDisplay = pygame.display.set_mode((display_width,display_height))
background = pygame.image.load('C:/Users/H.S/Desktop/background.png')
player = pygame.image.load('C:/Users/H.S/Desktop/player.png')
file = pygame.image.load('C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg')
pygame.display.set_icon((file))
pygame.display.set_caption('a bit racey')
clock = pygame.time.Clock()
x, y = 300, 500
crashed = False
while not crashed:
for event in pygame.event.get():
if event.type == pygame.QUIT:
crashed = True
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
x -= 1
if keys[pygame.K_d]:
x += 1
if keys[pygame.K_w]:
y -= 1
if keys[pygame.K_s]:
y += 1
gameDisplay.blit(background, (0,0))
gameDisplay.blit(player, (x, y))
pygame.display.flip()