当我按下Run时,我的开始屏幕不会启动!按钮



自从我为游戏添加了开始屏幕,我什么时候会按Run/启动按钮不会,屏幕会冻结,我什么都按不了。我的Sit/quit工作得很好,但我的Run/Start不会

https://gyazo.com/f0dfef5bc4be1875e6928d79a0b792fd

正如你从我的简短演示中看到的,如果我按下"运行/启动"按钮,我的游戏就会冻结,停止工作。我试过重写我的代码,但仍然不起作用,我想我遗漏了一些东西,但我不知道是什么。

这就是我遇到问题的地方

##############################################
# START MENUE
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(window, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()         
else:
pygame.draw.rect(window, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
window.blit(textSurf, textRect)
def quitgame():
pygame.quit()

def game_intro():
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
largeText = pygame.font.Font('freesansbold.ttf',95)
TextSurf, TextRect = text_objects("Lava Runer", largeText)
TextRect.center = ((500/2),(500/2))
window.blit(TextSurf, TextRect)

button("Run!",100,350,100,50,darkgreen,green,main_loop)
button("Sit!",300,350,100,50,darkred,red,quitgame)
pygame.display.update()
clock.tick(15)
bg = pygame.image.load("Sky2.jpg")
window.blit(bg,(0,0))


############################################

这是我的完整代码

import pygame
pygame.font.init()
pygame.init()
#set screen
window = pygame.display.set_mode((500,500))
#set Name
pygame.display.set_caption("Noob")

class player:
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.speed = 0
self.isJump = False
self.JumpCount = 10
self.fall = 0
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.topleft = (self.x,self.y)
class Floor:
def __init__ (self,x,y,height,width,color):
self.x = x
self.y = y
self.height = height
self.width = width
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)

class Coin():
def __init__(self,x,y,height,width,color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = pygame.Rect(x,y,height,width)
def draw(self):
self.topleft = (self.x,self.y)
pygame.draw.rect(window,self.color,self.rect)


white = (255,255,255)
green = (0,255,0)
red = (255,0,0)
darkred = (200,0,0)
darkgreen = (0,200,0)
black = (0,0,0)

player1 = player(50,400,40,40,white)
coin1 = Coin(100,300,30,30,red)
coin2 = Coin(200,300,30,30,red)
floor1 = Floor(0,0,1000,30,green)
floor2 = Floor(0,400,1000,30,green)

coins = [coin1,coin2]
floors = [floor1,floor2]


fps = (30)
clock = pygame.time.Clock()
##############################################
# START MENUE
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def button(msg,x,y,w,h,ic,ac,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
#print(click)
if x+w > mouse[0] > x and y+h > mouse[1] > y:
pygame.draw.rect(window, ac,(x,y,w,h))
if click[0] == 1 and action != None:
action()         
else:
pygame.draw.rect(window, ic,(x,y,w,h))
smallText = pygame.font.SysFont("comicsansms",20)
textSurf, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(w/2)), (y+(h/2)) )
window.blit(textSurf, textRect)
def quitgame():
pygame.quit()

def game_intro():
intro = True
while intro:
for event in pygame.event.get():
#print(event)
if event.type == pygame.QUIT:
pygame.quit()
quit()
largeText = pygame.font.Font('freesansbold.ttf',95)
TextSurf, TextRect = text_objects("Lava Runer", largeText)
TextRect.center = ((500/2),(500/2))
window.blit(TextSurf, TextRect)

button("Run!",100,350,100,50,darkgreen,green,main_loop)
button("Sit!",300,350,100,50,darkred,red,quitgame)
pygame.display.update()
clock.tick(15)
bg = pygame.image.load("Sky2.jpg")
window.blit(bg,(0,0))


############################################
def main_loop():
#window
def redrawwindow():
window.fill((0,0,0))    
#draw plyer
player1.draw()
for Coin in coins:
Coin.draw()
for Floor in floors:
Floor.draw()


# the score draw it on the screen
window.blit(text,textRect)

# Font for coin   
font  = pygame.font.Font("freesansbold.ttf",30)
score = 0
text = font.render("Coins = "+str(score),True,(255,255,255))
textRect = text.get_rect()
textRect.center = ((100,40))

run = True
while run:
clock.tick(fps)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False

# coin collisions
for Coin in coins:
for one in range(len(coins)-1-1-1):
if player1.rect.colliderect(coins[one].rect):
del coins[one]
score += 1
text = pygame.font.Font("comicsansms",30)
textRect.center = (100,40)

# Keys for player
keys = pygame.key.get_pressed()
if keys[pygame.K_a]and player1.x > player1.speed:
player1.x -= player1.speed
if keys[pygame.K_d]and player1.x <500 - player1.height - player1.speed:
player1.x += player1.speed
if keys[pygame.K_w]and player1.y > player1.speed:
player1.y -= player1.speed
if keys[pygame.K_s]and player1.y <500 - player1.width - player1.speed:
player1.y += player1.speed

# Fall
if not player1.isJump:
player1.y += player1.fall
player1.fall += 1
player1.isJump = False

# Collide with Floor
collide = False
for Floor in floors:
if player1.rect.colliderect(Floor.rect):
collide = True
player1.isJump = False
player1.y = Floor.rect.top - player1.height + 1
if player1.rect.right > Floor.rect.left and player1.rect.left > Floor.rect.right - player1.width:
player1.x = Floor1.rect.left - player1.width
if player1.rect.left < Floor.rect.right and player1.rect.right > Floor.rect.left + player1.width:
player1.x = Floor.rect.right

if player1.rect.bottom >= 500:
collide = True
player1.isJump = False
player1.JumpCount = 10
player1.y = 500 - player1.height
if collide:
if keys[pygame.K_SPACE]:
player1.isJump = True
player1.fall = 0
else:
if player1.JumpCount >= 0:
player1.y -= (player1.JumpCount*abs(player1.JumpCount))*0.3
player1.JumpCount -= 1
else:
player1.JumpCount = 10
player1.isJump = False


redrawwindow()
pygame.display.update()
pygame.quit()
game_intro()
main_loop()

所以这就是你开始游戏的地方。?

button("Run!",100,350,100,50,darkgreen,green,main_loop)
button("Sit!",300,350,100,50,darkred,red,quitgame)

我想说不要像这样直接运行主循环。好的…你想做的事情,你想改变一个与事件相关的状态,已经被触发了。否则,您将过度运行主循环,因为您已经在while循环中做好了准备。您将这个函数引导到一个函数it中,它可以像这样更改全局变量的状态。并让主函数根据全局变量的状态运行。

button("Run!",100,350,100,50,darkgreen,green,main_loop_state_turn_on)

然后继续评估它相对于按钮的位置。

event_i = False
def main_loop_state_turn_on():
global event_i = true
def main_loop_state_turn_off():
global event_i = false

如果这些全局变量为true,那么在main循环内部创建运行while循环的方法。

def main_loop():
if event_i:
print 'Begin'
#your code is going on here

为了获得更多的理解,你可以查阅这个

最新更新