Python 在pygame中随机创建图像

Python 在pygame中随机创建图像,python,pygame,2d-games,Python,Pygame,2d Games,我是编程新手,我正在尝试使用pygame制作一个简单的汽车游戏,一切正常,直到我尝试使汽车从相反方向随机出现,我成功地使矩形随机出现,但当我想使用一个矩形精灵时,它不起作用,我得到的错误是: 回溯(最近一次呼叫最后一次): 文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第241行,在 游戏介绍() 文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第158行,在游戏介绍中 按钮

我是编程新手,我正在尝试使用pygame制作一个简单的汽车游戏,一切正常,直到我尝试使汽车从相反方向随机出现,我成功地使矩形随机出现,但当我想使用一个矩形精灵时,它不起作用,我得到的错误是:

回溯(最近一次呼叫最后一次): 文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第241行,在 游戏介绍() 文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第158行,在游戏介绍中 按钮(“开始”,150450100,50,红色,绿色,“播放”) 文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第89行,在按钮中 博弈(循环) 文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第190行,在game_循环中 卡车(德克萨斯州、ty州) 文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第32行,卡车中 游戏显示。blit(卡车(德克萨斯州,德克萨斯州)) 文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第32行,卡车中 游戏显示。blit(卡车(德克萨斯州,德克萨斯州)) 文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第32行,卡车中 游戏显示。blit(卡车(德克萨斯州,德克萨斯州)) [上一行重复了992次] 递归错误:超过最大递归深度

这是我的密码

import pygame
import time
import random
pygame.init()

pygame.mixer.music.load('C:/Users/saifo/Downloads/Documents/sprites/khaled.mp3')
crash_sound= pygame.mixer.Sound('C:/Users/saifo/Downloads/Documents/sprites/crash.wav')
height=600
width=800
white=(255,255,255)
black=(0,0,0)
red=(255,0,0)
green=(0,255,0)
bleu=(0,0,255)

#x_change=0
gameDisplay= pygame.display.set_mode((width,height))
pygame.display.set_caption('Racing')
clock =pygame.time.Clock()

carImg = pygame.image.load('C:/Users/saifo/Downloads/Documents/sprites/viper.png')
backImg = pygame.image.load('C:/Users/saifo/Downloads/Documents/sprites/back.png')
truck = pygame.image.load('C:/Users/saifo/Downloads/Documents/sprites/truck.png')
carImg = pygame.transform.scale(carImg,(100,100))
truck = pygame.transform.scale(truck,(100,100))
backImg=pygame.transform.scale(backImg,(800,600))
carWidth =100
pause = False
pygame.display.set_icon(carImg)

def truck(tx,ty):
    gameDisplay.blit(truck(tx,ty))

# 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 dodged(count):
    font =pygame.font.SysFont(None,25)
    text =font.render("Dodged :" + str(count),True,white)
    gameDisplay.blit(text,(0,0))

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


def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',80)
    textSurf,textRect = text_objects(text,largeText)
    textRect.center=((width/2),(height/2))
    gameDisplay.blit(textSurf,textRect)

    pygame.display.update()

    time.sleep(2)
    game_loop()

def crash():
    pygame.mixer.music.stop()
    pygame.mixer.Sound.play(crash_sound)
    message_display('you crashed')

def button(msg,x,y,w,h,a,i,action=None):


    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    #print(click,mouse)
    if x + w > mouse[0] > x and y + h > mouse[1] > y:
        pygame.draw.rect(gameDisplay, a, (x, y, w, h))
        if click[0]==1 and action!=None:
            if action=='Play':
                game_loop()
            elif action=='Quit':
                pygame.QUIT
                quit()
            elif action=='Resume':
                unpause()

    smalltext = pygame.font.Font('freesansbold.ttf', 25)
    textsurf ,textrect=text_objects(msg,smalltext)
    textrect.center=((x+(w/2)),(y+(h/2)))
    gameDisplay.blit(textsurf,textrect)
    pygame.display.update()

def unpause():
    global pause
    pause = False
    pygame.mixer.music.unpause()


def paused():
    pygame.mixer.music.pause()
    while pause:

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

        gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf', 80)
        textSurf, textRect = text_objects("PAUSED", largeText)
        textRect.center = ((width / 2), (height / 2))
        gameDisplay.blit(textSurf, textRect)


        pygame.draw.rect(gameDisplay,green,(150,450,100,50))
        pygame.draw.rect(gameDisplay,bleu,(550,450,100,50))

        button("RESUME",150,450,120,50,red,green,'Resume')
        button("QUIT",550,450,110,50,red,bleu,'Quit')



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


def game_intro():
    intro = True

    while intro:

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

        gameDisplay.fill(white)
        largeText = pygame.font.Font('freesansbold.ttf', 80)
        textSurf, textRect = text_objects("Formula 00", largeText)
        textRect.center = ((width / 2), (height / 2))
        gameDisplay.blit(textSurf, textRect)


        pygame.draw.rect(gameDisplay,green,(150,450,100,50))
        pygame.draw.rect(gameDisplay,bleu,(550,450,100,50))

        button("START",150,450,100,50,red,green,'Play')
        button("QUIT",550,450,100,50,red,bleu,'Quit')



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



