我在调用 Button 类中的 draw_pause_button 方法时遇到问题。我做错了什么?

问题描述 投票:0回答:0

我正在尝试构建一个暂停按钮,希望能更接近我的最终目标,即为我构建的宇宙飞船决斗游戏构建菜单。但是,我在尝试调用负责绘制矩形并将按钮的文本传输到矩形上的 draw_pause_button 方法时遇到了困难。

程序能够运行,但是当我按回车键(这应该会导致按钮弹出)时,出现以下错误:

回溯(最近一次调用最后一次): 文件“/Users/yazdanhariri/PycharmProjects/Pygame/main.py”,第 223 行,位于 主功能() 文件“/Users/yazdanhariri/PycharmProjects/Pygame/main.py”,第 186 行,在 main_function 中 暂停按钮.draw_pause_button() 文件“/Users/yazdanhariri/PycharmProjects/Pygame/main.py”,第68行,在draw_pause_button中 按钮矩形 = pygame. 矩形 (self.x_pos, self.y_pos), (150, 25) 类型错误:参数必须是矩形样式对象

这是 Button 类(其中包括 draw_pause_button 方法):

class Button:
    def __init__(self, text, button, x_pos, y_pos, enabled=True):
        self.text = text
        self.button = button
        self.enabled = enabled
        self.x_pos = x_pos
        self.y_pos = y_pos

    def draw_pause_button(self):
        button_text = PAUSE_BUTTON_FONT.render(self.text, True, FONT_COLOR)
        button_rect = pygame.Rect(self.x_pos, self.y_pos), (150, 25)
        pygame.draw.rect(WINDOW, (0, 0, 0), button_rect, 0, 5)
        WINDOW.blit(button_text, self.x_pos + 3, self.y_pos + 3)

这是主函数的一部分,我尝试通过检测是否按下了某个键来调用draw_pause_button方法,然后调用该方法:

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q and len(yellow_bullets) < MAX_BULLETS:
                    bullet = pygame.Rect(
                        yellow.x + SPACESHIP_WIDTH, yellow.y + SPACESHIP_HEIGHT / 2 - BULLET_HEIGHT / 2, BULLET_WIDTH,
                        BULLET_HEIGHT)
                    yellow_bullets.append(bullet)

                if event.key == pygame.K_RSHIFT and len(red_bullets) < MAX_BULLETS:
                    bullet = pygame.Rect(
                        red.x, red.y + SPACESHIP_HEIGHT / 2 - 2, BULLET_WIDTH, BULLET_HEIGHT)
                    red_bullets.append(bullet)

                **if event.key == pygame.K_RETURN:
                    pause_button.draw_pause_button()

这是我的整个代码:

import pygame
import os

pygame.font.init()
pygame.mixer.init()

WINDOW_SIZE = (900, 600)
WINDOW = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption("Spaceship Duels")

WINDOW_COLOR = (0, 0, 255)

BORDER = pygame.Rect(900 / 2 - 10 / 2, 0, 10, 900)

pause = False

PAUSE_BUTTON_FONT = pygame.font.SysFont('arialblack', 50)
HEALTH_FONT = pygame.font.SysFont('arialblack', 30)
WINNER_FONT = pygame.font.SysFont('javanesetext', 80)
FONT_COLOR = (248, 248, 255)

BORDER_COLOR = (0, 0, 0)

RED_BULLET_COLOR = (248, 248, 255)

YELLOW_BULLET_COLOR = (248, 248, 255)

FPS = 60

SPACESHIP_VELOCITY = 5

BULLET_VELOCITY = 8

MAX_BULLETS = 3

BULLET_WIDTH, BULLET_HEIGHT = 10, 5

SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40

YELLOW_SHOT = pygame.USEREVENT + 1
RED_SHOT = pygame.USEREVENT + 2
PAUSED = pygame.USEREVENT + 3

