Pygame,使用向量射击子弹

时间:2013-10-22 19:29:55

标签: vector pygame

好的,所以整晚我和其他5个人一直在为我们的pygame模块做一个项目。我们的讲师没有给我们任何程序可以参考他只是向我们扔了一堆代码(没有评论)和期待我们理解它。我们从9月开始才开始python。我们真的需要一些帮助。

所以我们有一个由W A S& S控制的人类玩家。 D和15个随机点在屏幕上移动。我们需要使用矢量(我们从未使用过)来拍摄随机点。

请有人帮忙。

编辑代码添加〜

import pygame class Terminator(pygame.sprite.Sprite): def __init__(self, screen): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((20, 20)) self.image.fill((0, 0, 0)) pygame.draw.circle(self.image, (255, 0, 0), (10, 10), 10, 0) self.rect = self.image.get_rect() self.dx = screen.get_width()/2 self.dy = screen.get_height()/2 self.rect.center = (self.dx, self.dy) self.speed = 5 self.screen = screen def update(self): self.rect.center = (self.dx, self.dy) def MoveLeft(self): if self.rect.left < 0: self.dx += 0 else: self.dx -= self.speed def MoveRight(self): if self.rect.right > self.screen.get_width(): self.dx += 0 else: self.dx += self.speed def MoveUp(self): if self.rect.top <0: self.dy += 0 else: self.dy -= self.speed def MoveDown(self): if self.rect.bottom > self.screen.get_height(): self.dy += 0 else: self.dy += self.speed <code> humansprite.py <pre> import pygame import random import math class Humans(pygame.sprite.Sprite): def __init__(self, screen): pygame.sprite.Sprite.__init__(self) self.image = pygame.Surface((16, 16)) self.image.fill((0, 0, 0)) pygame.draw.circle(self.image, (0, 0, 255), (8, 8), 8, 0) self.rect = self.image.get_rect() self.dx = random.randrange(0, screen.get_height()) self.dy = random.randrange(0, screen.get_width()) self.screen = screen self.speed = 1 self.alive = True def update(self): self.rect.centerx -= self.dx self.rect.centery -= self.dy if self.rect.right < 0: self.reset() def reset(self): self.rect.left = self.screen.get_width() self.rect.centery = random.randrange(0, self.screen.get_height()) self.dy = random.randrange(-2, 2) self.dx = random.randrange(1, 4) <code> seekandDestory.py <pre> import pygame from TerminatorSprite import Terminator from humansSprite import Humans pygame.init() def checkKeys(myData): (event, ship) = myData if event.key == pygame.K_LEFT: print 'LEFT' ship.MoveLeft() if event.key == pygame.K_RIGHT: print 'RIGHT' ship.MoveRight() if event.key == pygame.K_UP: print 'UP' ship.MoveUp() if event.key == pygame.K_DOWN: print 'DOWN' ship.MoveDown() def main(): screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("Seek and Destroy") background = pygame.Surface(screen.get_size()) clock = pygame.time.Clock() screen.blit(background, (0, 0)) terminator = Terminator(screen) humans = [] for people in range(15): people = Humans(screen) humans.append(people) terminatorGroup = pygame.sprite.Group(terminator) humanGroup = pygame.sprite.Group(humans) clock = pygame.time.Clock() keepGoing = True pygame.key.set_repeat(10, 10) while keepGoing: clock.tick(60) pygame.mouse.set_visible(False) for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False if event.type == pygame.KEYDOWN: myData = (event, terminator) checkKeys(myData) #if pygame.sprite.spritecollide(terminator, humanGroup, False): terminatorGroup.clear(screen, background) terminatorGroup.update() terminatorGroup.draw(screen) humanGroup.clear(screen, background) humanGroup.update() humanGroup.draw(screen) pygame.display.flip() pygame.mouse.set_visible(True) if __name__ == "__main__": main() <code>

1 个答案:

答案 0 :(得分:0)

我昨晚完成了this set of examples!查看Lunar Lander示例,尤其是the way I'm handling vectors in lunarlander.py - 它为矢量管理提供了一个类V

你可能需要的是一个精灵为你的家伙用一个矢量代表他的速度(瞬间,可能是0)和角度,然后是每个随机点的精灵,具有相似的矢量和角度。拍摄时,使用矢量创建一个新的精灵,该矢量的角度与您当前所面对的角度相匹配,且幅度与子弹的速度相匹配。然后,在update()方法中,移动每个精灵(项目符号,随机点)。与我在lunarlander.py中所做的非常相似。

祝你好运!如果你刚刚开始使用Python,这是一个艰难的任务。快乐的编码!