gpt4 book ai didi

python - 如何移动 Pygame 表面?

转载 作者:行者123 更新时间:2023-12-01 09:24:47 25 4
gpt4 key购买 nike

import pygame, sys, os.path

pygame.init()

# set up the colours
# R G B
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 50, 130, 255)

screen_surf = pygame.display.set_mode((500,500), 0, 24)
pygame.display.set_caption("Lewis' Menu")

screen_surf.fill((100,0,0))

class Button():
def create(self,w,h,colour):
self.button_surf = pygame.Surface((w,h), 0, 24)
self.button = pygame.draw.rect(self.button_surf, colour, (0, 0, w, h))
def view(self,text,x,y):
width = self.button_surf.get_width()
height = self.button_surf.get_height()
sys_font = pygame.font.SysFont(("None"), 25)
rendered = sys_font.render(text,0,(255,255,255))
self.button_surf.blit(rendered, ((width - 140),((height / 2) - 10)))
screen_surf.blit(self.button_surf, (x,y))

start_button = Button()
start_button.create(300,100,BLUE), start_button.view("Clicky Button!",10,10)
exit_button = Button()
exit_button.create(300,50,GREEN), exit_button.view("Exit!",10,200)
while True:
pos = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
if start_button.button.collidepoint(pos):
print("You opened a chest!")
if exit_button.button.collidepoint(pos):
pygame.quit()
sys.exit()

if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()

“按钮单击”功能的工作原理是检查鼠标位置是否与按钮矩形重叠。我可以在 Button.view() 方法中重新定位矩形的位图传输 View ,但实际的矩形不会随之移动,这使得单击功能在窗口中的错误位置触发。有什么方法可以移动实际的按钮矩形碰撞及其位图传输 View 吗?

提前致谢。

最佳答案

我会将 create 方法替换为 __init__ 方法,在该方法中创建背景表面、文本表面及其相应的矩形。这允许您在实例化期间传递所有需要的参数:

start_button = Button(10, 10, 300, 100, "Clicky Button!", BLUE)

使用参数将 self.button 矩形放置在所需的坐标处:

self.button = pygame.Rect(x, y, w, h)

您还可以使用get_rect按钮表面的方法来创建矩形(请参阅 text_rect 创建)。

view 方法的唯一目的是在其矩形处位 block 传输表面。

import sys
import pygame


pygame.init()

RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 50, 130, 255)

screen_surf = pygame.display.set_mode((500,500), 0, 24)
screen_surf.fill((100,0,0))
clock = pygame.time.Clock()
# No need to recreate the font object all the time.
SYS_FONT = pygame.font.SysFont(None, 25)


class Button():

def __init__(self, x, y, w, h, text, colour):
self.button_surf = pygame.Surface((w,h), 0, 24)
self.button_surf.fill(colour)
self.button = pygame.Rect(x, y, w, h)
self.text_surf = SYS_FONT.render(text, False, (255,255,255))
# Pass the center coords of the button rect to the newly
# created text_rect for the purpose of centering the text.
self.text_rect = self.text_surf.get_rect(center=self.button.center)

def view(self, screen_surf):
screen_surf.blit(self.button_surf, self.button)
screen_surf.blit(self.text_surf, self.text_rect)


# Button() calls the __init__ method.
start_button = Button(10, 10, 300, 100, "Clicky Button!", BLUE)
start_button.view(screen_surf) # Call `view` in a separate line.
exit_button = Button(10, 200, 300, 50, "Exit!", GREEN)
exit_button.view(screen_surf)

while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONUP:
if start_button.button.collidepoint(event.pos):
print("You opened a chest!")
elif exit_button.button.collidepoint(event.pos):
pygame.quit()
sys.exit()
elif event.type == pygame.QUIT:
pygame.quit()
sys.exit()

pygame.display.update()

clock.tick(60) # Limit the frame rate to 60 FPS.

关于python - 如何移动 Pygame 表面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50522161/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com