如何在另一个文件中使用一个文件中的变量,该文件在python中导入第一个文件

时间:2017-07-18 12:46:59

标签: python python-2.7

我想开始用pygame在python中制作游戏。我在js做了几场比赛。我喜欢把我的游戏做成oop。当我在js中使用类时,我将它们分成文件。现在我用python创建简单的游戏(这是我的第一个js游戏)。就像在js中一样,我将对象拆分为单独的文件。现在我在播放器类中创建我的draw方法。我不知道如何绘制矩形,谁是我的窗口播放器(窗口是在一个名为main.py的文件中创建的,但是draw方法在名为Player.py的文件中) 问题是,当我开始游戏时,它只是向我显示白色窗口。

以下是我的代码: main.py:

import pygame

from Player import Player

pygame.init()
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
pygame.display.set_caption("Guitar Hero");
done = False
WW, WH = pygame.display.get_surface().get_size()

player = Player(0, 0, 0, WW, WH)

clock = pygame.time.Clock()
screen.fill((255, 255, 255))

while not done:
        for event in pygame.event.get():
                if event.type == pygame.QUIT:
                        done = True
                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_ESCAPE:
                        done = True
                    if event.key == pygame.K_UP:
                        player.move(1)
                        screen.fill((255, 255, 255))
                    if event.key == pygame.K_DOWN:
                        player.move(2)
                        screen.fill((255, 255, 255))

        player.draw(screen)

        pygame.display.flip()
        clock.tick(60)

和我的Player.py:

import pygame

WW, WH = 0, 0

class Player:
    """Player for Guitar Hero
        content:
            draw function,
            move function
    """
    def __init__(self, x, y, state, windowWidth, windowHeght):
        WW = windowWidth
        WH = windowHeght
        self.x = x
        self.y = y
        self.state = state
    def draw(self, surface):
        self.y = self.state * WH / 4
        #help me with this line
        pygame.draw.rect(surface, (0, 0, 255), pygame.Rect(self.x, self.y, WW / 50, WH / 4))
    def move(self, direction):
        if direction == 1 and self.state > 0:
            self.state -= 1
        else:
            self.state += 1

P.S。:如果Player类在main.py文件中,一切正常!

0 个答案:

没有答案