结束语
确定正确的碰撞样式可以极大地改变游戏的感觉 。我们现在有一个更好的流星与玩家碰撞,但请注意,我们没有改变子弹与流星碰撞的风格 。圆形对于子弹的形状来说是一个糟糕的选择,所以最好将它们保留为矩形 。
在下一部分中,我们将通过学习如何向精灵添加动画来使事情变得生动起来
此部分的完整代码
# KidsCanCode - Game Development with Pygame video series# Shmup game - part 5# Video link: https://www.youtube.com/watch?v=_y5U8tB36Vk# Improved collisionsimport pygameimport randomfrom os import pathimg_dir = path.join(path.dirname(__file__), 'img')WIDTH = 480HEIGHT = 600FPS = 60# define colorsWHITE = (255, 255, 255)BLACK = (0, 0, 0)RED = (255, 0, 0)GREEN = (0, 255, 0)BLUE = (0, 0, 255)YELLOW = (255, 255, 0)# initialize pygame and create windowpygame.init()pygame.mixer.init()screen = pygame.display.set_mode((WIDTH, HEIGHT))pygame.display.set_caption("Shmup!")clock = pygame.time.Clock()class Player(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image = pygame.transform.scale(player_img, (50, 38))self.image.set_colorkey(BLACK)self.rect = self.image.get_rect()self.radius = 20# pygame.draw.circle(self.image, RED, self.rect.center, self.radius)self.rect.centerx = WIDTH / 2self.rect.bottom = HEIGHT - 10self.speedx = 0def update(self):self.speedx = 0keystate = pygame.key.get_pressed()if keystate[pygame.K_LEFT]:self.speedx = -8if keystate[pygame.K_RIGHT]:self.speedx = 8self.rect.x += self.speedxif self.rect.right > WIDTH:self.rect.right = WIDTHif self.rect.left < 0:self.rect.left = 0def shoot(self):bullet = Bullet(self.rect.centerx, self.rect.top)all_sprites.add(bullet)bullets.add(bullet)class Mob(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image = meteor_imgself.image.set_colorkey(BLACK)self.rect = self.image.get_rect()self.radius = int(self.rect.width * .85 / 2)# pygame.draw.circle(self.image, RED, self.rect.center, self.radius)self.rect.x = random.randrange(WIDTH - self.rect.width)self.rect.y = random.randrange(-100, -40)self.speedy = random.randrange(1, 8)self.speedx = random.randrange(-3, 3)def update(self):self.rect.x += self.speedxself.rect.y += self.speedyif self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 20:self.rect.x = random.randrange(WIDTH - self.rect.width)self.rect.y = random.randrange(-100, -40)self.speedy = random.randrange(1, 8)class Bullet(pygame.sprite.Sprite):def __init__(self, x, y):pygame.sprite.Sprite.__init__(self)self.image = bullet_imgself.image.set_colorkey(BLACK)self.rect = self.image.get_rect()self.rect.bottom = yself.rect.centerx = xself.speedy = -10def update(self):self.rect.y += self.speedy# kill if it moves off the top of the screenif self.rect.bottom < 0:self.kill()# Load all game graphicsbackground = pygame.image.load(path.join(img_dir, "starfield.png")).convert()background_rect = background.get_rect()player_img = pygame.image.load(path.join(img_dir, "playerShip1_orange.png")).convert()meteor_img = pygame.image.load(path.join(img_dir, "meteorBrown_med1.png")).convert()bullet_img = pygame.image.load(path.join(img_dir, "laserRed16.png")).convert()all_sprites = pygame.sprite.Group()mobs = pygame.sprite.Group()bullets = pygame.sprite.Group()player = Player()all_sprites.add(player)for i in range(8):m = Mob()all_sprites.add(m)mobs.add(m)# Game looprunning = Truewhile running:# keep loop running at the right speedclock.tick(FPS)# Process input (events)for event in pygame.event.get():# check for closing windowif event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:player.shoot()# Updateall_sprites.update()# check to see if a bullet hit a mobhits = pygame.sprite.groupcollide(mobs, bullets, True, True)for hit in hits:m = Mob()all_sprites.add(m)mobs.add(m)# check to see if a mob hit the playerhits = pygame.sprite.spritecollide(player, mobs, False, pygame.sprite.collide_circle)if hits:running = False# Draw / renderscreen.fill(BLACK)screen.blit(background, background_rect)all_sprites.draw(screen)# *after* drawing everything, flip the displaypygame.display.flip()pygame.quit()
- 信息安全三要素CIA
- 红颜祸水原来说的是她女真第一美女叶赫老女
- 第六感真的存在吗?科学家做了这些实验却无法得出明确结论
- 为什么西汉开国的第一侯刘邦要选他痛恨的张良
- 清朝的九龙夺嫡为什么没有出现过第二次
- 震惊!中国第一古尸为何会被盗墓贼丢在水沟里!
- 第二节 法律权利与法律义务
- 学编程太枯燥?分享十几个边玩游戏边学编程的网站!第五个连孩子都在用
- 揭秘谁是天下第一个冒天下之大不韪的人?
- 金庸武侠小说中的功夫排名第一名你敢练吗?