【问题标题】:Pygame drawing a rectangle with OOPPygame用OOP画一个矩形
【发布时间】:2017-01-04 02:54:08
【问题描述】:

我正在尝试使用 pygame 学习 OOP 并制作一个简单的游戏,我正在松散地遵循一个教程,但尝试对其进行修改以满足我自己的需求,但现在它无法正常工作。我正在尝试在黑色窗口上绘制一个白色矩形,本教程在黑色窗口上绘制一个蓝色圆圈,当我将圆圈替换为矩形时,它不起作用。 我的代码被分成 2 个不同的文件,这是第一个文件:

import pygame
import LanderHandler

black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)


class MainLoop(object):
    def __init__(self, width=640, height=400):

        pygame.init()
        pygame.display.set_caption("Lander Game")
        self.width = width
        self.height = height
        self.screen = pygame.display.set_mode((self.width, self.height), pygame.DOUBLEBUF)
        self.background = pygame.Surface(self.screen.get_size()).convert()

    def paint(self):
        lander = LanderHandler.Lander()
        lander.blit(self.background)

    def run(self):

        self.paint()
        running = True

        while running:

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        running = False

            pygame.display.flip()

        pygame.quit()


if __name__ == '__main__':
    # call with width of window and fps
    MainLoop().run()

还有我的第二个文件:

import pygame

black = (0, 0, 0)
white = (255, 255, 255)
green = (0, 255, 0)
red = (255, 0, 0)


class Lander(object):
    def __init__(self, height=10, width=10, color=white, x=320, y=240):
        self.x = x
        self.y = y
        self.height = height
        self.width = width
        self.surface = pygame.Surface((2 * self.height, 2 * self.width))
        self.color = color

        pygame.draw.rect(self.surface, white, (self.height, self.height, self.width, self.width))

    def blit(self, background):
        """blit the Ball on the background"""
        background.blit(self.surface, (self.x, self.y))

    def move(self, change_x, change_y):
        self.change_x = change_x
        self.change_y = change_y

        self.x += self.change_x
        self.y += self.change_y

        if self.x > 300 or self.x < 0:
            self.change_x = -self.change_x
        if self.y > 300 or self.y < 0:
            self.change_y = -self.change_y

任何帮助或指出我正确的方向都会很棒,谢谢。 附言我没有遇到任何运行错误,并且确实会弹出一个黑色窗口,但没有白色矩形。

【问题讨论】:

  • while running: 是代码的主要部分,您不会在此循环中绘制任何内容,因此屏幕上没有任何内容。

标签: python pygame


【解决方案1】:

问题是因为您在表面上绘制矩形self.background

lander.blit(self.background)

但你永远不会在 self.screen 上对 self.background 进行 blit,这是主缓冲区,当你这样做时会在监视器上发送

pygame.display.flip()

所以你可以直接在self.screen上画画

lander.blit(self.screen)

或者你必须在self.screen上blit self.background

lander.blit(self.background)

self.screen.blit(self.background, (0,0))

【讨论】:

    【解决方案2】:

    您不应该创建一个名为 blit 的函数,因为它可能会妨碍实际的 blit 函数。同样在第二个代码中:

    pygame.draw.rect(self.surface, white, (self.height, self.height, self.width, self.width))
    

    你应该使用表面

    【讨论】:

    • 我不关注...你是什么意思,使用表面?
    猜你喜欢
    • 1970-01-01
    • 2017-03-21
    • 2014-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多