def game_loop():
    global  pause
    pygame.mixer.music.play(-1)

    x_change=0
    x=(width*0.45)
    y=(height*0.8)
    tx=random.randrange(0,width)
    ty=-100
    thing_startx=random.randrange(0,width)
    thing_starty=-100
    thing_speed=7
    thingw=60
    thingh=60
    dodge=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



        gameDisplay.fill(white)
        gameDisplay.blit(backImg,(0,0))
        truck(tx,ty)
        ty+=thing_speed
        # things(thing_startx,thing_starty,thingw,thingh,red)
        # thing_starty +=thing_speed
        car(x,y)
        dodged(dodge)
        x += x_change

        if x > width - carWidth or x < 0:
            crash()

        if ty > height:
            ty = 0 - 100
            tx = random.randrange(0, width)
            dodge+=1


        if y < ty+100:
            if x > tx and x < tx + 100 or x + carWidth>tx and x + carWidth < tx+100:
                crash()


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

game_intro()
game_loop()
pygame.quit()
quit()
导入pygame
导入时间
随机输入
pygame.init()
pygame.mixer.music.load('C:/Users/saifo/Downloads/Documents/sprites/khaled.mp3')
crash\u sound=pygame.mixer.sound('C:/Users/saifo/Downloads/Documents/sprites/crash.wav'))
高度=600
宽度=800
白色=(255255)
黑色=(0,0,0)
红色=(255,0,0)
绿色=(0255,0)
bleu=(0,0255)
#x_变化=0
gameDisplay=pygame.display.set_模式((宽度、高度))
pygame.display.set_标题('赛车')
clock=pygame.time.clock()
carImg=pygame.image.load('C:/Users/saifo/Downloads/Documents/sprites/viper.png')
backImg=pygame.image.load('C:/Users/saifo/Downloads/Documents/sprites/back.png')
truck=pygame.image.load('C:/Users/saifo/Downloads/Documents/sprites/truck.png')
carImg=pygame.transform.scale(carImg,(100100))
卡车=pygame.transform.scale(卡车,(100100))
backImg=pygame.transform.scale(backImg,(800600))
车宽=100
暂停=错误
pygame.display.set_图标(carImg)
def卡车(德克萨斯州、德克萨斯州):
游戏显示。blit(卡车(德克萨斯州,德克萨斯州))
#定义事物(thingx、thingy、thingw、thingh、颜色):
#pygame.draw.rect(游戏显示,颜色,[thingx,thingy,thingw,thingh])
def汽车(x,y):
游戏显示.blit(carImg,(x,y))
def闪避(计数):
font=pygame.font.SysFont(无,25)
text=font.render(“减淡:”+str(计数),真,白色)
blit(文本,(0,0))
def text_对象(文本、字体):
textSurface=font.render(文本,真,黑色)
返回textSurface,textSurface.get_rect()
def信息_显示(文本):
largeText=pygame.font.font('freesansbold.ttf',80)
textSurf,textRect=text\u对象(text,largeText)
textRect.center=((宽度/2),(高度/2))
blit(textSurf,textRect)
pygame.display.update()
时间。睡眠(2)
博弈(循环)
def crash():
pygame.mixer.music.stop()
pygame.mixer.Sound.play(崩溃声音)
信息显示(“您崩溃了”)
def按钮(消息、x、y、w、h、a、i、操作=无):
mouse=pygame.mouse.get_pos()
click=pygame.mouse.get_pressed()
#打印(单击鼠标)
如果x+w>鼠标[0]>x和y+h>鼠标[1]>y:
pygame.draw.rect(游戏显示,a,(x,y,w,h))
如果单击[0]==1并执行操作=无:
如果动作=='Play':
博弈(循环)
elif操作=='Quit':
pygame.退出
退出
elif操作=='Resume':
取消暂停
smalltext=pygame.font.font('freesansbold.ttf',25)
textsurf,textrect=text\u对象(msg,smalltext)
textrect.center=((x+(w/2)),(y+(h/2)))
blit(textsurf,textrect)
pygame.display.update()
def unpause():
全局暂停
暂停=错误
pygame.mixer.music.unpuse()
def暂停():
pygame.mixer.music.pause()
暂停时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
游戏显示。填充(白色)
largeText=pygame.font.font('freesansbold.ttf',80)
textSurf,textRect=text\u对象(“暂停”,largeText)
textRect.center=((宽度/2),(高度/2))
blit(textSurf,textRect)
pygame.draw.rect(游戏显示,绿色,(150450100,50))
pygame.draw.rect(游戏显示,bleu,(550450100,50))
按钮(“恢复”,150450120,50,红色,绿色,“恢复”)
按钮(“退出”,550450110,50,红色,蓝色,“退出”)
pygame.display.update()
时钟滴答(15)
def game_intro():
简介=正确
而简介:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
游戏显示。填充(白色)
largeText=pygame.font.font('freesansbold.ttf',80)
textSurf,textRect=text_对象(“公式00”,largeText)
textRect.center=((宽度/2),(高度/2))
blit(textSurf,textRect)
pygame.draw.rect(游戏显示,绿色,(150450100,50))
pygame.draw.rect(游戏显示,bleu,(550450100,50))
按钮(“开始”,150450100,50,红色,绿色,“播放”)
按钮(“退出”,550450100,50,红色,蓝色,“退出”)
pygame.display.update()
时钟滴答(15)
def game_loop():
全局暂停
pygame.mixer.music.play(-1)
x_变化=0
x=(宽度*0.45)
y=(高度*0.8)
tx=随机。随机范围(0,宽度)
ty=-100
thing_startx=随机.randrange(0,宽度)
事情开始=-100
速度=7
thingw=60
thingh=60
道奇=0
gameExit=False
不退出游戏时:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
退出
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_左:
x_变化=-5
艾利夫·伊夫
def blit_truck(tx,ty):
    gameDisplay.blit(truck, (tx, ty))