在pygame中随机创建图像

时间:2017-07-31 19:51:19

标签: python pygame 2d-games

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

  

追踪(最近一次通话):     文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第241行,在       game_intro()     game_intro中的文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第158行       按钮( “START”,150,450,100,50,红,绿, '播放')     文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第89行,按钮       game_loop()     在game_loop中输入文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第190行       卡车(TX,TY)     在卡车中输入文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第32行       gameDisplay.blit(卡车(TX,TY))     在卡车中输入文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第32行       gameDisplay.blit(卡车(TX,TY))     在卡车中输入文件“C:/Users/saifo/PycharmProjects/untitled/gamev2.py”,第32行       gameDisplay.blit(卡车(TX,TY))     [上一行重复992次]   RecursionError:超出最大递归深度

这是我的代码,

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

3 个答案:

答案 0 :(得分:2)

你在方法之间调用函数,这非常糟糕!

可能的答案:

Pygame - RuntimeError: maximum recursion depth exceeded while calling a Python object

祝你好运!

答案 1 :(得分:0)

RecursionError是由truck函数引起的,该函数调用自身直到超过Python的递归限制。此外,truck曲面/图像和truck函数的名称不能相同(def truck(tx, ty):行会覆盖图像变量)。

在我看来,你只是想在这个函数中对truck表面进行blit,所以我建议将其重命名为blit_truck并修复代码:

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

答案 2 :(得分:0)

谢谢你们,实际上,错误是愚蠢的,我错过了一个逗号,在gameDisplay.blit(truck , (tx,ty)),而我写了gameDisplay.blit(truck(tx,ty)),现在它有效。 再次感谢。