下面是我的代码,这是我的第一个 pygame 项目,任何帮助将不胜感激!
我认为这与
def 玩家(玩家,玩家):
pygame.display.update()
代码块。尽管当我使用它时,有时它甚至不会显示我的背景对象,而只显示屏幕填充黑色。
import pygame
#initializing the game
pygame.init()
FPS = 60
black = (0,0,0)
white = (255,255,255)
#creating the screen and setting the size
gameDisplay = pygame.display.set_mode((800,600))
#setting the title of the window
pygame.display.set_caption('Space Crashers')
#going into our files and loading the image for the background
background = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\newBackground.jpg')
#player image and model
playerImg = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\blueShip.png')
#these cords are for the player ship to be in the middle of the screen
playerX = 370
playerY = 480
#creating player function
def player(x,y):
gameDisplay.blit(playerImg, (x,y))
# window creation
clock = pygame.time.Clock()
running = True
while running:
#setting the background to black
gameDisplay.fill(black)
#then changing the background to the image we loaded
gameDisplay.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
pygame.display.update()
# if the keystroke is pressed, the player will move
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX -= .01
if event.key == pygame.K_RIGHT:
playerX += .01
if event.key == pygame.K_UP:
playerY -= .01
if event.key == pygame.K_DOWN:
playerY += .01
def player(playerX,playerY):
pygame.display.update()
clock.tick(FPS)
pygame.quit()
quit()
See 按住键时如何使精灵移动 https://stackoverflow.com/questions/9961563/how-can-i-make-a-sprite-move-when-key-is-held-down。还了解缩进 https://docs.python.org/3/reference/lexical_analysis.html。您的代码必须具有正确的缩进才能正常工作。
典型的 PyGame 应用程序循环必须:
- 限制每秒帧数以限制 CPU 使用率pygame.time.Clock.tick https://www.pygame.org/docs/ref/time.html#pygame.time.Clock
- 通过调用以下任一方法处理事件pygame.event.pump() https://www.pygame.org/docs/ref/event.html#pygame.event.pump or pygame.event.get() https://www.pygame.org/docs/ref/event.html#pygame.event.get.
- 根据输入事件和时间(分别为帧)更新游戏状态和对象位置
- 清除整个显示或绘制背景
- 绘制整个场景(
blit
所有对象)
- 通过调用更新显示pygame.display.update() https://www.pygame.org/docs/ref/display.html#pygame.display.update or pygame.display.flip() https://www.pygame.org/docs/ref/display.html#pygame.display.flip
import pygame
#initializing the game
pygame.init()
FPS = 60
black = (0,0,0)
white = (255,255,255)
#creating the screen and setting the size
gameDisplay = pygame.display.set_mode((800,600))
#setting the title of the window
pygame.display.set_caption('Space Crashers')
#going into our files and loading the image for the background
background = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\newBackground.jpg')
#player image and model
playerImg = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\blueShip.png')
#these cords are for the player ship to be in the middle of the screen
playerX = 370
playerY = 480
# creating player function
def player(x,y):
gameDisplay.blit(playerImg, (x,y))
# application loop
clock = pygame.time.Clock()
running = True
while running:
# handle the events
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# update the game states and positions of objects
keys = pygame.key.get_pressed()
playerX += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * 3
playerY += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 3
# clear the entire display or draw the background
gameDisplay.fill(black)
gameDisplay.blit(background,(0,0))
# draw the entire scene (blit all the objects)
player(playerX, playerY)
# update the display
pygame.display.update()
# limit the frames per second to limit CPU usage with
clock.tick(FPS)
pygame.quit()
quit()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)