开始屏幕不工作如何解决问题?

我遇到的问题是每次我运行我的游戏时,开始屏幕都无法正常工作,例如,所有点击 GO 按钮,它不会让我开始我的游戏,但一切似乎都是正确的,如果任何人都可以帮助它,将不胜感激


import pygame, random

from time import sleep


pygame.init()

# music/sounds

CarSound = pygame.mixer.Sound("image/CAR+Peels+Out.wav")

CarSound_two = pygame.mixer.Sound("image/racing01.wav")

CarSound_three = pygame.mixer.Sound("image/RACECAR.wav")

CarSound_four = pygame.mixer.Sound("image/formula+1.wav")

Crowds = pygame.mixer.Sound("image/cheer_8k.wav")

Crowds_two = pygame.mixer.Sound("image/applause7.wav")

Crowds_three = pygame.mixer.Sound("image/crowdapplause1.wav")

music = pygame.mixer.music.load("image/Led Zeppelin - Rock And Roll (Alternate Mix) (Official Music Video).mp3")

pygame.mixer.music.play(-1)

bg = pygame.image.load('image/Crowds.png')


clock = pygame.time.Clock()


# Setting up our colors that we are going to use

GREEN = (20, 255, 140)

GREY = (210, 210, 210)

WHITE = (255, 255, 255)

RED = (255, 0, 0)

PURPLE = (255, 0, 255)

BLACKWHITE = (96, 96, 96)

BLACK = (105, 105, 105)

RGREEN = (0, 66, 37)

LIGHT_RED = (200, 0, 0)

BRIGHT_GREEN = (0, 255, 0)

DARK_BLUE = (0, 0, 139)

BLUE = (0, 0, 255)

NAVY = (0, 0 , 128)

DARK_OLIVE_GREEN = (85, 107, 47)

YELLOW_AND_GREEN = (154, 205, 50)



SCREENWIDTH = 400

SCREENHEIGHT = 500


size = (SCREENWIDTH, SCREENHEIGHT)

screen = pygame.display.set_mode(size)

pygame.display.set_caption("Car Racing")

Icon = pygame.image.load("image/redca_iconr.png")

pygame.display.set_icon((Icon))

# This will be a list that will contain all the sprites we intend to use in our game.

# all_sprites_list = pygame.sprite.Group()


# player

playerIMG = pygame.image.load("image/red_racecar.png")

playerX = 280

playerY = 450

playerCar_position = 0


# player2

playerIMG_two = pygame.image.load("image/greencar.png")

playerX_two = 150

playerY_two = 450

playerCar_position_two = 0


# player3

playerIMG_three = pygame.image.load("image/Orangecar.png")

playerX_three = 60

playerY_three = 450

playerCar_position_three = 0



这是我的按钮,退出按钮工作正常它只是 GO 按钮,按下时不会让我开始我的游戏我唯一的猜测可能是除非我把东西放在错误的地方?


沧海一幻觉
浏览 98回答 2
2回答

人到中年有点甜

在游戏循环中,您不会调用屏幕更新。您还需要在循环中渲染汽车。# Main game loopdef game_loop():    print(".......GAME LOOP........")    global playerCar_position,playerCar_position_two,playerCar_position_three,playerCar_position_four    global playerY,playerY_two,playerY_three,playerY_four,playerX,playerX_two,playerX_three,playerX_four    finish_line_rect = pygame.Rect(50, 70, 235, 32)    finish_text = ""    players_finished = 0    time_to_blit = 0    run = True    while run:        # Drawing on Screen        screen.fill(BLACK)        # Draw The Road        pygame.draw.rect(screen, GREY, [40, 0, 300, 500])        # Draw Line painting on the road        pygame.draw.line(screen, WHITE, [185, 0], [185, 500], 5)        # Finish line        pygame.draw.rect(screen, BLACKWHITE, [50, 50, 280, 40])        pygame.draw.line(screen, WHITE, [50, 70], [330, 70], 5)        font = pygame.font.SysFont("Impact", 20)        text = font.render("Finish line!", 2, (150, 50, 25))        screen.blit(text, (185 - (text.get_width() / 2), 45))         #.............                for event in pygame.event.get():            if event.type == pygame.QUIT:                run = False                # Number of frames per secong e.g. 60                clock.tick(60)            keys = pygame.key.get_pressed()            if keys[pygame.K_1]:#                CarSound.play()                playerCar_position = -0.5            if keys[pygame.K_q]:                playerCar_position = 0.5            if keys[pygame.K_2]:#                CarSound_two.play()                playerCar_position_two = -0.5            if keys[pygame.K_w]:                playerCar_position_two = 0.5            if keys[pygame.K_3]:#                CarSound_three.play()                playerCar_position_three = -0.5            if keys[pygame.K_e]:                playerCar_position_three = 0.5            if keys[pygame.K_4]:#                CarSound_four.play()                playerCar_position_four = -0.5            if keys[pygame.K_r]:                playerCar_position_four = 0.5        # our functions        playerY += playerCar_position        playerY_two += playerCar_position_two        playerY_three += playerCar_position_three        playerY_four += playerCar_position_four        player(playerX, playerY)        player_two(playerX_two, playerY_two)        player_three(playerX_three, playerY_three)        player_four(playerX_four, playerY_four)                # Did anyone cross the line?        if (finish_line_rect.collidepoint(playerX, playerY)):            if finish_text[:8] != "Player 1":  # so it doesnt do this every frame the car is intersecting                finish_text = "Player 1 is " + placings[players_finished]                players_finished += 1                print("Player (one) has crossed into finish line!")#                Crowds.play()        elif (finish_line_rect.collidepoint(playerX_two, playerY_two)):            if finish_text[:8] != "Player 2":                print("Player one has crossed into finish line first other car lost!")                finish_text = "Player 2 is " + placings[players_finished]                players_finished += 1#                    Crowds_three.play()        elif (finish_line_rect.collidepoint(playerX_three, playerY_three)):            if finish_text[:8] != "Player 3":                print("Player two has crossed into finish line first other car lost!")                finish_text = "Player 3 is " + placings[players_finished]                players_finished += 1        elif (finish_line_rect.collidepoint(playerX_four, playerY_four)):            if finish_text[:8] != "Player 4":                print("Player two has crossed into finish line first other car lost!")                finish_text = "Player 4 is " + placings[players_finished]                players_finished += 1#                    Crowds_two.play()        if (players_finished and finish_text):            print("ft:", finish_text)                        font = pygame.font.SysFont("Impact", 15)            textrect = font.render(finish_text, False, (0, 66, 37))            print('x', (SCREENWIDTH - text.get_width()) / 2)            screen.blit(textrect, (0,0))            screen.blit(textrect, ((SCREENWIDTH - textrect.get_width()) // 2, -5))        if (finish_text):            font = pygame.font.SysFont("Impact", 20)            text = font.render('Game Over!!!', 5, (0, 66, 37))            screen.blit(text, (250 - (text.get_width() / 5), -2))        if players_finished == 4:            time_to_blit = pygame.time.get_ticks() + 5000        if time_to_blit:            screen.blit(text_two, (130, 460))            if pygame.time.get_ticks() >= time_to_blit:                time_to_blit = 0                        pygame.display.update()        clock.tick(60)game_intro()score(players_finished)  pygame.quit()

慕丝7291255

尝试:if action == "Play":     game_loop()         return我认为您的代码缩进有问题。你pygame.quit()在全球范围内。所以,你运行pygame.init(),然后定义一些函数,然后pygame.quit()注释掉该pygame.quit()行,看看会发生什么。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python