【问题标题】:I am trying to make a pygame and can't link a command to a button我正在尝试制作一个 pygame,但无法将命令链接到按钮
【发布时间】:2018-01-02 16:19:05
【问题描述】:

我正在尝试制作一个带有按钮的 GUI,当玩家将鼠标悬停在按钮上时会更改按钮,并将它们引导到游戏的该部分而不打开新窗口。我在下面包含了我的代码。

import pygame, sys, tkinter
from pygame.locals import *

#Initialize pygame and define colours
pygame.init()
white = 255, 255, 255

#Sets the resolution to 640 pixels by 720 pixels and caption for pygame window
DISPLAY_SURF = pygame.display.set_mode((640, 720))
pygame.display.set_caption("The Hunt!")



#Create a clock object
clock = pygame.time.Clock()
FPS = 60

#Define a variable to refer to image
background = pygame.image.load("Startermenu.png")
start = pygame.image.load("PlayGameButton.png")
help = pygame.image.load("HelpButton.png")
credits = pygame.image.load("ShowCreditsButton.png")


#Start main loop
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    DISPLAY_SURF.fill(white)
    DISPLAY_SURF.blit(background,(0,0))
    DISPLAY_SURF.blit(start, (0, 140))
    DISPLAY_SURF.blit(help, (0, 186))
    DISPLAY_SURF.blit(credits, (0, 235))
    pygame.display.update()

【问题讨论】:

  • 你当前的代码是做什么的?
  • 我不太确定我是否理解您要执行的操作,但我可能会先捕获鼠标移动事件,然后将鼠标坐标与按钮图形定义的矩形进行比较。每次鼠标在按钮区域内移动时,都会调用相应的函数。
  • 这和 tkinter 有什么关系?你确定你的代码在这里是正确的吗?
  • 我正在尝试设计一个 GUI,所以我做了一些研究,发现 tkinter 是 python GUI 工具包。我的代码应该打开一个窗口,其中的启动菜单设计为 blited 到表面,并带有 3 个单独的按钮 blited 但链接到 pygame 的一部分
  • 我真的不明白你在做什么......你为什么要使用 tkinter 和 pygame?

标签: python user-interface button pygame


【解决方案1】:

你需要一个按钮吗?

这是一个按钮类:

class Button(object):
    def __init__(self,x,y,width,height,text_color,background_color,text):
        self.rect=pygame.Rect(x,y,width,height)
        self.image=pygame.draw.rect(screen, background_color,(self.rect),)
        self.x=x
        self.y=y
        self.width=width
        self.height=height
        self.text=text
        self.text_color=text_color

    def check(self):
        #will return if mouse is inside button
        return self.rect.collidepoint(pygame.mouse.get_pos())

    def draw(self):
        #draw button and border 
        drawText(self.text,font,screen,self.x+self.width/2,self.y+self.height/2,self.text_color)                          
        pygame.draw.rect(screen,self.text_color,self.rect,3) 

将此实施到您的主循环中:

#create a button instance
button=Button(parameters)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if elif event.type == pygame.MOUSEBUTTONDOWN:
            if start_button.check()==True:
                #what you to do when the user clicks button

    DISPLAY_SURF.fill(white)
    DISPLAY_SURF.blit(background,(0,0))
    DISPLAY_SURF.blit(start, (0, 140))
    DISPLAY_SURF.blit(help, (0, 186))
    DISPLAY_SURF.blit(credits, (0, 235))

    #draw button
    button.draw()

    pygame.display.update()

【讨论】:

  • 我正在尝试将 PNG 转换为要使用的按钮,当鼠标悬停在它上面时会改变颜色。我知道我可以做到这一点,以便PNG轻松更改为不同颜色的PNG,但我遇到的主要问题是鼠标检测并且它会进入游戏的另一部分。不过,您的代码帮了很多忙,谢谢
  • 谢谢。该网站帮助我取得了很大进步
猜你喜欢
  • 2019-08-31
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 2020-04-22
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多