Pygame (A bit Racey) 游戏错误 [英] Pygame (A bit racey) game bug

查看:57
本文介绍了Pygame (A bit Racey) 游戏错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您第一次启动游戏时,有游戏开始菜单/介绍,它有 2 个按钮(开始游戏)(退出)

in the game when you first start it there's the game start menu/intro, it has 2 buttons (Start Game) (Quit)

现在,当您开始游戏并实际玩游戏时,当您按 P(暂停)时,会出现 2 个按钮(继续)(主菜单),如果您单击继续,它会正常完成游戏.但是,当您单击主菜单时,它会关闭游戏而不是返回到介绍,当您与您拥有的汽车(再试一次)和(主菜单)发生碰撞时也会出现同样的问题,并且如果您单击主菜单,它会关闭游戏

Now when you start the game and you're actually playing, when you press P (Paused) there're 2 buttons (Continue) (Main Menu) if you click continue it completes the game normally. However when you click main menu it closes the game instead of returning to the intro, same problem is present when you crash with the car you have (Try Again) and (Main Menu) and also if you click Main Menu it closes the game

关于可能是什么问题的任何想法?(忽略评论)

Any ideas on what might be the problem ?(ignore the comments)

import pygame
import time
import random

pygame.init()

crash_sound = pygame.mixer.Sound("C:\Users\itzrb_000\Documents\Untitled.wav")
pygame.mixer.music.load("C:\Users\itzrb_000\Downloads\Cool_Ride.mp3")

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)
red = (200,0,0)
green = (0,200,0)
blue = (0,0,255)

bright_green = (0,255,0)
bright_red = (255,0,0)

car_width = 50

gameDisplay = pygame.display.set_mode((display_width,display_height))#game screen size
pygame.display.set_caption('What A Ride')
clock = pygame.time.Clock()

carImg = pygame.image.load('C:\Users\itzrb_000\Downloads\Car_Green_Front.png')
gameBackground = pygame.image.load('C:\WhatARide_Background.png')
icon = pygame.image.load('C:\Users\itzrb_000\Downloads\Car_Green_Front - Copy.png')

pygame.display.set_icon(icon)

pause = False

car1 = pygame.image.load('C:\Users\itzrb_000\Downloads\download (3).png')
car2 = pygame.image.load('C:\Users\itzrb_000\Downloads\download (2).png')
car3 = pygame.image.load('C:\Users\itzrb_000\Downloads\images.png')
rock = pygame.image.load('C:\Users\itzrb_000\Downloads\Rock.png')

def background():
    gameDisplay.blit(gameBackground,(0,0))

'''def cars(ystart):
    objects = [car1, car2, car3, rock]
    num_of_objects = random.randint(1,4)
    for x in range(num_of_objects):
        y = random.choice(objects)
        objectblit = random.randrange(130, 625) - (y.get_width())
        gameDisplay.blit(y,(random.randrange(130, 625)))'''

def score(count):
    font = pygame.font.SysFont(None,25)
    text = font.render("Score: "+str(count),True, blue)
    gameDisplay.blit(text,(0,0))

def things(thingx, thingy, thingw, thingh, color):
    pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh])

def car(x,y):
    gameDisplay.blit(carImg,(x,y))

def text_objects(text,font):
    textSurface = font.render(text, True, white)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width*0.5),(display_height*0.5))
    gameDisplay.blit(TextSurf, TextRect)#blit display object

    pygame.display.update()

    time.sleep(2)

    game_loop()

def crash():
    pygame.mixer.music.stop()
    pygame.mixer.Sound.play(crash_sound)

    largeText = pygame.font.Font("freesansbold.ttf", 115)
    TextSurf, TextRect = text_objects("You Crashed", largeText)
    TextRect.center = ((display_width * 0.5), (display_height * 0.5))
    gameDisplay.blit(TextSurf, TextRect)  # blit display object
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        button("Try Again!", 300, 400, 200, 50, green, bright_green, game_loop)
        button("Main Menu!", 300, 470, 200, 50, red, bright_red, game_intro)

        pygame.display.update()
        clock.tick(20)

def button(msg,x,y,w,h,ic,ac,action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if x+w > mouse[0] > x and y+h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, ac,(x,y,w,h))

        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(gameDisplay, ic,(x,y,w,h))

    smallText = pygame.font.SysFont("freesansbold.ttf",20)
    textSurf, textRect = text_objects(msg, smallText)
    textRect.center = ( (x+(w/2)), (y+(h/2)) )
    gameDisplay.blit(textSurf, textRect)

def quitgame():
    pygame.quit()
    quit()

def unpause():
    global pause
    pause = False
    pygame.mixer.music.unpause()
def paused():
    global pause
    pause = True
    pygame.mixer.music.pause()
    largeText = pygame.font.Font("freesansbold.ttf", 115)
    TextSurf, TextRect = text_objects("Paused", largeText)
    TextRect.center = ((display_width * 0.5), (display_height * 0.5))
    gameDisplay.blit(TextSurf, TextRect)  # blit display object
    while pause == True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        button("Continue!", 300, 400, 200, 50, green, bright_green,unpause)
        button("Main Menu", 300, 470, 200, 50, red, bright_red,game_intro)

        pygame.display.update()
        clock.tick(20)

def game_intro():
    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        gameDisplay.fill(blue)
        largeText = pygame.font.SysFont("comicsansms", 115)
        TextSurf, TextRect = text_objects("What A Ride", largeText)
        TextRect.center = ((display_width / 2), (display_height / 2))
        gameDisplay.blit(TextSurf, TextRect)

        button("Start Game", 300, 400, 200, 50, green, bright_green,game_loop)
        button("Quit", 300, 470, 200, 50, red, bright_red,quitgame)

        pygame.display.update()
        clock.tick(20)


def game_loop():
    global pause
    pygame.mixer.music.play(-1)
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0

    thing_startx = random.randrange(130,625)
    thing_starty = -600
    thing_speed = 5
    thing_width = 100
    thing_height = 100

    dodged = 0

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            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.key == pygame.K_p:
                    pause = True
                    paused()

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

            print event
        x += x_change

        background()
        things(thing_startx, thing_starty, thing_width, thing_height, black)
        thing_starty += thing_speed
        car(x,y)
        score(dodged)

        if x > 625 - car_width or x < 130:
            crash()

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(130,625)
            dodged += 1
            thing_speed += 1
            thing_width += (dodged*1.2)
        if y < thing_starty+thing_height:
            if x > thing_startx and x < thing_startx + thing_width or x + car_width > thing_startx and x + car_width < thing_startx+thing_width:
                crash()
        pygame.display.update()
        clock.tick(51) #fps
game_intro()
game_loop()
pygame.quit()
quit()

推荐答案

因为在触发按钮之前,您不会等待释放鼠标按钮.

Because you're not waiting for the mouse button to be released before you trigger your buttons.

  • pause() 启动时,它会弹出两个按钮.
  • 用户将鼠标移动到主菜单.
  • 用户点击鼠标.
  • 只要按下鼠标按钮,就会调用 game_intro(),这会在同一位置放置一个 Quit 按钮.
  • 鼠标按钮仍然按下,所以游戏退出.
  • When pause() starts, it brings up two buttons.
  • User moves mouse to Main Menu.
  • User clicks mouse.
  • As soon as the mouse button is depressed, game_intro() is called, which puts a Quit button in the same place.
  • The mouse button is still depressed, so the game quits.

这篇关于Pygame (A bit Racey) 游戏错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