Python Pygame(有点疯狂)游戏bug

Python Pygame(有点疯狂)游戏bug,python,pygame,Python,Pygame,在游戏中,当你第一次启动它时,有游戏开始菜单/介绍,它有2个按钮(开始游戏)(退出) 现在,当您开始游戏并实际玩游戏时,当您按p(暂停)时,会有两个按钮(继续)(主菜单),如果您单击“继续”,则会正常完成游戏。但是,当您单击主菜单时,它会关闭游戏,而不是返回到介绍,当您与您拥有的汽车相撞(重试)和(主菜单)时,同样的问题也会出现,并且如果您单击主菜单,它会关闭游戏 有什么问题吗?(忽略评论) 导入pygame 导入时间 随机输入 pygame.init() crash\u sound=pygam

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

现在,当您开始游戏并实际玩游戏时,当您按p(暂停)时,会有两个按钮(继续)(主菜单),如果您单击“继续”,则会正常完成游戏。但是,当您单击主菜单时,它会关闭游戏,而不是返回到介绍,当您与您拥有的汽车相撞(重试)和(主菜单)时,同样的问题也会出现,并且如果您单击主菜单,它会关闭游戏

有什么问题吗?(忽略评论)

导入pygame
导入时间
随机输入
pygame.init()
crash\u sound=pygame.mixer.sound(“C:\Users\itzrb\u 000\Documents\Untitled.wav”)
pygame.mixer.music.load(“C:\Users\itzrb\u 000\Downloads\Cool\u Ride.mp3”)
显示宽度=800
显示高度=600
黑色=(0,0,0)
白色=(255255)
红色=(200,0,0)
绿色=(0200,0)
蓝色=(0,0255)
亮绿色=(0255,0)
鲜红色=(255,0,0)
轿厢宽度=50
gameDisplay=pygame.display.set_模式((显示宽度、显示高度))#游戏屏幕大小
pygame.display.set_标题('真好玩')
clock=pygame.time.clock()
carImg=pygame.image.load('C:\Users\itzrb\u 000\Downloads\Car\u Green\u Front.png')
gameBackground=pygame.image.load('C:\WhatARide\u Background.png')
icon=pygame.image.load('C:\Users\itzrb\u 000\Downloads\Car\u Green\u Front-Copy.png')
pygame.display.set_图标(图标)
暂停=错误
car1=pygame.image.load('C:\Users\itzrb\u 000\Downloads\download(3.png'))
car2=pygame.image.load('C:\Users\itzrb\u 000\Downloads\download(2.png'))
car3=pygame.image.load('C:\Users\itzrb\u 000\Downloads\images.png')
rock=pygame.image.load('C:\Users\itzrb\u 000\Downloads\rock.png')
def background():
blit(gameBackground,(0,0))
''def汽车(ystart):
objects=[car1,car2,car3,rock]
对象的数量=random.randint(1,4)
对于范围内的x(对象的数量):
y=随机选择(对象)
objectblit=random.randrange(130625)-(y.get_width())
gameDisplay.blit(y,(random.randrange(130625))“”
def分数(计数):
font=pygame.font.SysFont(无,25)
text=font.render(“分数:+str(计数),真,蓝色)
blit(文本,(0,0))
定义事物(thingx、thingy、thingw、thingh、颜色):
pygame.draw.rect(游戏显示,颜色,[thingx,thingy,thingw,thingh])
def汽车(x,y):
游戏显示.blit(carImg,(x,y))
def text_对象(文本、字体):
textSurface=font.render(文本、真、白)
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',115)
TextSurf,TextRect=text\u对象(text,largeText)
TextRect.center=((显示宽度*0.5),(显示高度*0.5))
gameDisplay.blit(TextSurf,TextRect)#blit显示对象
pygame.display.update()
时间。睡眠(2)
博弈(循环)
def crash():
pygame.mixer.music.stop()
pygame.mixer.Sound.play(崩溃声音)
largeText=pygame.font.font(“freesansbold.ttf”,115)
TextSurf,TextRect=text_对象(“您崩溃了”,largeText)
TextRect.center=((显示宽度*0.5),(显示高度*0.5))
gameDisplay.blit(TextSurf,TextRect)#blit显示对象
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
按钮(“重试!”、300、400、200、50、绿色、亮绿色、游戏循环)
按钮(“主菜单!”,300,470,200,50,红色,鲜红色,游戏介绍)
pygame.display.update()
时钟滴答(20)
def按钮(消息、x、y、w、h、ic、ac、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(游戏显示,ac,(x,y,w,h))
如果单击[0]==1并执行操作!=无:
行动()
其他:
pygame.draw.rect(游戏显示,ic,(x,y,w,h))
smallText=pygame.font.SysFont(“freesansbold.ttf”,20)
textSurf,textRect=text\u对象(msg,smallText)
textRect.center=((x+(w/2)),(y+(h/2)))
blit(textSurf,textRect)
def quitgame():
pygame.quit()
退出
def unpause():
全局暂停
暂停=错误
pygame.mixer.music.unpuse()
def暂停():
全局暂停
暂停=正确
pygame.mixer.music.pause()
largeText=pygame.font.font(“freesansbold.ttf”,115)
TextSurf,TextRect=text\u对象(“暂停”,largeText)
TextRect.center=((显示宽度*0.5),(显示高度*0.5))
gameDisplay.blit(TextSurf,TextRect)#blit显示对象
当pause==True时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
按钮(“继续!”、300、400、200、50、绿色、亮绿色、取消暂停)
按钮(“主菜单”,300、470、200、50、红色、鲜红色、游戏介绍)
pygame.display.update()
时钟滴答(20)
def game_intro():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
游戏显示。填充(蓝色)
largeText=pygame.font.SysFont(“comicsansms”,115)
TextSurf,TextRect=text_对象(“多棒的一次旅行”,largeText)
text rect.center=((显示宽度/2),(显示高度/2))
blit(TextSurf,TextRect)
按钮(“开始游戏”,300、400、200、50、绿色、亮绿色、游戏循环)
按钮(“退出”,30047020050,红色,亮红色,退出游戏)
pygame.display.update()
时钟滴答(20)
def game_loop():
全局暂停
pygame.mixer.music.play(-1)
x=(显示宽度*0.45)
y=(显示高度*0.8)
x_变化=0
thing_startx=随机.randrange(130625)
事情开始=-600
速度=5
物体宽度=100
物体高度=100
减淡=0
gameExit=False
不退出游戏时:
对于pygame.event.get()中的事件:
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()