当代码运行时,与退出代码时相比,它会显示一个黑屏

2024-04-20 12:41:58 发布

您现在位置:Python中文网/ 问答频道 /正文

# no errors were shown in the consle
import pygame
pygame.init()
# display size
width = 1800
height =1000
# colors
black = (0, 0, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
aqua = (1, 255, 255)
# main display
gamedisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption('Race Game')  # the
clock = pygame.time.Clock()
# the car image
carImg = pygame.image.load('myCar.png')
# car
def car(x, y):
    gamedisplay.blit(carImg, (x, y))
x = (width * 0.45)
y = (height * 0.8)
# events
crashed1 = False
x_change = 0
while not crashed1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed1 = True
                                  # keybinds
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -5
            elif event.key == pygame.K_RIGHT:
                x_change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or pygame.K_RIGHT:
                x_change = 0
x += x_change
# update loop
print(event)
gamedisplay.fill(white)
car(x, y)
pygame.display.update()
clock.tick(120)
pygame.quit()

1条回答
网友
1楼 · 发布于 2024-04-20 12:41:58

尝试在这些命令之前放置4个空格或一个选项卡,并获取

import pygame
pygame.init()
# display size
width = 1800
height =1000
# colors
black = (0, 0, 0)
blue = (0, 0, 255)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)
aqua = (1, 255, 255)
# main display
gamedisplay = pygame.display.set_mode((width, height))
pygame.display.set_caption('Race Game')  # the
clock = pygame.time.Clock()
# the car image
carImg = pygame.image.load('myCar.png')
# car
def car(x, y):
    gamedisplay.blit(carImg, (x, y))
x = (width * 0.45)
y = (height * 0.8)
# events
crashed1 = False
x_change = 0
while not crashed1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed1 = True
                                  # keybinds
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -5
            elif event.key == pygame.K_RIGHT:
                x_change = 5
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or pygame.K_RIGHT:
                x_change = 0
    x += x_change
    # update loop
    print(event)
    gamedisplay.fill(white) 
    car(x, y)
    pygame.display.update()
    clock.tick(120)
pygame.quit()

相关问题 更多 >