使用pygame

时间:2015-08-21 23:59:00

标签: python include pygame

我刚开始使用python,更确切地说是pygame,我发现自己无法做一件非常简单的事情: 导入文件。

以下是代码:

主档案:

import pygame
from pygame.locals import *
from cTile import Tile


class App:
    def __init__(self):
        self._running = True
        self._display_surf = None
        self._image_surf = None
        self._x = 0
        self._y = 0
        self.tiles = []

    def on_init(self):
        pygame.init()
        self._display_surf = pygame.display.set_mode((350,350), pygame.HWSURFACE)
        self._running = True
        #self._image_surf = pygame.image.load("myimage.jpg").convert()
        self._image_surf = pygame.image.load("myimage.jpg").convert()
        self.tiles = [Tile(0,0,0,0),Tile(1,0,64,0),Tile(0,1,0,64),Tile(1,1,64,64)]
        m = open("map1.map",'r')
        r = m.read().split(",")
        for i in range(len(r)/4):
            self.tiles.append(Tile(int(r[i*4]),int(r[i*4+1]),int(r[i*4+2]),int(r[i*4+3])))
            i = i+3

    def on_event(self, event):
        if event.type == QUIT:
            self._running = False
        elif event.type == pygame.KEYDOWN:
            self.key_event(event)

    def key_event(self,event):
        if event.key == pygame.K_DOWN:
            self._y = self._y+16
        if event.key == pygame.K_UP:
            self._y = self._y-16
        if event.key == pygame.K_RIGHT:
            self._x = self._x+16
        if event.key == pygame.K_LEFT:
            self._x = self._x-16
    def on_loop(self):
        pass
    def on_render(self):
        self._display_surf.fill((0, 0, 0))
        for tile in self.tiles:
            tile.render(self)
        self._display_surf.blit(self._image_surf,(self._x,self._y), pygame.Rect(0, 0, 16, 16) )
        pygame.display.flip()

    def on_cleanup(self):
        pygame.quit()

    def on_execute(self):
        if self.on_init() == False:
            self._running = False

        while( self._running ):

            for event in pygame.event.get():
                self.on_event(event)
            self.on_loop()
            self.on_render()
        self.on_cleanup()

if __name__ == "__main__" :
    theApp = App()
    theApp.on_execute()

我包含的文件:

class Tile:
    def __init__(self,_pidx,_pidy,_px,_py):
        self._image_surf = pygame.image.load("myimage.jpg").convert()
        self._idx = _pidx
        self._idy = _pidy
        self._x = _px
        self._y = _py
    def render(self,win):
        win._display_surf.blit(self._image_surf,(self._x,self._y), pygame.Rect(self._idx*16, self._idy*16, self._idx*16+16, self._idy*16+16) )

最后我得到的错误:

Traceback (most recent call last):
  File "C:\Users\Morgan\Desktop\test.py", line 70, in <module>
    theApp.on_execute()
  File "C:\Users\Morgan\Desktop\test.py", line 57, in on_execute
    if self.on_init() == False:
  File "C:\Users\Morgan\Desktop\test.py", line 22, in on_init
    self.tiles = [Tile(0,0,0,0),Tile(1,0,64,0),Tile(0,1,0,64),Tile(1,1,64,64)]
  File "C:\Users\Morgan\Desktop\cTile.py", line 3, in __init__
    self._image_surf = pygame.image.load("myimage.jpg").convert()
NameError: global name 'pygame' is not defined

我现在正在学习这门语言。我试图将pygame导入到我的cTile.py文件中,并尝试在之后使用pygame.init(),但仍然遇到同样的错误。

我虽然导入的内容与php中的内容相同,但显然不是!

代码本身可能会伤害任何对python知之甚少的人,但是我在一小时前再次开始,还有很多东西需要修复,目前的主要问题包括我无法完全理解!提前感谢您提供的任何帮助!

编辑:当cTile.py中包含的代码刚刚粘贴在“Class App:”上方时,一切运行顺利。

1 个答案:

答案 0 :(得分:0)

您需要在import pygame文件中cTile.py。模块不会继承其客户可能(或可能不)导入的模块。