gpt4 book ai didi

python - 在pygame中随机创建图像

转载 作者:行者123 更新时间:2023-12-01 02:43:35 25 4
gpt4 key购买 nike

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

Traceback (most recent call last): File "C:/Users/saifo/PycharmProjects/untitled/gamev2.py", line 241, in game_intro() File "C:/Users/saifo/PycharmProjects/untitled/gamev2.py", line 158, in game_intro button("START",150,450,100,50,red,green,'Play') File "C:/Users/saifo/PycharmProjects/untitled/gamev2.py", line 89, in button game_loop() File "C:/Users/saifo/PycharmProjects/untitled/gamev2.py", line 190, in game_loop truck(tx,ty) File "C:/Users/saifo/PycharmProjects/untitled/gamev2.py", line 32, in truck gameDisplay.blit(truck(tx,ty)) File "C:/Users/saifo/PycharmProjects/untitled/gamev2.py", line 32, in truck gameDisplay.blit(truck(tx,ty)) File "C:/Users/saifo/PycharmProjects/untitled/gamev2.py", line 32, in truck gameDisplay.blit(truck(tx,ty)) [Previous line repeated 992 more times] RecursionError: maximum recursion depth exceeded

这是我的代码,

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 - RuntimeError: maximum recursion depth exceeded while calling a Python object

祝你好运!

关于python - 在pygame中随机创建图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45424216/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com