我在让玩家按照玩家正在寻找的方向拍摄时遇到问题
class Player(pygame.sprite.Sprite):
def __init__(self, screen, (posx, posy), shell):
'Called the Sprite class constructor'
pygame.sprite.Sprite.__init__(self)
'Save a reference to the screen'
self.screen = screen
self.shell = shell
'Create the image for the sprite'
self.image = pygame.image.load("player0.png")
self.image = self.image.convert()
tranColor = self.image.get_at((1, 1))
self.image.set_colorkey(tranColor)
self.rect = self.image.get_rect()
self.charge = 50
'Position the sprite'
self.x = posx
self.y = posy
self.rect.center = (self.x, self.y)
self.velocity = Vectors.Vector2D(2, 2)
self.imgDown = []
self.imgRight = []
self.imgLeft = []
self.imgUp = []
self.frame = 0
self.delay = 3
self.pause = self.delay
self.speed = 5
def update(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
self.rect.centerx -= self.velocity.mulscaler(self.speed).x
self.loadPicsLeft()
self.calcDelay()
self.image = self.imgLeft[self.frame]
if keys[pygame.K_d]:
self.rect.centerx += self.velocity.mulscaler(self.speed).x
self.loadPicsRight()
self.calcDelay()
self.image = self.imgRight[self.frame]
if keys[pygame.K_w]:
self.rect.centery -= self.velocity.mulscaler(self.speed).y
self.loadPicsUp()
self.calcDelay()
self.image = self.imgUp[self.frame]
if keys[pygame.K_s]:
self.rect.centery += self.velocity.mulscaler(self.speed).y
self.loadPics()
self.calcDelay()
self.image = self.imgDown[self.frame]
if self.velocity.x > self.speed:
self.velocity.x = self.speed
if self.velocity.x < -self.speed:
self.velocity.x = -self.speed
if self.velocity.y > self.speed:
self.velocity.y = self.speed
if self.velocity.y < -self.speed:
self.velocity.y = -self.speed
if keys[pygame.K_SPACE]:
self.shell.x = self.rect.centerx
self.shell.y = self.rect.centery
self.shell.speed = self.charge
def calcDelay(self):
self.pause -= 1
if self.pause <= 0:
self.pause = self.delay
self.frame += 1
if self.frame > 2:
self.frame = 0
def loadPics(self):
for i in range (7):
imgName = "player%d.png"% i
tmpImg = pygame.image.load(imgName)
tmpImg.convert()
tranColor = tmpImg.get_at((0,0))
tmpImg.set_colorkey(tranColor)
self.imgDown.append(tmpImg)
def loadPicsRight(self):
for i in range (7):
imgName = "playerRight%d.png"% i
tmpImg = pygame.image.load(imgName)
tmpImg.convert()
tranColor = tmpImg.get_at((0,0))
tmpImg.set_colorkey(tranColor)
self.imgRight.append(tmpImg)
def loadPicsLeft(self):
for i in range (7):
imgName = "playerLeft%d.png"% i
tmpImg = pygame.image.load(imgName)
tmpImg.convert()
tranColor = tmpImg.get_at((0,0))
tmpImg.set_colorkey(tranColor)
self.imgLeft.append(tmpImg)
def loadPicsUp(self):
for i in range (7):
imgName = "playerUp%d.png"% i
tmpImg = pygame.image.load(imgName)
tmpImg.convert()
tranColor = tmpImg.get_at((0,0))
tmpImg.set_colorkey(tranColor)
self.imgUp.append(tmpImg)
class Shell(pygame.sprite.Sprite):
def __init__(self, screen):
pygame.sprite.Sprite.__init__(self)
self.screen = screen
self.image = pygame.Surface((50, 50))
self.image.fill((0xff, 0xff, 0xff))
self.image.set_colorkey((0xff, 0xff, 0xff))
pygame.draw.circle(self.image, (0, 0, 0), (25, 25), 25)
self.image = pygame.transform.scale(self.image, (10, 10))
self.rect = self.image.get_rect()
self.rect.center = (-100, -100)
self.speed = 0
self.dir =0
self.reset()
def update(self):
self.checkBounds()
self.rect.center = (self.x, self.y)
def reset(self):
""" move off stage and stop"""
self.x = -100
self.y = -100
self.speed = 0
这是我的播放器和我的shell类,目前我的所有shell都出现在我的播放器的位置,我正在寻找的是我的播放器,当他看起来正确时向右射击子弹,在看的时候离开可以任何人向我提供有关我能做些什么来实现这一目标的任何提示。我现在只在大学开始编码,所以我有点挣扎
答案 0 :(得分:1)
1 - 存储播放器的方向
2 - 像这样制作外壳的速度:
self.speedx = speed_length * cos(player.angle)
self.speedy = speed_length * sin(player.angle)
注意:
Speed_length是炮弹在一帧中行进的总欧几里德距离。
角度必须为弧度,否则您可以使用度:
self.speedx = speed_length * cos(radians(player.angle))
self.speedy = speed_length * sin(radians(player.angle))