如何在pygame中存储对象的先前坐标?我的问题可能很难解释,但是我会尽力而为,如果您亲自尝试我的代码以理解我的意思,这可能会有所帮助。
这就是我的比赛内容。我希望这将使我的问题更容易理解。
我正在创建一个pygame,在该游戏中,您有一个角色在底部射击朝向他的小行星。小行星每1秒出现一次。当小行星移向玩家时,我希望它们沿直线向下移动,但是我对x位置的变量是每个小行星随机产生的。
但是,先前的小行星的x位置已更新为新的小行星。这意味着先前的小行星将移动到与新小行星相同的x位置。如何防止这种情况发生?
这就是问题所在(向下滚动以获取完整代码)
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:
x = random.randrange(screen_width - 64 * 2) # right here
index = random.choice(number)
asteroids_on_screen.append(Enemy(rock.y, rock.width, rock.height))
for a in asteroids_on_screen:
if -141 < a.y < 500:
a.y += a.vel
else:
asteroids_on_screen.pop(asteroids_on_screen.index(a))
我将提供我的完整代码,供任何人尝试以阐明我的问题。
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 + 31, 123, 40)
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:
def __init__(self, y, width, height):
self.width = width
self.height = height
self.vel = 1.5
self.y = y
x = random.randrange(screen_width - 64 * 2)
self.hitbox = (x, self.y, self.width, self.height)
def draw(self, win):
win.blit(asteroids[index], (x, self.y))
if index == 0:
self.hitbox = (x + 68, self.y + 68, self.width - 10, self.height - 14)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
elif index == 1:
self.hitbox = (x + 38, self.y + 47, self.width + 20, self.height - 5)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
elif index == 2:
self.hitbox = (x + 18, self.y + 12, self.width + 32, self.height + 30)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
elif index == 3:
self.hitbox = (x + 20, self.y + 32, self.width + 16, self.height + 5)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
else:
self.hitbox = (x + 4, self.y + 7, self.width - 24, self.height - 31)
pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)
my_event_id = pygame.USEREVENT + 1
pygame.time.set_timer(my_event_id, 1000)
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 + 126 // 2), (man.y + 5), 7, 3, (255, 0, 0)))
def re_draw():
win.fill((0, 0, 0))
man.draw(win)
for bullet in bullets:
bullet.draw(win)
for a in asteroids_on_screen:
a.draw(win)
pygame.display.update()
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]
delay = Unit()
man = Player(186, 400, 128, 128)
bullets = []
asteroids_on_screen = []
rock = Enemy(-140, 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
elif event.type == my_event_id:
x = random.randrange(screen_width - 64 * 2)
index = random.choice(number)
asteroids_on_screen.append(Enemy(rock.y, rock.width, rock.height))
for a in asteroids_on_screen:
if -141 < a.y < 500:
a.y += a.vel
else:
asteroids_on_screen.pop(asteroids_on_screen.index(a))
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()
如果有人希望使用所使用的镜架/精灵,请告诉我
答案 0 :(得分:2)
您必须将x
设为类Enemy
的实例变量(属性)。这样每个敌人都会得到它自己的“ x”坐标。
这意味着用x
代替self.x
:
class Enemy:
def __init__(self, y, width, height):
self.width = width
self.height = height
self.vel = 1.5
self.y = y
self.x = random.randrange(screen_width - 64 * 2)
self.hitbox = (self.x, self.y, self.width, self.height)
def draw(self, win):
win.blit(asteroids[index], (self.x, self.y))
if 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 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 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 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)