为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

沧海一幻觉 2023-05-16 14:40:02
我遇到的问题是每次我运行我的游戏时,开始屏幕都无法正常工作,例如,所有点击 GO 按钮,它不会让我开始我的游戏,但一切似乎都是正确的,如果任何人都可以帮助它,将不胜感激import pygame, randomfrom time import sleeppygame.init()# music/soundsCarSound = 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 useGREEN = (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 = 400SCREENHEIGHT = 500size = (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()# playerplayerIMG = pygame.image.load("image/red_racecar.png")playerX = 280playerY = 450playerCar_position = 0# player2playerIMG_two = pygame.image.load("image/greencar.png")playerX_two = 150playerY_two = 450playerCar_position_two = 0# player3playerIMG_three = pygame.image.load("image/Orangecar.png")playerX_three = 60playerY_three = 450playerCar_position_three = 0这是我的按钮,退出按钮工作正常它只是 GO 按钮,按下时不会让我开始我的游戏我唯一的猜测可能是除非我把东西放在错误的地方?
查看完整描述

2 回答

?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

在游戏循环中,您不会调用屏幕更新。您还需要在循环中渲染汽车。


# Main game loop

def 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()



查看完整回答
反对 回复 2023-05-16
?
慕丝7291255

TA贡献1859条经验 获得超6个赞

尝试:

if action == "Play":
    game_loop()
        return

我认为您的代码缩进有问题。你pygame.quit()在全球范围内。所以,你运行pygame.init(),然后定义一些函数,然后pygame.quit()

注释掉该pygame.quit()行,看看会发生什么。


查看完整回答
反对 回复 2023-05-16
  • 2 回答
  • 0 关注
  • 97 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信