YELLOW_SPACESHIP_IMAGE = pygame.image.load(os.path.join(
    'Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(
    pygame.transform.scale(YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)

RED_SPACESHIP_IMAGE = pygame.image.load(os.path.join(
    'Assets', 'spaceship_red.png'))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
    RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)

SPACE = pygame.transform.scale(pygame.image.load(
    os.path.join('Assets', 'Space.jpeg')), (WINDOW_SIZE[0], WINDOW_SIZE[1]))


class Button:
    def __init__(self, text, button, x_pos, y_pos, enabled=True):
        self.text = text
        self.button = button
        self.enabled = enabled
        self.x_pos = x_pos
        self.y_pos = y_pos

    def draw_pause_button(self):
        button_text = PAUSE_BUTTON_FONT.render(self.text, True, FONT_COLOR)
        button_rect = pygame.Rect(self.x_pos, self.y_pos), (150, 25)
        pygame.draw.rect(WINDOW, (0, 0, 0), button_rect, 0, 5)
        WINDOW.blit(button_text, self.x_pos + 3, self.y_pos + 3)


def draw_window(red, yellow, yellow_bullets, red_bullets, red_health, yellow_health):
    WINDOW.blit(SPACE, (0, 0))
    pygame.draw.rect(WINDOW, BORDER_COLOR, BORDER)
    red_health_text = HEALTH_FONT.render(
        "Health: " + str(red_health), 1, FONT_COLOR)
    yellow_health_text = HEALTH_FONT.render(
        "Health: " + str(yellow_health), 1, FONT_COLOR)
    WINDOW.blit(red_health_text, (WINDOW_SIZE[0] - red_health_text.get_width() - 10, 10))
    WINDOW.blit(yellow_health_text, (10, 10))

    WINDOW.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
    WINDOW.blit(RED_SPACESHIP, (red.x, red.y))

    for bullet in red_bullets:
        pygame.draw.rect(WINDOW, RED_BULLET_COLOR, bullet)

    for bullet in yellow_bullets:
        pygame.draw.rect(WINDOW, YELLOW_BULLET_COLOR, bullet)

    pygame.display.update()


def yellow_spaceship_movement(keys_pressed, yellow):
    if keys_pressed[pygame.K_a] and yellow.x - SPACESHIP_VELOCITY > 0:  # left
        yellow.x = yellow.x + (-SPACESHIP_VELOCITY)
    if keys_pressed[pygame.K_d] and yellow.x + SPACESHIP_VELOCITY + SPACESHIP_WIDTH / 1.5 < BORDER.x:  # right
        yellow.x = yellow.x + SPACESHIP_VELOCITY
    if keys_pressed[pygame.K_w] and yellow.y - SPACESHIP_VELOCITY > 0:  # up
        yellow.y = yellow.y + (-SPACESHIP_VELOCITY)
    if keys_pressed[pygame.K_s] and yellow.y + SPACESHIP_VELOCITY + SPACESHIP_HEIGHT < WINDOW_SIZE[1] - 15:  # down
        yellow.y = yellow.y + SPACESHIP_VELOCITY


def red_spaceship_movement(keys_pressed, red):
    if keys_pressed[pygame.K_LEFT] and red.x - SPACESHIP_VELOCITY > BORDER.x + BORDER.width:
        red.x = red.x + (-SPACESHIP_VELOCITY)
    if keys_pressed[pygame.K_RIGHT] and red.x + SPACESHIP_VELOCITY + SPACESHIP_WIDTH / 1.5 < WINDOW_SIZE[0]:
        red.x = red.x + SPACESHIP_VELOCITY
    if keys_pressed[pygame.K_UP] and red.y - SPACESHIP_VELOCITY > 0:
        red.y = red.y + (-SPACESHIP_VELOCITY)
    if keys_pressed[pygame.K_DOWN] and red.y + SPACESHIP_VELOCITY + SPACESHIP_HEIGHT < WINDOW_SIZE[1] - 15:
        red.y = red.y + SPACESHIP_VELOCITY


def handle_bullets(yellow_bullets, red_bullets, yellow, red):
    for bullet in yellow_bullets:
        bullet.x = bullet.x + BULLET_VELOCITY
        if red.colliderect(bullet):
            pygame.event.post(pygame.event.Event(RED_SHOT))
            yellow_bullets.remove(bullet)
        elif bullet.x > WINDOW_SIZE[0]:
            yellow_bullets.remove(bullet)

    for bullet in red_bullets:
        bullet.x = bullet.x + (-BULLET_VELOCITY)
        if yellow.colliderect(bullet):
            pygame.event.post(pygame.event.Event(YELLOW_SHOT))
            red_bullets.remove(bullet)
        elif bullet.x < 0:
            red_bullets.remove(bullet)


def make_pause_true():
    global pause
    pause = True


def draw_winner(text):
    draw_text = WINNER_FONT.render(text, 1, FONT_COLOR)
    WINDOW.blit(draw_text, (WINDOW_SIZE[0] / 2 - draw_text.get_width() /
                            2, WINDOW_SIZE[1] / 2 - draw_text.get_height() / 2))
    pygame.display.update()
    pygame.time.delay(50000)


def main_function():
    red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
    yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
    pause_button = Button("click me", 10, 10, True)

    red_bullets = []
    yellow_bullets = []

    red_health = 10
    yellow_health = 10

    clock = pygame.time.Clock()
    run = True
    while run:
        clock.tick(FPS)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q and len(yellow_bullets) < MAX_BULLETS:
                    bullet = pygame.Rect(
                        yellow.x + SPACESHIP_WIDTH, yellow.y + SPACESHIP_HEIGHT / 2 - BULLET_HEIGHT / 2, BULLET_WIDTH,
                        BULLET_HEIGHT)
                    yellow_bullets.append(bullet)

                if event.key == pygame.K_RSHIFT and len(red_bullets) < MAX_BULLETS:
                    bullet = pygame.Rect(
                        red.x, red.y + SPACESHIP_HEIGHT / 2 - 2, BULLET_WIDTH, BULLET_HEIGHT)
                    red_bullets.append(bullet)

                if event.key == pygame.K_RETURN:
                    pause_button.draw_pause_button()

            if event.type == RED_SHOT:
                red_health = red_health - 1

            if event.type == YELLOW_SHOT:
                yellow_health = yellow_health - 1

        winner_text = ""
        if red_health <= 0:
            winner_text = "Yellow Wins!"

        if yellow_health <= 0:
            winner_text = "Red Wins!"

        if winner_text != "":
            draw_winner(winner_text)
            break

        keys_pressed = pygame.key.get_pressed()

        yellow_spaceship_movement(keys_pressed, yellow)
        red_spaceship_movement(keys_pressed, red)

        handle_bullets(yellow_bullets, red_bullets, yellow, red)

        draw_window(red, yellow, red_bullets, yellow_bullets,
                    red_health, yellow_health)

        pygame.display.update()

        pygame.event.pump()

    pygame.quit()


if __name__ == "__main__":
    main_function()

我尝试完全删除该类并单独使用draw_pause_button函数来创建按钮,但是我也遇到了这样做的问题。我首先选择类的原因是因为我计划制作多个按钮,所以我认为类会让我的代码更加高效。请注意,我是编码和在堆栈交换上发布的初学者。我花了很多时间浏览类似的问题,但找不到对我有用的答案;因此,我决定继续发表这篇文章。如果问题非常重复,我提前表示歉意,但考虑到如果我没有用尽所有其他可能的帮助来源,我不会发布这个问题。如果存在任何格式问题,如果我被告知这些问题(或者通过评论或只是编辑帖子并修复它)。请随时向我询问比本文中提供的更多信息,我将尽力提供我能提供的信息。预先感谢您的宝贵时间。

class button methods pygame pygame-menu
© www.soinside.com 2019 - 2024. All rights reserved.