信息:我目前正在制作一款游戏,想要在开始之前先了解开始菜单,因为这有助于我想要设计的游戏的其他部分。基本上我想要它,所以当我按START GAME
游戏开始时,当我按HELP
它会显示帮助页面时,当我按QUIT
时游戏将退出。到目前为止,当QUIT
被按下时游戏退出了,这很简单,但我仍然坚持开始游戏。
问题: 当我按下开始游戏时,它会显示播放器精灵以及时钟。时钟和帧一样有效,直到移动鼠标。
代码
import pygame, random, time
pygame.init()
#Screen
SIZE = width, height = 1280, 720 #Make sure background image is same size
SCREEN = pygame.display.set_mode(SIZE)
pygame.display.set_caption("Cube")
#Events
done = False
menu_on = True
#Colors
BLACK = 0, 0, 0
WHITE = 255, 255, 255
#Fonts
FONT = pygame.font.SysFont("Trebuchet MS", 25)
MENU_FONT = (FONT)
#Info
time = 0
minute = 0
hour = 0
day = 0
year = 0
counter = 0
blink_clock = 0
blink = 0
#Year
YEARFONT = FONT.render("Year:{0:03}".format(year),1, BLACK) #zero-pad day to 3 digits
YEARFONTR=YEARFONT.get_rect()
YEARFONTR.center=(885, 20)
#Day
DAYFONT = FONT.render("Day:{0:03}".format(day),1, BLACK) #zero-pad day to 3 digits
DAYFONTR=DAYFONT.get_rect()
DAYFONTR.center=(985, 20)
#Hour
HOURFONT = FONT.render("Hour:{0:02}".format(hour),1, BLACK) #zero-pad hours to 2 digits
HOURFONTR=HOURFONT.get_rect()
HOURFONTR.center=(1085, 20)
#Minute
MINUTEFONT = FONT.render("Minute:{0:02}".format(minute),1, BLACK) #zero-pad minutes to 2 digits
MINUTEFONTR=MINUTEFONT.get_rect()
MINUTEFONTR.center=(1200, 20)
#Characters
def load_image(cube):
image = pygame.image.load(cube)
return image
class Menu:
hovered = False
def __init__(self, text, pos):
self.text = text
self.pos = pos
self.set_rect()
self.draw()
def draw(self):
self.set_rend()
SCREEN.blit(self.rend, self.rect)
def set_rend(self):
self.rend = MENU_FONT.render(self.text, 1, self.get_color())
def get_color(self):
if self.hovered:
return (255, 255, 255)
else:
return (100, 100, 100)
def set_rect(self):
self.set_rend()
self.rect = self.rend.get_rect()
self.rect.topleft = self.pos
class Cube(pygame.sprite.Sprite):
def __init__(self):
super(Cube, self).__init__()
self.images = []
self.images.append(load_image('Fine.png'))
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(440, 180, 74, 160)
def update(self):
self.index += 1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
class Blink(pygame.sprite.Sprite):
def __init__(self):
super(Blink, self).__init__()
self.images = []
self.images.append(load_image('Blink.png'))
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(440, 180, 74, 160)
def update(self):
self.index += 1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
class Blank(pygame.sprite.Sprite):
def __init__(self):
super(Blank, self).__init__()
self.images = []
self.images.append(load_image('Blank.png'))
self.index = 0
self.image = self.images[self.index]
self.rect = pygame.Rect(440, 180, 74, 160)
def update(self):
self.index += 1
if self.index >= len(self.images):
self.index = 0
self.image = self.images[self.index]
allsprites = Cube()
group = pygame.sprite.Group(allsprites)
blink = Blink()
blinking = pygame.sprite.Group(blink)
blankcube = Blank()
blankgroup = pygame.sprite.Group(blankcube)
start_game = [Menu("START GAME", (140, 105))]
help_ = [Menu("HELP", (140, 155))]
quit_ = [Menu("QUIT", (140, 205))]
#Game Speed
clock = pygame.time.Clock()
FPS = 60
CLOCKTICK = pygame.USEREVENT+1
pygame.time.set_timer(CLOCKTICK, 1000)
game_start = False
SCREEN.fill(WHITE)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if menu_on == True:
for Menu in help_:
if Menu.rect.collidepoint(pygame.mouse.get_pos()):
Menu.hovered = True
else:
Menu.hovered = False
Menu.draw()
for Menu in quit_:
if Menu.rect.collidepoint(pygame.mouse.get_pos()):
Menu.hovered = True
if event.type == pygame.MOUSEBUTTONDOWN:
done = True
else:
Menu.hovered = False
Menu.draw()
for Menu in start_game:
if Menu.rect.collidepoint(pygame.mouse.get_pos()):
Menu.hovered = True
if event.type == pygame.MOUSEBUTTONDOWN:
game_start = True
else:
Menu.hovered = False
Menu.draw()
group.update()
group.draw(SCREEN)
if event.type == CLOCKTICK:
blink_clock=blink_clock + 1
if blink_clock == 60:
blink_clock = 0
if blink_clock == 0:
blink = random.randint(0, 1)
if blink == 1:
blinking.update()
blinking.draw(SCREEN)
if blink_clock == 41:
blink = 0
blankgroup.update()
blankgroup.draw(SCREEN)
if game_start == True:
menu_on = False
if event.type == CLOCKTICK:
minute = minute + 1
if minute == 60:
hour = hour + 1
minute = 0
if hour == 24:
day = day + 1
hour = 0
if day == 365:
year = year + 1
day = 0
SCREEN.fill(WHITE)
MINUTEFONT = FONT.render("Minute:{0:02}".format(minute), 1, BLACK)
SCREEN.blit(MINUTEFONT, MINUTEFONTR)
HOURFONT = FONT.render("Hour:{0:02}".format(hour), 1, BLACK)
SCREEN.blit(HOURFONT, HOURFONTR)
DAYFONT = FONT.render("Day:{0:03}".format(day), 1, BLACK)
SCREEN.blit(DAYFONT, DAYFONTR)
YEARFONT = FONT.render("Year:{0:03}".format(year), 1, BLACK)
SCREEN.blit(YEARFONT, YEARFONTR)
group.update()
group.draw(SCREEN)
if event.type == CLOCKTICK:
blink_clock=blink_clock + 1
if blink_clock == 60:
blink_clock = 0
if blink_clock == 0:
blink = random.randint(0, 1)
if blink == 1:
blinking.update()
blinking.draw(SCREEN)
if blink_clock == 41:
blink = 0
blankgroup.update()
blankgroup.draw(SCREEN)
clock.tick(FPS)
pygame.display.flip()
pygame.quit()
感谢任何帮助,我很乐意看到您的反馈:)
答案 0 :(得分:0)
我现在发现了你的问题。问题出在游戏循环中。
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if menu_on == True:
**do stuff 1**
if game_start == True:
**do stuff 2**
在**做东西1 **你没有检查事件循环中的任何事件,所以它工作正常。但是在**做东西2 **你试图检查一个事件 CLOCKTICK ,你定义为每1000毫秒(每秒)发生一次。
事件变量将仅引用事件循环后的最后一个事件,这意味着如果发生任何事件(例如移动鼠标或按下按钮),它可能会发生检查该事件而不是 CLOCKTICK 。
此外,如果没有事件发生,事件变量将仍然成为上一个事件。这就是为什么你的时钟如此之快,而不是每一秒钟。
解决这个问题的方法是为每个州设置一个事件循环。
while not done:
if menu_on == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
** the rest of the code **
if game_start == True:
menu_on = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == CLOCKTICK: # Make sure this code is inside the event loop!
minute = minute + 1
if minute == 60:
hour = hour + 1
minute = 0
if hour == 24:
day = day + 1
hour = 0
if day == 365:
year = year + 1
day = 0
** the rest of the code **