猿问

如何在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

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


我什至要从哪里开始?


郎朗坤
浏览 315回答 2
2回答
随时随地看视频慕课网APP

相关分类

Python
我要回答