对于我正在制作的简单汽车游戏,我真的需要一些代码帮助。我对编码很陌生,所以对于更先进的人来说这可能很容易。 每当你在游戏中点击某个东西时会调用crash()函数,这会导致它运行崩溃循环,然后冻结没有明显的原因(这就是问题)。 请帮助我,我已经尝试将事情分开找到原因,尝试不同的价值观和变量以及其他许多事情,但没有成功。 PS:我使用的是Python 3.6和Pygame 1.9。
有问题的循环:
def crash():
if invincibility==False:
gameExit=True
blitBackground()
text(2,'You crashed!',88,blk,dW/2,dH/6) #(2 means the text coords will start in the middle and not top left)
text(2,('Your total score is '+str(score)+'! '),49,blk,dW/2,dH/3)
text(2,'You crashed!',90,whte,dW/2,dH/6) #(coords, txt, font, colour, x coords, y coords)
text(2,('Your total score is '+str(score)+'! '),50,whte,dW/2,dH/3)
ad=1 #loop variable
buttonW=250 #button Width
buttonH=125 #button height
bX=dW/2 #button position on x axis
#dH and dW is just displayHeight and display Width
bY=dH-dH/3 #button position on y axis
a=1 #variable that shows if you're hovering over button 1 (main menu button)
b=1 #variable that shows if you're hovering over button 2 (keep playing)
while ad<2: #loop
mousePos=pygame.mouse.get_pos()
mClick=pygame.mouse.get_pressed()
if mousePos[0]>bX-buttonW/2+buttonW/2 and mousePos[0]<bX-buttonW/2+buttonW/2+buttonW and mousePos[1]>dH/2+buttonH/1.75 and mousePos[1]<dH/2+buttonH/1.75+buttonH: #basically asks if mouse is hovering over button 1
a=2
gameDisplay.blit(buttonImgHighlighted,(bX-buttonW/1.5,dH/2))
text(2,'Main menu',40,whte,bX-buttonW/2+buttonW/2,dH/2+buttonH/1.75)
text(2,'Main menu',38,blk,bX-buttonW/2+buttonW/2,dH/2+buttonH/1.75)
if mClick[0]==1:
ad=4
mainMenu()
else:
gameDisplay.blit(buttonImgIdle,(bX-buttonW/1.5,dH/2))
if mousePos[0]>bX-buttonW/2+buttonW-buttonW/2 and mousePos[0]<bX-buttonW/2+buttonW-buttonW/2+buttonW and mousePos[1]>dH-dH/4+buttonH/1.75 and mousePos[1]<dH-dH/4+buttonH/1.75+buttonH:
b=2
gameDisplay.blit(buttonImgHighlighted,(bX-buttonW/1.5,dH-dH/4))
text(2,'Keep playing',40,whte,bX-buttonW/2+buttonW-buttonW/2,dH-dH/4+buttonH/1.75)
text(2,'Keep playing',38,blk,bX-buttonW/2+buttonW-buttonW/2,dH-dH/4+buttonH/1.75)
if mClick[0]==1:
ad=4
game_loop()
else:
gameDisplay.blit(buttonImgIdle,(bX-buttonW/1.5,dH-dH/4))
if a==1:
text(2,'Main menu',40,blk,bX-buttonW/2+buttonW/2,dH/2+buttonH/1.75) #if button isn't hovered over, text doesnt have a shadow
if b==1:
text(2,'Keep playing',40,blk,bX-buttonW/2+buttonW-buttonW/2,dH-dH/4+buttonH/1.75)
pygame.display.update()
clock.tick(10)
整个代码:(因此您可以自行运行并更轻松地进行故障排除)
PS:我知道这不完美,但我试图让主要概念失效,然后对其进行优化。import pygame
import time
import random
pygame.init()
carW=55
carH=120
dW=1200#====Default setting
dH=800#=====Everything is optimised for any resolution
#RGB
orange=(255,130,0)
grass=(0,102,51)
yellow=(255,255,0)
blk=(0,0,0)
whte=(255,255,255)
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
gray=(64,64,64)
rdm=(random.randrange(0,255),random.randrange(0,255), random.randrange(0,255))
txtClr=blk
gameDisplay = pygame.display.set_mode((dW,dH))
pygame.display.set_caption('Skrrr skrrr')
clock= pygame.time.Clock()
carImg= pygame.image.load('C:\\Users\\gabyb\\Desktop\\Python code\\Resources\\v.jpg')
carImg= pygame.transform.scale(carImg, (carW, carH))
buttonImgIdle=pygame.image.load('C:\\Users\\gabyb\\Desktop\\Python code\\Resources\\button.png')
buttonImgIdle= pygame.transform.scale(buttonImgIdle, (int(dW/3.42857142857), int(dH/5.51724137931)))
buttonImgHighlighted=pygame.image.load('C:\\Users\\gabyb\\Desktop\\Python code\\Resources\\buttonHilighted.png')
buttonImgHighlighted=pygame.transform.scale(buttonImgHighlighted, (int(dW/3.42857142857), int(dH/5.51724137931)))
menuBackground=pygame.image.load('C:\\Users\\gabyb\\Desktop\\Python code\\Resources\\menu.jpg')
menuBackground= pygame.transform.scale(menuBackground, (dW, dH))
########################################################################################Function list
def score_count():
text(2,'Score: '+str(score),36,blk,roadEnd/2-5,25)
text(2,'Score: '+str(score),34,whte,roadEnd/2-5,25)
def rectangle(thngX,thngY,thngW,thngH,colour):
pygame.draw.rect(gameDisplay, colour, [thngX,thngY,thngW,thngH])
def car(x,y):
gameDisplay.blit(carImg,(x,y))
def text_objects(text, font,colour):
textSurface=font.render(text, True, colour)
return textSurface, textSurface.get_rect()
def message_display(text,seconds,colour,Wloc,Hloc,size):
txtClr=colour
largeText=pygame.font.Font('freesansbold.ttf',size)#90
TextSurf, TextRect = text_objects(text,largeText, txtClr)
TextRect.center=((Wloc),(Hloc))
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(seconds)
def text(c,text,size,colour,Wloc,Hloc):
if c==2:
txtClr=colour
largeText=pygame.font.Font('freesansbold.ttf',size)#90
TextSurf, TextRect = text_objects(text,largeText, txtClr)
TextRect.center=((Wloc),(Hloc))
gameDisplay.blit(TextSurf, TextRect)
if c==1:
font=pygame.font.SysFont(None,size)
tcxt=font.render(str(text),True, colour)
gameDisplay.blit(tcxt,(Wloc,Hloc))
#def crash():
# if invincibility==False:
# blitBackground()
# txtClr=(blue)
# text(2,'You crashed!',70,blk,dW/2,dH/2.75) #(text,seconds,colour,Wloc,Hloc,size)
# text(2,('Your total score is '+str(score)+'! '),50,whte,dW/2,(dH/3.5)*2)
# pygame.display.update()
# time.sleep(4)
# game_loop()
def crash():
if invincibility==False:
gameExit=True
blitBackground()
txtClr=(blue)
text(2,'You crashed!',88,blk,dW/2,dH/6) #(text,seconds,colour,Wloc,Hloc,size)
text(2,('Your total score is '+str(score)+'! '),49,blk,dW/2,dH/3)
text(2,'You crashed!',90,whte,dW/2,dH/6) #(text,seconds,colour,Wloc,Hloc,size)
text(2,('Your total score is '+str(score)+'! '),50,whte,dW/2,dH/3)
ad=1
buttonW=250
buttonH=125
bX=dW/2
bY=dH-dH/3
a=1
b=1
while ad<2:
mousePos=pygame.mouse.get_pos()
#print (mousePos)
mClick=pygame.mouse.get_pressed()
#print(mClick)
if mousePos[0]>bX-buttonW/2+buttonW/2 and mousePos[0]<bX-buttonW/2+buttonW/2+buttonW and mousePos[1]>dH/2+buttonH/1.75 and mousePos[1]<dH/2+buttonH/1.75+buttonH:
a=1
gameDisplay.blit(buttonImgIdle,(bX-buttonW/1.5,dH/2))
text(2,'Main menu',40,whte,bX-buttonW/2+buttonW/2,dH/2+buttonH/1.75)
text(2,'Main menu',38,blk,bX-buttonW/2+buttonW/2,dH/2+buttonH/1.75)
if mClick[0]==1:
ad=4
mainMenu()
else:
gameDisplay.blit(buttonImgHighlighted,(bX-buttonW/1.5,dH/2))
if mousePos[0]>bX-buttonW/2+buttonW-buttonW/2 and mousePos[0]<bX-buttonW/2+buttonW-buttonW/2+buttonW and mousePos[1]>dH-dH/4+buttonH/1.75 and mousePos[1]<dH-dH/4+buttonH/1.75+buttonH:
b=2
gameDisplay.blit(buttonImgIdle,(bX-buttonW/1.5,dH-dH/4))
text(2,'Keep playing',40,whte,bX-buttonW/2+buttonW-buttonW/2,dH-dH/4+buttonH/1.75)
text(2,'Keep playing',38,blk,bX-buttonW/2+buttonW-buttonW/2,dH-dH/4+buttonH/1.75)
if mClick[0]==1:
ad=4
game_loop()
else:
gameDisplay.blit(buttonImgHighlighted,(bX-buttonW/1.5,dH-dH/4))
if a==1:
text(2,'Main menu',40,blk,bX-buttonW/2+buttonW/2,dH/2+buttonH/1.75)
if b==1:
text(2,'Keep playing',40,blk,bX-buttonW/2+buttonW-buttonW/2,dH-dH/4+buttonH/1.75)
pygame.display.update()
clock.tick(10)
def blitBackground():
gameDisplay.fill(gray)
rectangle(0,0,roadEnd,dH,grass)
rectangle(dW-roadEnd,0,roadEnd,dH,grass)
edge(0,whte)
edges()
car(x,y)
rectangle(thng_strtX, thng_strtY, thng_width, thng_height, rdm)
rectangle(thng_strtXa, thng_strtYa, thng_width, thng_height, rdm)
rectangle(thng_strtXb, thng_strtYb, thng_width, thng_height, rdm)
rectangle(thng_strtXc, thng_strtYc, thng_width, thng_height, rdm)
rectangle(point_strtX, point_strtY, point_width, point_height, green)#######
score_count()
pygame.display.update()
def getReady():
blitBackground()
message_display('Ready?',1,red,dW/2,dH/2,90)#(text,seconds,colour,Wloc,Hloc,size)
blitBackground()
message_display('Get set.',1, yellow,dW/2,dH/2,90)
blitBackground()
message_display('Go!',1,green ,dW/2,dH/2,90)
blitBackground()
def edge(p,r):
rectangle(roadEnd-10,sqry+p,26,20,r)
rectangle(dW-roadEnd-13,sqry+p,26,20,r)
def cones(add,a):
edge(20+add,red)
edge(40+add,whte)
edge(60+add,red)
edge(80+add,whte)
edge(20-a,red)
edge(40-a,whte)
edge(60-a,red)
edge(80-a,whte)
def edges():
cones(0,0)
cones(80,0)
cones(160,0)
cones(240,0)
cones(320,0)
cones(400,0)
cones(480,0)
cones(560,0)
cones(640,0)
cones(720,0)
cones(800,0)
cones(0,80)
cones(0,160)
cones(0,240)
cones(0,320)
cones(0,400)
cones(0,480)
cones(0,560)
cones(0,640)
cones(0,720)
cones(0,800)
#######################################################################################
def mainMenu():
mainMenu=True
buttonW=int(dW/3.42857143)#349
buttonH=int(dH/5.51724138)#144
avg=(buttonW+buttonH)/2 #246.5
while mainMenu==True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
print ('end')
if event.type==pygame.KEYDOWN:
#'''quitt'''
if event.key==pygame.K_ESCAPE:
pygame.quit()
print ('end')
gameDisplay.blit(menuBackground,(0,0))
text(2,'Skrr skrr',int(avg/1.82592592593),blk,dW/2,dH/10)#135
text(2,'Skrr skrr',int(avg/1.89615384615),whte,dW/2,dH/10)#130
pygame.draw.line(gameDisplay, blk,(dW/2-int(avg/0.8216),dH/10+int(avg/4.1083)),(dW/2+int(avg/0.8216), dH/10+int(avg/4.1083)),int(avg/18.9615384615))
pygame.draw.line(gameDisplay, whte,(dW/2-int(avg/0.8216),dH/10+int(avg/4.1083)),(dW/2+int(avg/0.8216), dH/10+int(avg/4.1083)),int(avg/49.3))
mousePos=pygame.mouse.get_pos()
mouseClick= pygame.mouse.get_pressed()
#W-350 H-145
a=1
b=1
if mousePos[0]>dW/10 and mousePos[0]<dW/10+buttonW and mousePos[1]>dH/2 and mousePos[1]<dH/2+buttonH:
gameDisplay.blit(buttonImgHighlighted,(dW/10,dH/2))
a=2
text(2,'Start',int(avg/4.95+2),blk,dW/10+buttonW/2,dH/2+buttonH/2)#52
text(2,'Start',int(avg/4.95),whte,dW/10+buttonW/2,dH/2+buttonH/2)
if mouseClick[0]==1:
mainMenu=False
game_loop()
else:
gameDisplay.blit(buttonImgIdle,(dW/10,dH/2))
if mousePos[0]>dW-dW/10-buttonW and mousePos[0]<dW-dW/10 and mousePos[1]>dH/2 and mousePos[1]<dH/2+buttonH:
gameDisplay.blit(buttonImgHighlighted,(dW-dW/10-350,dH/2))
b=2
text(2,'No, don\'t!',int(avg/4.95+2),whte,dW-dW/10-buttonW+buttonW/2,dH/2+buttonH/2)
text(2,'No, don\'t!',int(avg/4.95),red,dW-dW/10-buttonW+buttonW/2,dH/2+buttonH/2)
if mouseClick[0]==1:
mainMenu=False
pygame.quit()
else:
gameDisplay.blit(buttonImgIdle,(dW-dW/10-buttonW,dH/2))
if a==1:
text(2,'Start',int(avg/4.95+2),whte,dW/10+buttonW/2,dH/2+buttonH/2)
text(2,'Start',int(avg/4.95),blk,dW/10+buttonW/2,dH/2+buttonH/2)
if b==1:
text(2,'Quit',int(avg/4.95+2),whte,dW-dW/10-buttonW+buttonW/2,dH/2+buttonH/2)
text(2,'Quit',int(avg/4.95),blk,dW-dW/10-buttonW+buttonW/2,dH/2+buttonH/2)
pygame.display.update()
########################################################################################
#gameloooooooooooooooooooooooop
def game_loop():
devmode=True
hg=0
global z
global h
z=0
h=0
global roadEnd
roadEnd=200
global score
score=0
global x
global y
x=(dW * 0.47)
y=(dH*0.75)
global sqry
sqry=0
paus=False
x_change=0
y_change=0
global invincibility
invincibility=False
toggle=1
speed=3
akd=1
global thng_width
global thng_strtX
global thng_strtY
global thng_height
global thng_strtXa
global thng_strtYa
global thng_strtXb
global thng_strtYb
global thng_strtXc
global thng_strtYc
global thng_strtXd
global thng_strtYd
thng_width=55
thng_strtX=random.randrange(roadEnd+thng_width,dW-roadEnd-thng_width)
thng_strtY=-450 #obstacle dymentions
thng_height=120
thng_strtXa=random.randrange(roadEnd+thng_width,dW-roadEnd-thng_width)
thng_strtYa=-450
thng_strtXb=random.randrange(roadEnd+thng_width,dW-roadEnd-thng_width)
thng_strtYb=-450
thng_strtXc=random.randrange(roadEnd+thng_width,dW-roadEnd-thng_width)
thng_strtYc=-450
thng_strtXd=random.randrange(roadEnd+thng_width,dW-roadEnd-thng_width)
thng_strtYd=-450
global point_width
global point_strtX
global point_strtY
global point_height
point_width=20
point_strtX=random.randrange(roadEnd+point_width+10+10,dW-roadEnd-point_width-20) #point dymentions
point_strtY=-450
point_height=20
x=(dW * 0.47)
y=(dH*0.75)
car(x,y)
pygame.display.update()
getReady()
gameExit=False
while not gameExit:
gameDisplay.fill(gray)
rectangle(0,0,roadEnd,dH,grass)
rectangle(dW-roadEnd,0,roadEnd,dH,grass)
edge(0,whte)
edges()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
print ('end')
if event.type==pygame.KEYDOWN: #'''Pressing of controls'''
if event.key==pygame.K_ESCAPE:
gameExit=True
mainMenu()
if event.key == pygame.K_a:
x_change = -3
if event.key==pygame.K_d:
x_change=3
if event.key==pygame.K_w:
y_change=-2.4
if event.key==pygame.K_s:
y_change=4.5
#if devmode==False:
#if event.key == pygame.K_p:
#############################normal pause feature
if devmode==True:
if event.key == pygame.K_z:
hg=-0.01
if event.key == pygame.K_x:
hg=0.01
if event.key==pygame.K_b:
speed+=0.5
#add text to tell you if youre invincible, your speed, etc.
if event.key==pygame.K_v:
speed+=-0.5
if event.key==pygame.K_e:
if akd==1:
akd=2
ahg=speed
speed=0
paus=True
#######add pause feature
elif akd==2:
akd=1
speed=ahg
paus=False
if event.key==pygame.K_c:
score+=10
if event.key==pygame.K_q:
if toggle==1:
invincibility=True
toggle=2
else:
invincibility=False
toggle=1
if event.type==pygame.KEYUP: #'''Release of controls'''
if event.key == pygame.K_z or event.key == pygame.K_x:
hg=0
if event.key==pygame.K_a or event.key==pygame.K_d:
x_change = 0
if event.key==pygame.K_w or event.key==pygame.K_s:
y_change = 0
if event.key==pygame.K_x or event.key==pygame.K_z:
speed=speed
if speed<0:
speed=0
speed=speed+hg
sqry=sqry+speed*1.55
#x,y,w,h,color
#####add start line
#add cars starting race with you and speeding
#off top of screen
#find way to de-blit said finishing line and cars
#after a while, pass the finishing line again..
#maybe add powerups like slowmotion (change clock ticks to less and move mouse or give lots of inputs to print)
#,invincibility for like 3 secs etc
#fix things spawing inside each other
#add models
#replace images with royality free ones
x=x+x_change
y=y+y_change #Movement
car(x,y)
score_count()
rectangle(thng_strtX, thng_strtY, thng_width, thng_height, rdm)#######
thng_strtY += speed
rectangle(point_strtX, point_strtY, point_width, point_height, green)#######
point_strtY += speed
if z>1:
rectangle(thng_strtXa, thng_strtYa, thng_width, thng_height, rdm)
thng_strtYa += speed*(random.randrange(100,150)/100)
if x+55>thng_strtXa+6.5 and x<thng_strtXa+thng_width-6.5 and y<thng_strtYa+thng_height-6.5 and y+120>thng_strtYa+6.5:
crash()
if z>2:
rectangle(thng_strtXb, thng_strtYb, thng_width, thng_height, rdm)
thng_strtYb += speed*(random.randrange(100,150)/150)
if x+55>thng_strtXb+6.5 and x<thng_strtXb+thng_width-6.5 and y<thng_strtYb+thng_height-6.5 and y+120>thng_strtYb+6.5:
crash()
if z>3:
rectangle(thng_strtXc, thng_strtYc, thng_width, thng_height, rdm)
thng_strtYc += speed*(random.randrange(100,150)/150)
if x+55>thng_strtXc+6.5 and x<thng_strtXc+thng_width-6.5 and y<thng_strtYc+thng_height-6.5 and y+120>thng_strtYc+6.5:
crash()
if sqry>dH:
sqry=-30
if thng_strtYb>dH:
thng_strtYb=0-thng_width-20 #obstcles
thng_strtXb=random.randrange(roadEnd+10+10+10,dW-roadEnd-thng_width-10-10)
if thng_strtYc>dH:
thng_strtYc=0-thng_width-20 #obstcles
thng_strtXc=random.randrange(roadEnd+10+10+10,dW-roadEnd-thng_width-10-10)
if thng_strtY>dH:
thng_strtY=0-thng_width-20 #obstcles
thng_strtX=random.randrange(roadEnd+10+10+10,dW-roadEnd-thng_width-10-10)
if point_strtY>dH:
point_strtY=0-thng_width-20
point_strtX=random.randrange(roadEnd+5+10+10+10,dW-roadEnd-point_width-5-10-10)
if x>dW-roadEnd+10-55:
if invincibility==False:
x_change=4.5
if event.type==pygame.KEYUP:
if event.key==pygame.K_a or event.key==pygame.K_d:
x_change = 4.5
if event.key==pygame.K_w or event.key==pygame.K_s:
y_change = 1.5
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_a or event.key==pygame.K_d:
x_change = 4.5
if event.key==pygame.K_w or event.key==pygame.K_s:
y_change = 1.5
if x>dW+4-55: #spinning out and crashing
crash()
if x<roadEnd-10:
if invincibility==False:
x_change= -4.5
if event.type==pygame.KEYUP:
if event.key==pygame.K_a or event.key==pygame.K_d:
x_change = -4.5
if event.key==pygame.K_w or event.key==pygame.K_s:
y_change = 1.5
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_a or event.key==pygame.K_d:
x_change = -4.5
if event.key==pygame.K_w or event.key==pygame.K_s:
y_change = 1.5
if x<0-4:
crash()
if x+55>thng_strtX+6.5 and x<thng_strtX+thng_width-6.5 and y<thng_strtY+thng_height-6.5 and y+120>thng_strtY+6.5:
crash()
if x+55>point_strtX and x<point_strtX+point_width and y<point_strtY+point_height and y+120>point_strtY:
point_strtY=0-thng_width-20 #points
point_strtX=random.randrange(roadEnd+10+5,dW-roadEnd-15)
score=score+1
speed+=0.063
if score%10==0:
z=z+1
if z>4:
z=4
if thng_strtYa>dH:
thng_strtYa=0-thng_width-20 #obstcles respawn
thng_strtXa=random.randrange(roadEnd+10+10+10,dW-roadEnd-thng_width-10-10)
if y<=dH/10+2:
y_change=+0
#'''Stopping point for top of screen'''
if y<dH/10:
y_change=+2
if y>dH-152:
y_change=-0
#'''Stopping point for botton of screen'''
if y>dH-148:
y_change=-4
if paus==True:
text(2,'Paused.',90,orange,dW/2,dH/2)
pygame.display.update()
clock.tick(5000)
mainMenu()
pygame.quit()
print ('end')