Python 在pygame中,如何连续生成和跟踪多个具有时间延迟的随机对象?
我想知道我怎么能在一个随机的位置以2秒的延迟/冷却时间产生物体(比如敌人) 我知道如何在随机坐标下繁殖它们。但我想知道的是,我如何生成多个对象,并且仍然跟踪其他已经移动的对象,就像你在pygame中射击子弹一样 我可能只需要使用Python 在pygame中,如何连续生成和跟踪多个具有时间延迟的随机对象?,python,loops,oop,random,pygame,Python,Loops,Oop,Random,Pygame,我想知道我怎么能在一个随机的位置以2秒的延迟/冷却时间产生物体(比如敌人) 我知道如何在随机坐标下繁殖它们。但我想知道的是,我如何生成多个对象,并且仍然跟踪其他已经移动的对象,就像你在pygame中射击子弹一样 我可能只需要使用pygame.time.get_ticks()就可以解决时间延迟/冷却问题。因此,我的主要问题是如何生成多个对象并使用Hitbox(我已经制作)跟踪它们 这是本例中生成小行星的基本部分 class Enemy: asteroids = [pygame.image.
pygame.time.get_ticks()
就可以解决时间延迟/冷却问题。因此,我的主要问题是如何生成多个对象并使用Hitbox(我已经制作)跟踪它们
这是本例中生成小行星的基本部分
class Enemy:
asteroids = [pygame.image.load('rock0.png'), pygame.image.load('rock1.png'), pygame.image.load('rock2.png'),
pygame.image.load('rock3.png'), pygame.image.load('rock4.png')]
def __init__(self, y, width, height):
self.width = width
self.height = height
self.vel = 1.5
self.x = random.randrange(screen_width - self.width * 2)
self.y = y
self.asteroid = random.choice(self.asteroids)
def draw(self, win):
self.move()
win.blit(self.asteroid, (self.x, self.y))
def move(self):
self.y = self.y + self.vel
这是所有需要它的人的全部代码
import pygame
import random
pygame.init()
screen_width = 500
screen_height = 500
win = pygame.display.set_mode((screen_width, screen_height))
walk_left = [pygame.image.load('sprite_5.png'), pygame.image.load('sprite_6.png')]
walk_right = [pygame.image.load('sprite_3.png'), pygame.image.load('sprite_4.png')]
standing = [pygame.image.load('sprite_0.png'), pygame.image.load('sprite_1.png'), pygame.image.load('sprite_2.png')]
class Player:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 4
self.left = False
self.right = False
self.standing = True
self.walk_count = 0
self.hitbox = (self.x + 2, self.y + 26, 123, 45)
def draw(self, win):
if self.walk_count + 1 >= 12:
self.walk_count = 0
if not self.standing:
if self.left:
win.blit(walk_left[self.walk_count // 6], (self.x, self.y))
self.walk_count += 1
elif self.right:
win.blit(walk_right[self.walk_count // 6], (self.x, self.y))
self.walk_count += 1
else:
win.blit(standing[self.walk_count // 4], (self.x, self.y))
self.walk_count += 1
self.hitbox = (self.x + 2, self.y + 26, 123, 45)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
def move():
if keys[pygame.K_LEFT] and man.x > man.vel or keys[pygame.K_a] and man.x > man.vel:
man.x -= man.vel
man.left = True
man.right = False
man.standing = False
elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
man.x += man.vel
man.left = False
man.right = True
man.standing = False
else:
man.standing = True
class Enemy:
asteroids = [pygame.image.load('rock0.png'), pygame.image.load('rock1.png'), pygame.image.load('rock2.png'),
pygame.image.load('rock3.png'), pygame.image.load('rock4.png')]
number = [0, 1, 2, 3, 4]
def __init__(self, y, width, height):
self.width = width
self.height = height
self.vel = 1.5
self.x = random.randrange(screen_width - self.width * 2)
self.y = y
self.index = random.choice(self.number)
self.hitbox = (self.x, self.y, self.width, self.height)
def draw(self, win):
self.move()
win.blit(self.asteroids[self.index], (self.x, self.y))
if self.index == 0:
self.hitbox = (self.x + 68, self.y + 68, self.width - 10, self.height - 14)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
elif self.index == 1:
self.hitbox = (self.x + 38, self.y + 47, self.width + 20, self.height - 5)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
elif self.index == 2:
self.hitbox = (self.x + 18, self.y + 12, self.width + 32, self.height + 30)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
elif self.index == 3:
self.hitbox = (self.x + 20, self.y + 32, self.width + 16, self.height + 5)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
else:
self.hitbox = (self.x + 4, self.y + 7, self.width - 24, self.height - 31)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
def move(self):
self.y = self.y + self.vel
class Projectile:
def __init__(self, x, y, width, height, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.vel = 5
def draw(self, win):
pygame.draw.rect(win, self.color, (self.x, self.y, self.height, self. width))
class Unit:
def __init__(self):
self.last = pygame.time.get_ticks()
self.cooldown = 200
def fire(self):
now = pygame.time.get_ticks()
if now - self.last >= self.cooldown:
self.last = now
spawn_bullet()
def spawn_bullet():
if keys[pygame.K_SPACE]:
bullets.append(Projectile((man.x + man.width // 2), (man.y - 7), 7, 3, (255, 0, 0)))
def re_draw():
win.fill((0, 0, 0))
asteroid.draw(win)
man.draw(win)
for bullet in bullets:
bullet.draw(win)
pygame.display.update()
delay = Unit()
man = Player(186, 400, 128, 128)
bullets = []
asteroid = Enemy(10, 64, 64)
run = True
clock = pygame.time.Clock()
while run:
last = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if 0 < bullet.y < 500:
bullet.y -= bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
move()
delay.fire()
clock.tick(60)
re_draw()
pygame.quit()
导入pygame
随机输入
pygame.init()
屏幕宽度=500
屏幕高度=500
win=pygame.display.set_模式((屏幕宽度、屏幕高度))
walk_left=[pygame.image.load('sprite_5.png'),pygame.image.load('sprite_6.png')]
walk_right=[pygame.image.load('sprite_3.png'),pygame.image.load('sprite_4.png')]
standing=[pygame.image.load('sprite_0.png')、pygame.image.load('sprite_1.png')、pygame.image.load('sprite_2.png')]
职业球员:
定义初始值(自、x、y、宽度、高度):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.vel=4
self.left=False
self.right=False
自立
self.walk\u计数=0
self.hitbox=(self.x+2,self.y+2612345)
def抽签(自我,赢):
如果self.walk\u count+1>=12:
self.walk\u计数=0
如果不是自立的:
如果self.left:
win.blit(左行走[self.walk\u count//6],(self.x,self.y))
self.walk_count+=1
elif self.right:
win.blit(向右行走[self.walk\u count//6],(self.x,self.y))
self.walk_count+=1
其他:
win.blit(站着[self.walk\u count//4],(self.x,self.y))
self.walk_count+=1
self.hitbox=(self.x+2,self.y+2612345)
pygame.draw.rect(赢,(255,0,0),self.hitbox,2)
def move():
如果键[pygame.K_左]和man.x>man.vel或键[pygame.K_a]和man.x>man.vel:
man.x-=man.vel
左=真
对=错
站着的人
elif键[pygame.K_RIGHT]和man.x<500-man.width-man.vel:
man.x+=man.vel
左=假
正确的
站着的人
其他:
站着
阶级敌人:
小行星=[pygame.image.load('rock0.png')、pygame.image.load('rock1.png')、pygame.image.load('rock2.png'),
pygame.image.load('rock3.png')、pygame.image.load('rock4.png')]
数字=[0,1,2,3,4]
定义初始值(自身、y、宽度、高度):
self.width=宽度
自我高度=高度
self.vel=1.5
self.x=random.randrange(屏幕宽度-self.width*2)
self.y=y
self.index=random.choice(self.number)
self.hitbox=(self.x、self.y、self.width、self.height)
def抽签(自我,赢):
self.move()
blit(self.asteroids[self.index],(self.x,self.y))
如果self.index==0:
self.hitbox=(self.x+68,self.y+68,self.width-10,self.height-14)
pygame.draw.rect(赢,(255,0,0),self.hitbox,2)
elif self.index==1:
self.hitbox=(self.x+38,self.y+47,self.width+20,self.height-5)
pygame.draw.rect(赢,(255,0,0),self.hitbox,2)
elif self.index==2:
self.hitbox=(self.x+18,self.y+12,self.width+32,self.height+30)
pygame.draw.rect(赢,(255,0,0),self.hitbox,2)
elif self.index==3:
self.hitbox=(self.x+20,self.y+32,self.width+16,self.height+5)
pygame.draw.rect(赢,(255,0,0),self.hitbox,2)
其他:
self.hitbox=(self.x+4,self.y+7,self.width-24,self.height-31)
pygame.draw.rect(赢,(255,0,0),self.hitbox,2)
def移动(自我):
self.y=self.y+self.vel
类射弹:
定义初始值(自、x、y、宽度、高度、颜色):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.color=颜色
self.vel=5
def抽签(自我,赢):
pygame.draw.rect(win,self.color,(self.x,self.y,self.height,self.width))
班级单位:
定义初始化(自):
self.last=pygame.time.get_ticks()
自我冷却时间=200
def火灾(自):
now=pygame.time.get_ticks()
如果现在-self.last>=self.cooldown:
self.last=现在
繁殖子弹()
def spawn_bullet():
如果键[pygame.K_SPACE]:
附加(投射物((man.x+man.width//2),(man.y-7),7,3,(255,0,0)))
定义重新绘制()
赢。填充((0,0,0))
小行星。平局(赢)
男子平局(胜利)
对于子弹中的子弹:
子弹。抽签(赢)
pygame.display.update()
延迟=单位()
男子=运动员(186400128128)
项目符号=[]
小行星=敌人(10,64,64)
运行=真
clock=pygame.time.clock()
运行时:
last=pygame.time.get_ticks()
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
对于子弹中的子弹:
如果0
我建议使用计时器事件。用于在事件队列上重复创建事件。使用并从中派生出敌人来管理多个敌人。注意在精灵中使用属性
.image
和.rect
非常重要。e、 g:
class敌人(py)