Python 向pygame添加带有按钮的菜单
很长一段时间以来,我一直在尝试为我的游戏添加菜单,但它没有出现。有一个屏幕出现,但是它是空的,只有文字,没有按钮,所以无论我点击哪里,它都会带我到同一个页面。就像一个屏幕显示,但按钮没有出现,我不知道为什么。如何为选项添加按钮Python 向pygame添加带有按钮的菜单,python,pygame,character,Python,Pygame,Character,很长一段时间以来,我一直在尝试为我的游戏添加菜单,但它没有出现。有一个屏幕出现,但是它是空的,只有文字,没有按钮,所以无论我点击哪里,它都会带我到同一个页面。就像一个屏幕显示,但按钮没有出现,我不知道为什么。如何为选项添加按钮 background = pygame.image.load('background.png') backgroundX = 0 backgroundX2 = background.get_width() homeScreen = pygame.image.load('h
background = pygame.image.load('background.png')
backgroundX = 0
backgroundX2 = background.get_width()
homeScreen = pygame.image.load('home_screen.png')
obstacle = pygame.image.load('obstacle.png')
obstacleX = 0
obstacleX2 = obstacle.get_width()
instructions = pygame.image.load('instructions.png')
# frame rate
clock = pygame.time.Clock()
# use procedure for game window rather than using it within loop
def redrawGameWindow():
# background images for right to left moving screen
screen.blit(background, (backgroundX, 0))
screen.blit(background, (backgroundX2, 0))
man.draw(screen)
screen.blit(obstacle, (obstacleX, 400))
screen.blit(obstacle, (obstacleX2, 400))
pygame.display.flip()
pygame.display.update()
# create class for character (object)
class player(object):
def __init__(self, x, y, width, height): # initialize attributes
self.x = x
self.y = y
self.width = width
self.height = height
self.left = True
self.right = True
self.isJump = False
self.stepCount = 0
self.jumpCount = 10
self.standing = True
def draw(self, screen):
if self.stepCount + 1 >= 27: # 9 sprites, with 3 frames - above 27 goes out of range
self.stepCount = 0
if not self.standing:
if self.left:
screen.blit(leftDirection[self.stepCount // 5], (self.x, self.y), )
self.stepCount += 1
elif self.right:
screen.blit(rightDirection[self.stepCount // 5], (self.x, self.y), )
self.stepCount += 1
else:
if self.right:
screen.blit(rightDirection[0], (self.x, self.y)) # using index, include right faced photo
else:
screen.blit(leftDirection[0], (self.x, self.y))
class enlargement(object):
def __init__(self, x, y, radius, color, facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
def draw(self, screen):
pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius, 1)
man = player(200, 313, 64, 64)
font = pygame.font.Font(None, 75) # font for home screen
instructionsFont = pygame.font.Font(None, 30) # font for instructions page
# HOME SCREEN
font = pygame.font.Font(None, 75) # font for home screen
instructionsFont = pygame.font.Font(None, 30) # font for instructions page
display_instructions = True
homePage = 1
play_page = 2
done = False
while not done and display_instructions:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.MOUSEBUTTONDOWN:
homePage += 1
if homePage == 3:
display_instructions = False
# background
screen.blit(homeScreen, (0, 0))
if homePage == 1:
text = font.render("Star Keeper", True, white)
screen.blit(text, [115, 40])
text = font.render("Home", True, white)
screen.blit(text, [180, 130])
text = font.render("Instructions", True, white)
screen.blit(text, [100, 220])
text = font.render("Play", True, white)
screen.blit(text, [200, 300])
if homePage == 2:
# Draw instructions, page 2
text1 = instructionsFont.render("Welcome to Star Keeper", True, white)
text2 = instructionsFont.render("The objective of the game", True, white)
text3 = instructionsFont.render("is to jump over obstacles using ", True, white)
text4 = instructionsFont.render("the space key. Hitting an obstacle", True, white)
text5 = instructionsFont.render("will cause you to lose. Good luck!", True, white)
screen.blit(text1, [130, 50])
screen.blit(text2, [130, 100])
screen.blit(text3, [93, 150])
screen.blit(text4, [93, 200])
screen.blit(text5, [93, 250])
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
pygame.display.update()
# main loop
speed = 30 # NEW
man = player(200, 410, 64, 64) # set main character attributes
run = True
while run:
screen.fill(white)
clock.tick(30)
pygame.display.update()
redrawGameWindow() # call procedure
clock.tick(speed) # NEW
backgroundX -= 1.4 # Move both background images back
backgroundX2 -= 1.4
obstacleX -= 1.4
obstacleX2 -= 1.4
if backgroundX < background.get_width() * -1: # If our background is at the -width then reset its position
backgroundX = background.get_width()
if backgroundX2 < background.get_width() * -1:
backgroundX2 = background.get_width()
if obstacleX < obstacle.get_width() * -10:
obstacleX = obstacle.get_width
if obstacleX2 < obstacle.get_width() * -10:
obstacleX2 = obstacle.get_width()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
quit()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
man.left = True
man.right = False
man.standing = False # false, because man is walking
# verify that character is within window parameters
elif keys[pygame.K_RIGHT]:
man.right = True
man.left = False
man.standing = False # false, because man is walking
else:
man.standing = True
man.stepCount = 0
if not man.isJump:
if keys[pygame.K_SPACE]:
man.isJump = True # when jumping, man shouldn't move directly left or right
man.right = False
man.left = False
man.stepCount = 0
else:
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * .5 * neg # to jump use parabola
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
pygame.quit()
background=pygame.image.load('background.png')
背景x=0
backgroundX2=background.get_width()
homeScreen=pygame.image.load('home\u screen.png'))
障碍=pygame.image.load('barrier.png')
obstacleX=0
obstacleX2=障碍物。获取_宽度()
instructions=pygame.image.load('instructions.png')
#帧速率
clock=pygame.time.clock()
#使用游戏窗口的过程,而不是在循环中使用它
def重画游戏窗口():
#从右向左移动屏幕的背景图像
屏幕blit(背景(背景x,0))
屏幕blit(背景(背景x2,0))
人.画(屏幕)
屏幕blit(障碍物,(障碍物,400))
屏幕blit(障碍物,(障碍物2400))
pygame.display.flip()
pygame.display.update()
#为角色(对象)创建类
类播放器(对象):
定义初始化(self、x、y、width、height):#初始化属性
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.left=True
self.right=True
self.isJump=False
self.stepCount=0
self.jumpCount=10
自立
def提取(自身,屏幕):
如果self.stepCount+1>=27:#9个精灵,3帧-大于27超出范围
self.stepCount=0
如果不是自立的:
如果self.left:
blit(leftDirection[self.stepCount//5],(self.x,self.y),)
self.stepCount+=1
elif self.right:
blit(rightDirection[self.stepCount//5],(self.x,self.y),)
self.stepCount+=1
其他:
如果你是对的:
screen.blit(righdirection[0],(self.x,self.y))#使用索引,包括右面照片
其他:
blit(左方向[0],(self.x,self.y))
类放大(对象):
定义初始(自、x、y、半径、颜色、面):
self.x=x
self.y=y
自半径=半径
self.color=颜色
自我面对
def提取(自身,屏幕):
pygame.draw.circle(屏幕,self.color,(self.x,self.y),self.radius,1)
男子=运动员(200、313、64、64)
font=pygame.font.font(无,75)#主屏幕字体
说明font=pygame.font.font(无,30)#说明页的字体
#主屏幕
font=pygame.font.font(无,75)#主屏幕字体
说明font=pygame.font.font(无,30)#说明页的字体
显示指令=真
首页=1
播放页面=2
完成=错误
未完成时,显示\u说明:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
如果event.type==pygame.MOUSEBUTTONDOWN:
首页+=1
如果主页==3:
显示\u指令=错误
#背景
屏幕blit(主屏幕,(0,0))
如果主页==1:
text=font.render(“星星守护者”,真,白色)
blit(文本,[115,40])
text=font.render(“主”,真,白色)
blit(文本,[180130])
text=font.render(“说明”,真,白色)
blit(文本,[100220])
text=font.render(“播放”,真,白色)
blit(文本,[200300])
如果主页==2:
#绘图说明,第2页
text1=instructionsFont.render(“欢迎来到星际守护者”,真,白色)
text2=instructionsFont.render(“游戏的目标”,真,白色)
text3=instructionsFont.render(“是使用”,True,white跳过障碍物)
text4=instructionsFont.render(“空格键。遇到障碍物”,真,白色)
text5=instructionsFont.render(“将导致您失败。祝您好运!”,真,白色)
screen.blit(text1[130,50])
screen.blit(text2[130100])
screen.blit(text3[93150])
screen.blit(text4[93200])
screen.blit(text5[93250])
#限制为每秒60帧
时钟滴答(60)
#继续,用我们绘制的内容更新屏幕。
pygame.display.flip()
pygame.display.update()
#主回路
速度=30#新
男=玩家(200410,64,64)#设置主要角色属性
运行=真
运行时:
屏幕填充(白色)
时钟滴答(30)
pygame.display.update()
重画GameWindow()#调用过程
时钟。滴答声(速度)#新
backgroundX-=1.4#将两幅背景图像向后移动
背景x2-=1.4
obstacleX-=1.4
障碍2-=1.4
如果backgroundX#!/usr/bin/env python3
import pygame
# --- constants --- (UPPER_CASE_NAMES)
WHITE = (255,255,255)
BLACK = ( 0, 0, 0)
RED = (255, 0, 0)
GREEN = ( 0,255, 0)
BLUE = ( 0, 0,255)
YELLOW = (255,255, 0)
# --- classes --- (CamelCaseNanes)
# empty
# --- functions --- (lower_case_names_
def button_create(text, rect, inactive_color, active_color, action):
font = pygame.font.Font(None, 40)
button_rect = pygame.Rect(rect)
text = font.render(text, True, BLACK)
text_rect = text.get_rect(center=button_rect.center)
return [text, text_rect, button_rect, inactive_color, active_color, action, False]
def button_check(info, event):
text, text_rect, rect, inactive_color, active_color, action, hover = info
if event.type == pygame.MOUSEMOTION:
# hover = True/False
info[-1] = rect.collidepoint(event.pos)
elif event.type == pygame.MOUSEBUTTONDOWN:
if hover and action:
action()
def button_draw(screen, info):
text, text_rect, rect, inactive_color, active_color, action, hover = info
if hover:
color = active_color
else:
color = inactive_color
pygame.draw.rect(screen, color, rect)
screen.blit(text, text_rect)
# ---
def on_click_button_1():
global stage
stage = 'game'
print('You clicked Button 1')
def on_click_button_2():
global stage
stage = 'options'
print('You clicked Button 2')
def on_click_button_3():
global stage
global running
stage = 'exit'
running = False
print('You clicked Button 3')
def on_click_button_return():
global stage
stage = 'menu'
print('You clicked Button Return')
# --- main --- (lower_case_names)
# - init -
pygame.init()
screen = pygame.display.set_mode((800,600))
screen_rect = screen.get_rect()
# - objects -
stage = 'menu'
button_1 = button_create("GAME", (300, 100, 200, 75), RED, GREEN, on_click_button_1)
button_2 = button_create("OPTIONS", (300, 200, 200, 75), RED, GREEN, on_click_button_2)
button_3 = button_create("EXIT", (300, 300, 200, 75), RED, GREEN, on_click_button_3)
button_return = button_create("RETURN", (300, 400, 200, 75), RED, GREEN, on_click_button_return)
# - mainloop -
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if stage == 'menu':
button_check(button_1, event)
button_check(button_2, event)
button_check(button_3, event)
elif stage == 'game':
button_check(button_return, event)
elif stage == 'options':
button_check(button_return, event)
#elif stage == 'exit':
# pass
# - draws -
screen.fill(BLACK)
if stage == 'menu':
button_draw(screen, button_1)
button_draw(screen, button_2)
button_draw(screen, button_3)
elif stage == 'game':
button_draw(screen, button_return)
elif stage == 'options':
button_draw(screen, button_return)
#elif stage == 'exit':
# pass
pygame.display.update()
# - end -
pygame.quit()