创建一个类的多个实例

时间:2013-09-12 02:05:18

标签: python class pygame

我正在Pygame中开发一种太空入侵者类型的游戏。到目前为止,我只是将游戏的基础知识降下来了:

import pygame, sys, os, math, random, time
from pygame.locals import *

pygame.init()

window = pygame.display.set_mode((1000,500))
screen = pygame.display.get_surface()

spaceBackground = pygame.image.load("C:/Users/LN/Desktop/space-background.png")
spaceShip = pygame.image.load("C:/Users/LN/Desktop/space-ship.png")
bullet = pygame.image.load("C:/Users/LN/Desktop/bullet.png")
enemyShip = pygame.image.load("C:/Users/LN/Desktop/enemyAircraft.png")
class move():
    '''Move the space ship'''

    def _init_(self):
        screen.blit(spaceBackground, (0,0))
        self.position = spaceShip.get_rect()
        self.position = self.position.move(500,477)
        global place
        place = self.position
        pygame.display.flip()

    def moveUp(self):
        screen.blit(spaceBackground, self.position, self.position)
        self.position = self.position.move(0,-2)
        global place
        place = self.position

    def moveDown(self):
        screen.blit(spaceBackground, self.position, self.position)
        self.position = self.position.move(0,2)
        global place
        place = self.position

    def moveLeft(self):
        screen.blit(spaceBackground, self.position, self.position)
        self.position = self.position.move(-2,0)
        global place
        place = self.position

    def moveRight(self):
        screen.blit(spaceBackground, self.position, self.position)
        self.position = self.position.move(2,0)
        global place
        place = self.position

    def update(self):
        screen.blit(spaceShip, self.position)

    def notTooLow(self):
        if self.position[1] < (478):
            return True
        else:
            return False

    def notTooHigh(self):
        if self.position[1] > (4):
            return True
        else:
            return False
    def notTooRight(self):
        if self.position[0] < (974):
            return True
        else:
            return False

    def notTooLeft(self):
        if self.position[0] > (5):
            return True
        else:
            return False


class shoot():
    '''Shoots bullets out of the ship'''

    def _init_(self):
        global bulletUp
        bulletUp = False
        self.position = (0,0)

    def startMoveUp(self):
        self.position = place
        self.position = self.position.move(11,0)

    def bulletOnScreen(self):
        if self.position[1] > -5:
            return True
        else:
            global bulletUp
            bulletUp = False

    def moveUp(self):
        screen.blit(spaceBackground, self.position)
        self.position = self.position.move(0,-6)
        screen.blit(bullet, self.position)
        screen.blit(spaceShip, place)


class enemy():
    '''Spawn and control enemies'''

    def _init_(self):
        global numbEnemy
        numbEnemy = 0
        self.position = enemyShip.get_rect()

    def moveSpawn(self):
        self.position = self.position.move(0,50)

    def spawnEnemy(self):                                 
        screen.blit(enemyShip, (500, 20))
        self.position = enemyShip.get_rect()

    def moveEnemy(self):
        screen.blit(spaceBackground, self.position)
        self.position = self.position.move(0,1)
        screen.blit(enemyShip, self.position)






move = move()
move._init_()
shoot = shoot()
shoot._init_()
enemy = enemy()
enemy._init_()



counter = 0
while True:
    if counter//300*300 == counter:
        enemy.spawnEnemy()
    if counter//2*2 == counter:
        enemy.moveEnemy()
    if pygame.key.get_pressed()[K_UP] and move.notTooHigh():
        move.moveUp()
    pygame.event.pump()
    if pygame.key.get_pressed()[K_DOWN] and move.notTooLow():
        move.moveDown()

我不知道该怎么做,就是制造多艘敌舰。有没有一种简单的方法可以让它产生另一艘船,而不会删除另一艘?

1 个答案:

答案 0 :(得分:5)

假设你的游戏中有一个叫做敌人的类。为简洁起见,我不会明确定义函数,只会使用pass语句作为占位符。

class Enemy:
    def __init__(self):
        pass

    def update(self):
        pass

    def draw(self, display_surface):
        pass

要创建多个敌人,您只需创建一个敌人列表,您将存储对当前活动敌方对象的所有引用。

enemies = []

要添加单个敌人,只需将enemy的构造函数调用添加到列表中即可。您可以根据需要多次执行此操作。

enemies.append(Enemy())

正如@monkey在评论中也提到的那样,你可以轻松地使用列表理解来同时将你的活动敌人列表设置为多个对象实例。

enemies = [Enemy() for x in range(10)]

然后在你的游戏循环中,你会在每次迭代中执行以下操作:

for enemy in enemies: # loop through all your active enemies
    enemy.update() # Update the state of every enemy.

for enemy in enemies: # once again loop through all your active enemies
    enemy.draw(display_surface) # Draw the image of every enemy to the display surface.

这是你如何巧妙地在Pygame中进行面向对象的游戏编程的一个基本例子。