2013-03-31 75 views
-2

我有讓我的球員的方向拍攝的一個問題球員正在射擊的方向

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 

這是我的球員,我的殼類,在我所有的外殼程序是出現在當下我的球員的位置,即時尋找的是我的球員,當他看起來正確時向右射擊子彈,向左看時留下等等。任何人都可以爲我提供關於我能做些什麼來達到此目的的任何提示。林纔剛剛開始編碼在此刻大學,所以我掙扎了一下

+0

代碼你在哪裏存儲的方向是什麼? – YXD

+0

嗯我不知道,我認爲它在玩家的更新方法中,因爲在每次我按下空間時,強化器都會朝那個方向開火 –

+0

您應該使用Shell.update來移動子彈的速度。不知道你在哪裏移動它。 – ninMonkey

回答

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))