#Importing Modules
import pygame
import sys
import random
#All pygame stuff under here
pygame.init()
#Font definitions
backFont = pygame.font.SysFont("monospace",40)
titleFont = pygame.font.SysFont("garamond", 100)
buttonFont = pygame.font.SysFont("garamond", 25)
bigFont = pygame.font.SysFont("garamond",100)
#Colour definitions
BackGray = pygame.Color('gray60')
screenGray = pygame.Color('gray80')
buttonGray1 = pygame.Color('gray40')
buttonGray2 = pygame.Color('gray50')
buttonGray3 = pygame.Color('gray30')
textColour = pygame.Color('navy')
#Screen size set
screen = pygame.display.set_mode((800, 600))
#Class for the buttons set here
class Button:
def __init__(self, x, y, width, height, colour, surface):
self.x = x
self.y = y
self.height = height
self.width = width
self.colour = colour
self.surface = surface
def isPressed(self):
mouse_position = pygame.mouse.get_pos()
mouse_x = mouse_position[0]
mouse_y = mouse_position[1]
if mouse_x > self.x:
if mouse_x < self.x + self.width:
if mouse_y > self.y:
if mouse_y < self.y + self.height:
mouse_click = pygame.mouse.get_pressed()
left_click = mouse_click[0]
if left_click:
self.colour = (0,0,0)
return True
self.colour = (230, 230, 230)
return False
def drawButton(self):
pygame.draw.rect(self.surface, self.colour, (self.x, self.y, self.width, self.height))
pygame.display.flip()
def FrontPage():
#Back screen
screen.fill(screenGray)
#The background is defined here
BinaryPage = []
for i in range(0,15):
BinaryString = ""
for j in range(0,33):
BinaryString += random.choice(["0","1"])
BinaryPage.append(BinaryString)
for i in range(0,15):
screen.blit(backFont.render(BinaryPage[i], 1, BackGray), (0,i * 40))
#The title is defined and printed here
Title1 = titleFont.render("The Six", 10, textColour)
Title2 = titleFont.render("Cipher", 10, textColour)
Title3 = titleFont.render("Simulator", 10, textColour)
screen.blit(Title1, (100, 100))
screen.blit(Title2, (100, 200))
screen.blit(Title3, (100, 300))
#Text for the button
buttonText = buttonFont.render("Continue", 10, textColour)
screen.blit(buttonText, (115,405))
#Where the buttons are defined and drawn
ButtonBig = Button(100, 450, 120, 60, buttonGray1, screen)
ButtonSmall = Button(105, 455, 110, 50, buttonGray2, screen)
ButtonBig.drawButton()
ButtonSmall.drawButton()
#Pygame While loop
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if ButtonBig.isPressed():
print("Hello")
break
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
clock.tick(60)
< >上面的代码运行一个窗口,它有0和1的随机顺序作为背景,中间的标题是我的程序。你知道吗然后,底部应该有一个按钮,或者一个灰色的矩形。然而,当我试图.blit,或打印,一些东西到按钮框,当我把按钮画到屏幕上,它不会出现。你知道吗
有人能告诉我为什么和怎么修吗? 或任何其他方法如何将文本输出到按钮上。你知道吗
提前谢谢
我首先创建一个单独的
background
曲面,并将所有永远不会改变的背景图形blit到它上面(下面示例中的数字)。在主循环中,你只需点击这个背景来清除屏幕。你知道吗要将文本blit到按钮上,请执行以下操作:
__init__
方法对于按钮,我将使用sprites并将它们放入一个sprite组,这样您就可以通过调用} 和^{} 属性才能工作。Pygame rect有冲突检测方法,您应该使用这些方法,而不是编写自己的方法。在本例中,我们可以使用
sprite_group.draw(screen)
来简单地绘制它们。精灵需要^{collidepoint
方法并将event.pos
传递给它。你知道吗相关问题 更多 >
编程相关推荐