我如何制作一个相对于背景的子弹类,这样当我继承这个类给我的敌人时,子弹就不会跟随我的玩家。
当前子弹类:
class Bullet():
def __init__(self, x, y, targetX, targetY):
self.origX = x
self.origY = y
self.x = x
self.y = y
self.targetX = targetX
self.targetY = targetY
self.image = image.load("res/attack/attack.png")
self.vel = 25 # rnge is the range of the bullet, in frames
self.rnge = 50 # prog is the progress of the bullet, in frames
self.prog = 0 # dmg is the damage that the bullet will do upon impact
self.dmg = 25
self.dmg_mult = 1 # deathtick is the timer for enemy death
self.deathTick = 0 # rect is the hitbox of the bullet
self.w, self.h = self.image.get_width(), self.image.get_height()
self.rect = Rect(self.x, self.y, self.w, self.h)
def update(self, playerRect):
# Increases Progress of the bullet
draw.rect(screen, (0,0,255), playerRect, 4)
draw.line(screen, (0,255,255), (playerRect[0], playerRect[1]), (playerRect[0], playerRect[1]), 4)
if not (sqrt((self.targetX - playerRect[0]) ** 2 + (self.targetY - playerRect[1]) ** 2)) == 0:
self.x += int((self.vel) * (self.targetX - playerRect[0]) /
(sqrt((self.targetX - playerRect[0]) ** 2 +
(self.targetY - playerRect[1]) ** 2)))
self.y += int((self.vel) * (self.targetY - playerRect[1]) /
(sqrt((self.targetX - playerRect[0]) ** 2 +
(self.targetY - playerRect[1]) ** 2)))
self.rect.center = [self.x-(playerRect[0]-player.rect[0]), self.y-(playerRect[1]-player.rect[1])]
我在我的运行循环中调用它:
playerRect = RelRect(player,camera)
if button == 1:
bullets.append(Bullet(playerRect[0]+playerRect[2]//2, playerRect[1]+playerRect[3]//2, mx, my))
和CameraClass:
class Camera(object):
def __init__(self, screen, player, levelWidth, levelHeight):
self.player = player
self.rect = screen.get_rect()
self.rect.center = self.player.center
self.worldRect = Rect(0, 0, levelWidth, levelHeight)
def update(self):
if self.player.centerx > self.rect.centerx:
self.rect.centerx = self.player.centerx
if self.player.centerx < self.rect.centerx:
self.rect.centerx = self.player.centerx
if self.player.centery > self.rect.centery:
self.rect.centery = self.player.centery
if self.player.centery < self.rect.centery:
self.rect.centery = self.player.centery
def drawSprites(self, surface, sprites):
for sprite in sprites:
if sprite.rect.colliderect(self.rect):
surface.blit(sprite.image, RelRect(sprite, self))
和RelRect函数是:
def RelRect(char, camera):
return Rect(char.rect.x - camera.rect.x, char.rect.y - camera.rect.y, char.rect.w, char.rect.h)
我无法弄清楚如何让子弹相对...... 谢谢!