AttributeError:'开始'对象没有属性' draw'

时间:2018-01-19 17:57:26

标签: python pygame attributeerror

我正在尝试通过制作小程序来学习python。我正在尝试制作一个小型中文游戏程序(通过pygame),为YouTube中的一些人建模。 我正在尝试设置一个开始游戏精灵,当你与它碰撞并按下按钮时它将切换到一个新的图片。

我已经设法让我的Tiles出现了,但我无法得到我开始的游戏画面。因为我希望播放器与开始精灵发生冲突,我试图将Start图像放在另一个组中,但是当我尝试让程序显示该组时,我得到了:

  

属性错误:'开始'对象没有属性' draw'

我有几个文件夹,但只有两个相关的主要游戏叫做Class Test 和精灵:叫做汉字

这是我的代码:新部分是我获得Start游戏组的地方

import pygame as pg
import sys
import os
from Settings import *
from Sprites import *
from Hanzi import*
from Pinyin import*

class Game:
    def __init__(self):
        pg.init()
        self.screen = pg.display.set_mode((WIDTH, HEIGHT))
        pg.display.set_caption(TITLE)
        self.clock = pg.time.Clock()
        pg.key.set_repeat(500, 100)
        self.load_data()

    def load_data(self):
        pass
#
    def new(self):
        self.all_sprites = pg.sprite.Group()
        self.player = Player(self, 1, 1)
        self.gameround =0
        self.startgame = pg.sprite.Group()
        self.startgame = Start(self, "StartGame")


        self.hanziwo1 = Tile(self,"Hao", 1)
        self.hanziwo2 = Tile(self,"Wo", 2)
        self.hanzishi3 = Tile(self, "Shi", 3)
        self.hanziren4 = Tile(self, "Ren", 4)
    def run(self):
        # game loop - set self.playing = False to end the game

        self.playing = True
        while self.playing:
            self.dt = self.clock.tick(FPS) / 1000
            self.events()
            self.update()
            self.draw()

    def quit(self):
        pg.quit()
        sys.exit()

    def update(self):
        self.all_sprites.update()
        self.startgame.update()
    def draw_grid(self):
        for x in range(0, WIDTH, TILESIZE):
            pg.draw.line(self.screen, LIGHTGREY, (x, 0), (x, HEIGHT))
        for y in range(0, HEIGHT, TILESIZE):
            pg.draw.line(self.screen, LIGHTGREY, (0, y), (WIDTH, y))

    def draw(self):
        self.screen.fill(BGCOLOR)
        self.draw_grid()
        self.all_sprites.draw(self.screen)
        pg.display.flip()
        self.startgame.draw(self.screen)

    def events(self):
        for event in pg.event.get():
            if event.type == pg.QUIT:
                self.quit()
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_SPACE:
                    self.game.new.startgame.kill()
                if event.key == pg.K_ESCAPE:
                    self.quit()
                if event.key == pg.K_LEFT:
                    self.player.move(dx=-1)
                if event.key == pg.K_RIGHT:
                    self.player.move(dx=1)
                if event.key == pg.K_UP:
                    self.player.move(dy=-1)
                if event.key == pg.K_DOWN:
                    self.player.move(dy=1)

    def show_start_screen(self):
        pass

    def show_go_screen(self):
        pass

# create the game object
g = Game()
g.show_start_screen()
while True:

    g.new()
    g.run()
    g.show_go_screen()

然后这是我的精灵文件夹:Tile类可以工作,但Start类没有。我认为它与sprite组有关...

#Hanzi
import pygame as pg
import sys
from Settings import *
import Sprites
import os

game_folder = os.path.dirname(__file__)

Char_folder = os.path.join(game_folder, "Chin_Char1")

class Start(pg.sprite.Sprite):
    def __init__(self, game, character):
        self.groups = game.startgame
        pg.sprite.Sprite.__init__(self, self.groups)
        self.game = game
        self.character = character
        self.image = pg.image.load(os.path.join(Char_folder, self.character+".png")).convert()
        self.image.set_colorkey(WHITE)
        self.image = pg.transform.scale(self.image,(TILESIZE*4,TILESIZE*4))
        self.rect = self.image.get_rect()
    # Apparently I dont even need an update section....? that is strange but not really because maybe if I need to change rounds it will need to be in the update section
    def update(self):
        x = 3
        y = 3

        self.rect.x = x * TILESIZE
        self.rect.y = y *TILESIZE

class Tile(pg.sprite.Sprite):
    def __init__(self, game, character, location):
        self.game = game
        self.groups = game.all_sprites
        pg.sprite.Sprite.__init__(self, self.groups)
        self.character = character
        self.location = location
        self.image = pg.image.load(os.path.join(Char_folder, self.character+".png")).convert()
        self.image.set_colorkey(WHITE)
        self.image = pg.transform.scale(self.image,(TILESIZE,TILESIZE))
        self.rect = self.image.get_rect()
    # Apparently I dont even need an update section....? that is strange but not really because maybe if I need to change rounds it will need to be in the update section
    #def update(self):
        x = -1
        y = 5
        if self.location <= 10:
            x += self.location
        if self.location >= 11:
            x += self.location -10
            y += self.location -10
        self.rect.x = x * TILESIZE
        self.rect.y = y *TILESIZE

1 个答案:

答案 0 :(得分:1)

要将元素添加到组,您必须使用add()

    self.startgame.add( Start(self, "StartGame") )

但您将组替换为Start

    self.startgame = Start(self, "StartGame")

它会产生问题