无法在列表中绘制所有矩形

时间:2015-11-16 16:03:25

标签: list python-3.x pygame

问题是接下来,我有一个列表"敌人"我正在添加矩形。在我完成添加矩形后,我需要使用属于Enemy类的.draw()函数绘制列表中的所有内容。

# Colour
# Created by Niktia Kotter

#!/usr/bin/env python
import pygame, sys, random, time
from pygame.locals import*

# set up pygame
pygame.init()

FPS=60
fpsclock = pygame.time.Clock()

#           R    G    B
WHITE   = (255, 255, 255)
GREEN   = (78, 255, 87)
YELLOW  = (241, 255, 0)
BLUE    = (80, 255, 239)
PURPLE  = (203, 0, 255)
RED     = (237, 28, 36)

SCREEN_W = 800
SCREEN_H = 480
SCREEN =  pygame.display.set_mode((SCREEN_W,SCREEN_H),0,32)            
snapMult = SCREEN_W / 5

pos0 = 0
pos1 = snapMult
pos2 = pos1 + snapMult
pos3 = pos2 + snapMult
pos4 = pos3 + snapMult

# set up calsses
class Actor:

    def __init__ (self,sizeX,sizeY,colour,positionX,positionY):

        self.sizeX = sizeX
        self.sizeY = sizeY
        self.colour = colour
        self.positionX = positionX
        self.positionY = positionY

    def move(self, dx, i):
        self.positionX += dx
        self.location = i

    def draw(self):
        pygame.draw.rect(SCREEN, self.colour, (self.positionX, self.positionY, self.sizeX, self.sizeY))
        return

class Enemy:

    def __init__ (self,sizeX,sizeY,colour,positionX,positionY):

        self.sizeX = sizeX
        self.sizeY = sizeY
        self.colour = colour
        self.positionX = positionX
        self.positionY = positionY
    def move(self, dy, i):
        self.positionY += dy
        self.location = i

    def draw(self):
        pygame.draw.rect(SCREEN, self.colour, (self.positionX, self.positionY, self.sizeX, self.sizeY))
        return

class Capture(object):

    def __init__(self):

        self.caption = pygame.display.set_caption('Space Invaders')
        self.screen = SCREEN
        self.startGame = True
        self.gameOver = False
        self.enemyCount = 0

    def main(self):

        clock = pygame.time.Clock()      
        actor =[]
        enemy =[]
        #enemy.append(Enemy(snapMult/20,snapMult/20,RED,pos1,SCREEN_H/2))
        #print(enemy, sep='\n')
        actor.append(Actor(snapMult,snapMult/2,RED,pos2,SCREEN_H-(snapMult/2)))
        while True:
            if self.startGame:

                SCREEN.fill(WHITE)
                actor[0].draw()

                if self.enemyCount != 5 or self.enemyCount > 5:
                    num = random.randint(1, 5)
                    if num == 0:
                        print (0)
                        enemy.append(Enemy(snapMult/20,snapMult/20,RED,pos0,SCREEN_H/2))
                    if num == 1:
                        print (1)
                        enemy.append(Enemy(snapMult/20,snapMult/20,RED,pos1,SCREEN_H/2))
                    if num == 2:
                        print (2)
                        enemy.append(Enemy(snapMult/20,snapMult/20,RED,pos2,SCREEN_H/2))
                    if num == 3:
                        print (3)
                        enemy.append(Enemy(snapMult/20,snapMult/20,RED,pos3,SCREEN_H/2))
                    if num == 4:
                        print (4)
                        enemy.append(Enemy(snapMult/20,snapMult/20,RED,pos4,SCREEN_H/2))
                    print(enemy, sep='\n')
                    #for i in range(10):
                       # x = enemy[i]
                       # x.draw()
                    #for Enemy in enemy:
                    #   Enemy.draw()
                    self.enemyCount += 1


                for event in pygame.event.get():

                    if actor[0].positionX != pos4 and (event.type == KEYDOWN) and (event.key == K_d):
                        actor[0].move(snapMult,0)

                    if actor[0].positionX != pos0 and(event.type == KEYDOWN) and (event.key == K_a):
                        actor[0].move(-snapMult,0)  

                    if event.type == QUIT:
                        pygame.quit()
                        sys.exit()


                pygame.display.update()
                fpsclock.tick(FPS)

if __name__ == '__main__':
    game = Capture()
    game.main()

1 个答案:

答案 0 :(得分:1)

使用简单的for循环:

for e in enemy:
    e.draw()

另一种方法是让您的类继承自Sprite,并使用Group代替普通list,这样您就可以使用draw()函数{ {3}}

另外,我不认为您想在每个活动上致电move。您通常每循环迭代更新一次游戏状态,例如移动鼠标会产生很多事件。