Pygame乒乓球拍不显示

时间:2018-12-16 19:03:11

标签: python pygame

我正在Pygame中开发乒乓球游戏。使桨在班级的屏幕上显示时遇到一些麻烦。我感到我的构造函数init方法不正确(尽管没有抛出任何错误),或者显示颜色覆盖了桨。

这是我的代码。

Program.py

import sys
import pygame

from settings import Settings
from paddles import Paddles
import game_functions as gf


def run_game():
    # Initialise pygame, settings and screen object.
    pygame.init()

    ai_settings = Settings()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption('Ping Pong')


    # Start the main loop for the game.
    while True:
        gf.check_events()
        gf.update_screen(ai_settings,screen)

    #Make paddles
    paddles = Paddles(screen)


run_game()

paddles.py

import pygame
import sys

class Paddles():

    def __init__(self, screen):
        self.screen = screen
        self.paddle_l = pygame.draw.rect(screen, (255, 255, 255), [15, 250, 10, 100])
        self.paddle_r = pygame.draw.rect(screen, (255, 255, 255), [780, 250, 10, 100])

    def paddles(self):
        pass

settings.py

class Settings():
    """A class to store all settings for Ping Pong"""

    def __init__(self):
        """Initialise the game's settings."""
        # Screen settings
        self.screen_width = 800
        self.screen_height = 600
        self.bg_colour = (0,0,0)

game_functions.py

import sys
import pygame

def check_events():
    """Respond to keypresses and mouse events."""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

def update_screen(ai_settings, screen):
    """Update images on the screen and flip to the new screen."""
    # Redraw the screen during each pass through the loop.
    screen.fill(ai_settings.bg_colour)


    pygame.display.flip()

就像我说我没有收到任何错误一样,游戏窗口只是打开为黑色背景。当我将其放入普通函数而不是类中时,代码便起作用了。

我在做什么错?如何显示桨?

1 个答案:

答案 0 :(得分:1)

您是否有理由在{main loop}之外设置paddles

paddles移至update_screen方法中至少会使它们显示。但是,每次调用update_screen时,都会更新一个新的Paddles对象。

def update_screen(ai_settings, screen):
    """Update images on the screen and flip to the new screen."""
    # Redraw the screen during each pass through the loop.
    screen.fill(ai_settings.bg_colour)
    paddles = Paddles(screen)
    pygame.display.flip()