如何移动Pygame表面?

时间:2018-05-25 05:36:02

标签: python pygame

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()

“按钮单击”功能通过检查鼠标位置是否与按钮rect重叠来工作。我可以在Button.view()方法中重新定位矩形的blitted视图,但实际的矩形不随其移动,这使得单击功能在窗口中的错误位置触发。有没有办法将实际的按钮矩形碰撞与它的blitted视图一起移动?

提前致谢。

1 个答案:

答案 0 :(得分:1)

我用create方法替换__init__方法,其中创建了背景曲面,文本曲面及其对应的rects。这允许您在实例化期间传递所有需要的参数:

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

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

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

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

view方法的唯一目的是在它们的面上blit表面。

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.