如何在PYGAME中同时移动多个对象

时间:2013-06-05 16:45:56

标签: python class pygame

我想制作一个游戏,让我有来自屏幕两侧的敌人。 现在我拥有它,以便敌人一次一个地滚动屏幕。 我想一次又一次地慢慢增加他们遇到的频率。 这是我的代码

import pygame, sys, time, random
from pygame.locals import *
pygame.init()
winW = 1000
winH = 600
surface = pygame.display.set_mode ((winW, winH),0,32)


pygame.display.set_caption ('Moving Orc')

class Enemy:
    def __init__(self, char, startY, startX):
        self.char=char
        self.startY=startY
        self.startX=startX
        self.drawChar()

    def drawChar (self):
        self.space = pygame.image.load (self.char)
        self.spaceRect = self.space.get_rect ()
        self.spaceRect.topleft = (self.startX,self.startY)
        self.moveChar()

    def moveChar (self):
        if self.startX == 0:
            self.xMoveAmt = 5
        elif self.startX == 800:
            self.xMoveAmt = -5

        while True:
            surface.fill ((255,255,255))
            self.spaceRect.left += self.xMoveAmt

            surface.blit (self.space, self.spaceRect)

            pygame.display.update()

            time.sleep (0.02)

            if self.spaceRect.right >= winW:
                surface.fill ((255,255,255))
                break

            elif self.spaceRect.left <= 0:
                surface.fill ((255,255,255))
                break


#MAINLINE
while True:
    enemyList=[]
    leftOrRight = random.randint(0,1)
    if leftOrRight == 0:
        leftOrRight = 0
    elif leftOrRight == 1:
        leftOrRight = 800
    enemyList.append(Enemy(("orc.png"), random.randint(50, 500), leftOrRight))

    for i in range (0,len(enemyList)):
        enemyList[i].drawChar()
        break

我有它,所以每次进入循环时,它会重置它在我所制作的类中运行的列表。一个人将从左侧或右侧穿过屏幕。

我甚至会从哪里开始?

2 个答案:

答案 0 :(得分:2)

摆脱drawChar功能;让Enemy类知道应该只存在于游戏逻辑中的surface是不好的做法。更改moveChar功能,使其只更新对象的位置。从moveChar中取出循环并处理主游戏循环中的移动。

Enemy 类:

class Enemy(object):
    def __init__(self, char, startX=0, startY=0, xMovAmnt=0):
        self.char = char
        self.x = startX
        self.y = startY
        self.xMovAmnt = xMovAmnt
        # no reason to load the image every time you want to draw, do it here
        self.image = pygame.image.load(self.char)
        self.rect = self.image.get_rect()

    def moveChar(self):
        self.x += self.xMovAmnt

游戏循环

enemyList = []
while True:
    ...
    # you never specified when you want to create a new Enemy, 
    #    so you need to figure that out on your own
    ...

    # this is a more "Pythonic" way of looping over a list than using a range
    for enemy in enemyList:
        enemy.movChar()
        surface.blit(enemy.image, (enemy.x, enemy.y))

    pygame.display.update()

答案 1 :(得分:2)

为了拥有多个敌人,你应该修复一些事情。

简单的pygame程序结构如何

init() 
While(True):
    draw()
    update()
    checkInput()

我看到你已经为敌人写了一个平局和移动函数,但是他们没有做他们应该做的事。

您的绘图方法加载图像,并调用移动函数。加载通常应在__init__()

中完成

你的移动功能会绘制并移动角色,但它有一个While循环,这会使它一直停留,直到该角色不在屏幕上。

示例解决方案:

def draw(self,surface):
    surface.blit (self.space, self.spaceRect)

def move(self):
    self.spaceRect.left += self.xMoveAmt
    if self.spaceRect.right >= winW:
        self.kill()
    elif self.spaceRect.left <= 0:
        self.kill()

杀死对象的一种可能方法是设置一个标志,并在While方法中检查是否可以从对象列表中删除它。

现在你可以创建一个敌人列表,并调用draw,并为每个敌人进行更新。在for循环中。

相关问题