问题描述
我想知道如何以 2 秒的延迟/冷却在随机位置生成物体(如敌人).
I wonder how I can spawn objects (like an enemy) at a random position with a 2 seconds delay/cooldown.
我知道如何在随机坐标处生成它们.但我想知道的是我如何生成多个物体,并且仍然跟踪其他已经移动的物体,就像你射子弹时所做的那样一个pygame.
I know how to spawn them at a random coordinate. But what I'm wondering is how I can spawn multiple objects and still keep track of the other ones that are already moving, kind of like you do when you shoot bullets in a pygame.
我可能可以通过使用 pygame.time.get_ticks()
来解决时间延迟/冷却时间.所以我的主要问题是如何生成多个对象并使用hitboxes(我已经制作)跟踪它们
The time delay/cooldown I can probably solve just by using pygame.time.get_ticks()
. So my main question is how I can spawn multiple objects and track them with hitboxes (which I have already made)
这是本示例中生成小行星的基本部分.
Here is the basic part that in this example spawns an asteroid.
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
这里是任何需要它的人的完整代码.
Here is the entire code for anybody who needs it.
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.time.set_timer()
在事件队列上重复创建一个事件.
使用 pygame.sprite.Group
并从 Enemy>pygame.sprite.Sprite
管理多个敌人.请注意,在精灵中使用属性 .image
和 .rect
很重要.例如:
I recommend to use a timer event. Use pygame.time.set_timer()
to repeatedly create an event on the event queue.
Use a pygame.sprite.Group
and derive Enemy
from pygame.sprite.Sprite
to manage multiple enemies. Note it is important to use the attributes .image
and .rect
in a sprite. e.g.:
class Enemy(pygame.sprite.Sprite):
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):
super().__init__()
self.width = width
self.height = height
self.vel = 1.5
x = random.randrange(screen_width - self.width * 2)
self.image = random.choice(self.asteroids)
self.rect = self.image.get_rect(center = (x, y))
def move(self):
self.rect.y += self.vel
enemies = pygame.sprite.Group()
my_event_id = pygame.USEREVENT + 1
pygame.time.set_timer(my_event_id, 2000) # 2000 milliseconds = 2 seconds
run = True
while run:
last = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == my_event_id:
# spawn new enemy
enemies.add(Enemy(10, 64, 64))
# [...]
for e in enemies:
e.move()
# [...]
enemies.draw(win)
如果您将 Projectile
设为 pygame.sprite.Sprite
,然后你可以使用和 bullets
一个 pygame.sprite.Group
,然后就可以使用pygame.sprite.spritecollide()
或 pygame.sprite.groupcollide()
找到击中并杀死敌人.例如:
If you make Projectile
a pygame.sprite.Sprite
, then you can use and bullets
a pygame.sprite.Group
, then you can use pygame.sprite.spritecollide()
or pygame.sprite.groupcollide()
to find hits and kill the enemies. e.g.:
class Projectile(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(color)
self.rect = self.image.get_rect(center = (x, y))
self.vel = 5
def move(self):
self.rect.y -= self.vel
bullets = pygame.sprite.Group()
def spawn_bullet():
if keys[pygame.K_SPACE]:
bullets.add(Projectile((man.x + man.width // 2), (man.y - 7), 3, 7, (255, 0, 0)))
def re_draw():
win.fill((0, 0, 0))
enemies.draw(win)
man.draw(win)
bullets.draw(win)
pygame.display.update()
while run:
# [...]
for e in enemies:
e.move()
if e.rect.y > 500:
e.kill()
for b in bullets:
b.move()
if 0 > b.rect.y or b.rect.y > 500:
b.kill()
pygame.sprite.groupcollide(bullets, enemies, True, True)
完整代码:
Full code:
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(pygame.sprite.Sprite):
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):
super().__init__()
self.width = width
self.height = height
self.vel = 1.5
x = random.randrange(screen_width - self.width * 2)
self.image = random.choice(self.asteroids)
self.rect = self.image.get_rect(topleft = (x, y))
def move(self):
self.rect.y += self.vel
class Projectile(pygame.sprite.Sprite):
def __init__(self, x, y, width, height, color):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(color)
self.rect = self.image.get_rect(topleft = (x, y))
self.vel = 5
def move(self):
self.rect.y -= self.vel
my_event_id = pygame.USEREVENT + 1
pygame.time.set_timer(my_event_id, 2000) # 2000 milliseconds = 2 seconds
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.add(Projectile((man.x + man.width // 2), (man.y - 7), 3, 7, (255, 0, 0)))
def re_draw():
win.fill((0, 0, 0))
enemies.draw(win)
man.draw(win)
bullets.draw(win)
pygame.display.update()
delay = Unit()
man = Player(186, 400, 128, 128)
bullets = pygame.sprite.Group()
enemies = pygame.sprite.Group()
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
elif event.type == my_event_id:
# spawn new enemy
enemies.add(Enemy(10, 64, 64))
for e in enemies:
e.move()
if e.rect.y > 500:
e.kill()
for b in bullets:
b.move()
if 0 > b.rect.y or b.rect.y > 500:
b.kill()
pygame.sprite.groupcollide(bullets, enemies, True, True)
keys = pygame.key.get_pressed()
move()
delay.fire()
clock.tick(60)
re_draw()
pygame.quit()
这篇关于如何在 pygame 中连续生成和跟踪具有时间延迟的多个随机对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!