Python星际生存小游戏开发

这篇具有很好参考价值的文章主要介绍了Python星际生存小游戏开发。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

项目介绍:

项目名称:python星际生存游戏

编程语言:python

用到关键知识:pygame模块,面向对象思想,python基础等等

实现功能:

1:飞机的运动,发射子弹,飞机的生命值动态显示

2:太空中障碍物的无规则移动。障碍物的大小形态不一

3:背景音乐:发射子弹声音,飞机爆炸声音,中弹声音,背景音效等等

4:难度增加:随着得分的增高,障碍物的移动速度增快,游戏难度增大

5:累计游戏得分显示

6:该项目的可塑性(可修改行很强,可以在其基础上对其大幅度修改【如修改音乐,主角形态,背景图片,甚至可以添加一些游戏第三方补给以及敌方的生命条和多样性障碍等等】即可变成属于自己的项目)

项目分析:共计10个python类文件,代码共计800-1000行,拿来做pyagme的练习和课设是非常不错的

代码如下:

01_jugador.py

import pygame, random

WIDTH = 800
HEIGHT = 600

BLACK = (0, 0, 0)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooter")
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

all_sprites = pygame.sprite.Group()


player = Player()
all_sprites.add(player)


# Game Loop
running = True
while running:
   # Keep loop running at the right speed
   clock.tick(60)
   # Process input (events)
   for event in pygame.event.get():
      # check for closing window
      if event.type == pygame.QUIT:
         running = False
      

   # Update
   all_sprites.update()

   #Draw / Render
   screen.fill(BLACK)
   all_sprites.draw(screen)
   # *after* drawing everything, flip the display.
   pygame.display.flip()

pygame.quit()

02_enemigos.py

import pygame, random
pygame.init()
WIDTH = 800
HEIGHT = 600

BLACK = (0, 0, 0)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooter")
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

class Meteor(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/meteorGrey_med1.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.x = random.randrange(WIDTH - self.rect.width)
      self.rect.y = random.randrange(-100, -40)
      self.speedy = random.randrange(1, 10)
      self.speedx = random.randrange(-5, 5)

   def update(self):
      self.rect.x += self.speedx
      self.rect.y += self.speedy
      if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 22 :
         self.rect.x = random.randrange(WIDTH - self.rect.width)
         self.rect.y = random.randrange(-100, -40)
         self.speedy = random.randrange(1, 8)


# Cargar fondo.
background = pygame.image.load("assets/background.png").convert()


all_sprites = pygame.sprite.Group()
meteor_list = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

for i in range(8):
   meteor = Meteor()
   all_sprites.add(meteor)
   meteor_list.add(meteor)

# Game Loop
running = True
while running:
   # Keep loop running at the right speed
   clock.tick(60)
   # Process input (events)
   for event in pygame.event.get():
      # check for closing window
      if event.type == pygame.QUIT:
         running = False
      

   # Update
   all_sprites.update()




   #Draw / Render
   screen.blit(background, [0, 0])
   all_sprites.draw(screen)
   # *after* drawing everything, flip the display.
   pygame.display.flip()

pygame.quit()

03_colisiones_laser.py

import pygame, random

WIDTH = 800
HEIGHT = 600

BLACK = (0, 0, 0)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooter")
clock = pygame.time.Clock()

class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

   def shoot(self):
      bullet = Bullet(self.rect.centerx, self.rect.top)
      all_sprites.add(bullet)
      bullets.add(bullet)

class Meteor(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/meteorGrey_med1.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.x = random.randrange(WIDTH - self.rect.width)
      self.rect.y = random.randrange(-100, -40)
      self.speedy = random.randrange(1, 10)
      self.speedx = random.randrange(-5, 5)

   def update(self):
      self.rect.x += self.speedx
      self.rect.y += self.speedy
      if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 22 :
         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):
      super().__init__()
      self.image = pygame.image.load("assets/laser1.png")
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.y = y
      self.rect.centerx = x
      self.speedy = -10

   def update(self):
      self.rect.y += self.speedy
      if self.rect.bottom < 0:
         self.kill()

# Cargar fondo.
background = pygame.image.load("assets/background.png").convert()


all_sprites = pygame.sprite.Group()
meteor_list = pygame.sprite.Group()
bullets = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

for i in range(8):
   meteor = Meteor()
   all_sprites.add(meteor)
   meteor_list.add(meteor)

# Game Loop
running = True
while running:
   # Keep loop running at the right speed
   clock.tick(60)
   # Process input (events)
   for event in pygame.event.get():
      # check for closing window
      if event.type == pygame.QUIT:
         running = False
      
      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            player.shoot()

   # Update
   all_sprites.update()
   # Colisiones meteoro - laser
   hits = pygame.sprite.groupcollide(meteor_list, bullets, True, True)
   for hit in hits:
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)
      
   # Colisiones jugador - meteoro
   hits = pygame.sprite.spritecollide(player, meteor_list, False)
   if hits:
      running = False
   #Draw / Render
   screen.blit(background, [0, 0])
   all_sprites.draw(screen)
   # *after* drawing everything, flip the display.
   pygame.display.flip()

pygame.quit()

04_marcador.py

import pygame, random

WIDTH = 800
HEIGHT = 600

BLACK = (0, 0, 0)


pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooter")
clock = pygame.time.Clock()


def draw_text(surface, text, size, x, y):
   font = pygame.font.SysFont("serif", size)
   text_surface = font.render(text, True, (255, 255, 255))
   text_rect = text_surface.get_rect()
   text_rect.midtop = (x, y)
   surface.blit(text_surface, text_rect)

class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

   def shoot(self):
      bullet = Bullet(self.rect.centerx, self.rect.top)
      all_sprites.add(bullet)
      bullets.add(bullet)

class Meteor(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/meteorGrey_med1.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.x = random.randrange(WIDTH - self.rect.width)
      self.rect.y = random.randrange(-100, -40)
      self.speedy = random.randrange(1, 10)
      self.speedx = random.randrange(-5, 5)

   def update(self):
      self.rect.x += self.speedx
      self.rect.y += self.speedy
      if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 22 :
         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):
      super().__init__()
      self.image = pygame.image.load("assets/laser1.png")
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.y = y
      self.rect.centerx = x
      self.speedy = -10

   def update(self):
      self.rect.y += self.speedy
      if self.rect.bottom < 0:
         self.kill()

# Cargar fondo.
background = pygame.image.load("assets/background.png").convert()


all_sprites = pygame.sprite.Group()
meteor_list = pygame.sprite.Group()
bullets = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

for i in range(8):
   meteor = Meteor()
   all_sprites.add(meteor)
   meteor_list.add(meteor)

#Marcador / Score
score = 0

# Game Loop
running = True
while running:
   # Keep loop running at the right speed
   clock.tick(60)
   # Process input (events)
   for event in pygame.event.get():
      # check for closing window
      if event.type == pygame.QUIT:
         running = False
      
      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            player.shoot()
      

   # Update
   all_sprites.update()

   # Colisiones meteoro - laser
   hits = pygame.sprite.groupcollide(meteor_list, bullets, True, True)
   for hit in hits:
      score += 1
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)

      
   # Colisiones jugador - meteoro
   hits = pygame.sprite.spritecollide(player, meteor_list, False)
   if hits:
      running = False

   #Draw / Render
   screen.blit(background, [0, 0])
   all_sprites.draw(screen)

   # Marcador
   draw_text(screen, str(score), 25, WIDTH // 2, 10)


   pygame.display.flip()

pygame.quit()

05_meteoros.py

import pygame, random

WIDTH = 800
HEIGHT = 600

BLACK = (0, 0, 0)


pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooter")
clock = pygame.time.Clock()


def draw_text(surface, text, size, x, y):
   font = pygame.font.SysFont("serif", size)
   text_surface = font.render(text, True, (255, 255, 255))
   text_rect = text_surface.get_rect()
   text_rect.midtop = (x, y)
   surface.blit(text_surface, text_rect)

class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

   def shoot(self):
      bullet = Bullet(self.rect.centerx, self.rect.top)
      all_sprites.add(bullet)
      bullets.add(bullet)

class Meteor(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = random.choice(meteor_images)
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.x = random.randrange(WIDTH - self.rect.width)
      self.rect.y = random.randrange(-100, -40)
      self.speedy = random.randrange(1, 10)
      self.speedx = random.randrange(-5, 5)

   def update(self):
      self.rect.x += self.speedx
      self.rect.y += self.speedy
      if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 22 :
         self.rect.x = random.randrange(WIDTH - self.rect.width)

         #Change this variable
         self.rect.y = random.randrange(-150, -100)
         self.speedy = random.randrange(1, 8)

class Bullet(pygame.sprite.Sprite):
   def __init__(self, x, y):
      super().__init__()
      self.image = pygame.image.load("assets/laser1.png")
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.y = y
      self.rect.centerx = x
      self.speedy = -10

   def update(self):
      self.rect.y += self.speedy
      if self.rect.bottom < 0:
         self.kill()

meteor_images = []
meteor_list = ["assets/meteorGrey_big1.png", "assets/meteorGrey_big2.png", "assets/meteorGrey_big3.png", "assets/meteorGrey_big4.png",
            "assets/meteorGrey_med1.png", "assets/meteorGrey_med2.png", "assets/meteorGrey_small1.png", "assets/meteorGrey_small2.png",
            "assets/meteorGrey_tiny1.png", "assets/meteorGrey_tiny2.png"]
for img in meteor_list:
   meteor_images.append(pygame.image.load(img).convert())

# Cargar fondo.
background = pygame.image.load("assets/background.png").convert()


all_sprites = pygame.sprite.Group()
meteor_list = pygame.sprite.Group()
bullets = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

for i in range(8):
   meteor = Meteor()
   all_sprites.add(meteor)
   meteor_list.add(meteor)

#Marcador / Score
score = 0

# Game Loop
running = True
while running:
   # Keep loop running at the right speed
   clock.tick(60)
   # Process input (events)
   for event in pygame.event.get():
      # check for closing window
      if event.type == pygame.QUIT:
         running = False
      
      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            player.shoot()
      

   # Update
   all_sprites.update()

   # Colisiones meteoro - laser
   hits = pygame.sprite.groupcollide(meteor_list, bullets, True, True)
   for hit in hits:
      score += 1
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)

      
   # Colisiones jugador - meteoro
   hits = pygame.sprite.spritecollide(player, meteor_list, False)
   if hits:
      running = False

   #Draw / Render
   screen.blit(background, [0, 0])
   all_sprites.draw(screen)

   # Marcador
   draw_text(screen, str(score), 25, WIDTH // 2, 10)


   pygame.display.flip()

pygame.quit()

06_sonidos_musica.py

import pygame, random

WIDTH = 800
HEIGHT = 600

BLACK = (0, 0, 0)


pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooter")
clock = pygame.time.Clock()


def draw_text(surface, text, size, x, y):
   font = pygame.font.SysFont("serif", size)
   text_surface = font.render(text, True, (255, 255, 255))
   text_rect = text_surface.get_rect()
   text_rect.midtop = (x, y)
   surface.blit(text_surface, text_rect)

class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

   def shoot(self):
      bullet = Bullet(self.rect.centerx, self.rect.top)
      all_sprites.add(bullet)
      bullets.add(bullet)

      #Agregamos sonido
      laser_sound.play()

class Meteor(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = random.choice(meteor_images)
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.x = random.randrange(WIDTH - self.rect.width)
      self.rect.y = random.randrange(-100, -40)
      self.speedy = random.randrange(1, 10)
      self.speedx = random.randrange(-5, 5)

   def update(self):
      self.rect.x += self.speedx
      self.rect.y += self.speedy
      if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 22 :
         self.rect.x = random.randrange(WIDTH - self.rect.width)

         #Change this variable
         self.rect.y = random.randrange(-150, -100)
         self.speedy = random.randrange(1, 8)

class Bullet(pygame.sprite.Sprite):
   def __init__(self, x, y):
      super().__init__()
      self.image = pygame.image.load("assets/laser1.png")
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.y = y
      self.rect.centerx = x
      self.speedy = -10

   def update(self):
      self.rect.y += self.speedy
      if self.rect.bottom < 0:
         self.kill()

meteor_images = []
meteor_list = ["assets/meteorGrey_big1.png", "assets/meteorGrey_big2.png", "assets/meteorGrey_big3.png", "assets/meteorGrey_big4.png",
            "assets/meteorGrey_med1.png", "assets/meteorGrey_med2.png", "assets/meteorGrey_small1.png", "assets/meteorGrey_small2.png",
            "assets/meteorGrey_tiny1.png", "assets/meteorGrey_tiny2.png"]
for img in meteor_list:
   meteor_images.append(pygame.image.load(img).convert())

# Cargar fondo.
background = pygame.image.load("assets/background.png").convert()

# Cargar sonidos
laser_sound = pygame.mixer.Sound("assets/laser5.org")
explosion_sound = pygame.mixer.Sound("assets/explosion.wav")
pygame.mixer.music.load("assets/music.org")
pygame.mixer.music.set_volume(0.1)

all_sprites = pygame.sprite.Group()
meteor_list = pygame.sprite.Group()
bullets = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

for i in range(8):
   meteor = Meteor()
   all_sprites.add(meteor)
   meteor_list.add(meteor)

#Marcador / Score
score = 0
pygame.mixer.music.play(loops=-1)

# Game Loop
running = True
while running:
   # Keep loop running at the right speed
   clock.tick(60)
   # Process input (events)
   for event in pygame.event.get():
      # check for closing window
      if event.type == pygame.QUIT:
         running = False
      
      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            player.shoot()
      

   # Update
   all_sprites.update()

   # Colisiones meteoro - laser
   hits = pygame.sprite.groupcollide(meteor_list, bullets, True, True)
   for hit in hits:
      score += 1
      explosion_sound.play()
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)

      
   # Colisiones jugador - meteoro
   hits = pygame.sprite.spritecollide(player, meteor_list, False)
   if hits:
      running = False

   #Draw / Render
   screen.blit(background, [0, 0])
   all_sprites.draw(screen)

   # Marcador
   draw_text(screen, str(score), 25, WIDTH // 2, 10)


   pygame.display.flip()

pygame.quit()

07_escudos.py

import pygame, random

WIDTH = 800
HEIGHT = 600

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)


pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooter")
clock = pygame.time.Clock()


def draw_text(surface, text, size, x, y):
   font = pygame.font.SysFont("serif", size)
   text_surface = font.render(text, True, (255, 255, 255))
   text_rect = text_surface.get_rect()
   text_rect.midtop = (x, y)
   surface.blit(text_surface, text_rect)

def draw_shield_bar(surface, x, y, percentage):
   BAR_LENGHT = 100
   BAR_HEIGHT = 10
   fill = (percentage / 100) * BAR_LENGHT
   border = pygame.Rect(x, y, BAR_LENGHT, BAR_HEIGHT)
   fill = pygame.Rect(x, y, fill, BAR_HEIGHT)
   pygame.draw.rect(surface, GREEN, fill)
   pygame.draw.rect(surface, WHITE, border, 2)


class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0

      self.shield = 100

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

   def shoot(self):
      bullet = Bullet(self.rect.centerx, self.rect.top)
      all_sprites.add(bullet)
      bullets.add(bullet)

      #Agregamos sonido
      #laser_sound.play()

class Meteor(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = random.choice(meteor_images)
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.x = random.randrange(WIDTH - self.rect.width)
      self.rect.y = random.randrange(-100, -40)
      self.speedy = random.randrange(1, 10)
      self.speedx = random.randrange(-5, 5)

   def update(self):
      self.rect.x += self.speedx
      self.rect.y += self.speedy
      if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 22 :
         self.rect.x = random.randrange(WIDTH - self.rect.width)

         #Change this variable
         self.rect.y = random.randrange(-150, -100)
         self.speedy = random.randrange(1, 8)

class Bullet(pygame.sprite.Sprite):
   def __init__(self, x, y):
      super().__init__()
      self.image = pygame.image.load("assets/laser1.png")
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.y = y
      self.rect.centerx = x
      self.speedy = -10

   def update(self):
      self.rect.y += self.speedy
      if self.rect.bottom < 0:
         self.kill()

meteor_images = []
meteor_list = ["assets/meteorGrey_big1.png", "assets/meteorGrey_big2.png", "assets/meteorGrey_big3.png", "assets/meteorGrey_big4.png",
            "assets/meteorGrey_med1.png", "assets/meteorGrey_med2.png", "assets/meteorGrey_small1.png", "assets/meteorGrey_small2.png",
            "assets/meteorGrey_tiny1.png", "assets/meteorGrey_tiny2.png"]
for img in meteor_list:
   meteor_images.append(pygame.image.load(img).convert())

# Cargar fondo.
background = pygame.image.load("assets/background.png").convert()

# Cargar sonidos
laser_sound = pygame.mixer.Sound("assets/laser5.org")
explosion_sound = pygame.mixer.Sound("assets/explosion.wav")
pygame.mixer.music.load("assets/music.org")
pygame.mixer.music.set_volume(0.1)

all_sprites = pygame.sprite.Group()
meteor_list = pygame.sprite.Group()
bullets = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

for i in range(8):
   meteor = Meteor()
   all_sprites.add(meteor)
   meteor_list.add(meteor)

#Marcador / Score
score = 0
#pygame.mixer.music.play(loops=-1)

# Game Loop
running = True
while running:
   # Keep loop running at the right speed
   clock.tick(60)
   # Process input (events)
   for event in pygame.event.get():
      # check for closing window
      if event.type == pygame.QUIT:
         running = False
      
      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            player.shoot()
      

   # Update
   all_sprites.update()

   # Colisiones meteoro - laser
   hits = pygame.sprite.groupcollide(meteor_list, bullets, True, True)
   for hit in hits:
      score += 1
      #explosion_sound.play()
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)

      
   # Colisiones jugador - meteoro


   ################## CHANGES HERE ################################
   hits = pygame.sprite.spritecollide(player, meteor_list, True) # Change here
   for hit in hits:
      player.shield -= 25
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)
      if player.shield <= 0:
         running = False

   #Draw / Render
   screen.blit(background, [0, 0])
   all_sprites.draw(screen)

   # Marcador
   draw_text(screen, str(score), 25, WIDTH // 2, 10)

   # ESCUDO.
   draw_shield_bar(screen, 5, 5, player.shield)


   pygame.display.flip()

pygame.quit()

08_explosions.py

import pygame, random

WIDTH = 800
HEIGHT = 600

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)


pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooter")
clock = pygame.time.Clock()


def draw_text(surface, text, size, x, y):
   font = pygame.font.SysFont("serif", size)
   text_surface = font.render(text, True, (255, 255, 255))
   text_rect = text_surface.get_rect()
   text_rect.midtop = (x, y)
   surface.blit(text_surface, text_rect)

def draw_shield_bar(surface, x, y, percentage):
   BAR_LENGHT = 100
   BAR_HEIGHT = 10
   fill = (percentage / 100) * BAR_LENGHT
   border = pygame.Rect(x, y, BAR_LENGHT, BAR_HEIGHT)
   fill = pygame.Rect(x, y, fill, BAR_HEIGHT)
   pygame.draw.rect(surface, GREEN, fill)
   pygame.draw.rect(surface, WHITE, border, 2)


class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0

      self.shield = 100

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

   def shoot(self):
      bullet = Bullet(self.rect.centerx, self.rect.top)
      all_sprites.add(bullet)
      bullets.add(bullet)

      #Agregamos sonido
      #laser_sound.play()

class Meteor(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = random.choice(meteor_images)
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.x = random.randrange(WIDTH - self.rect.width)
      self.rect.y = random.randrange(-100, -40)
      self.speedy = random.randrange(1, 10)
      self.speedx = random.randrange(-5, 5)

   def update(self):
      self.rect.x += self.speedx
      self.rect.y += self.speedy
      if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 22 :
         self.rect.x = random.randrange(WIDTH - self.rect.width)

         #Change this variable
         self.rect.y = random.randrange(-150, -100)
         self.speedy = random.randrange(1, 8)

class Bullet(pygame.sprite.Sprite):
   def __init__(self, x, y):
      super().__init__()
      self.image = pygame.image.load("assets/laser1.png")
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.y = y
      self.rect.centerx = x
      self.speedy = -10

   def update(self):
      self.rect.y += self.speedy
      if self.rect.bottom < 0:
         self.kill()

class Explosion(pygame.sprite.Sprite):
   def __init__(self, center):
      super().__init__()
      self.image = explosion_anim[0]
      self.rect = self.image.get_rect()
      self.rect.center = center
      self.frame = 0
      self.last_update = pygame.time.get_ticks()
      self.frame_rate = 50 # how long to wait for the next frame VELOCITY OF THE EXPLOSION

   def update(self):
      now = pygame.time.get_ticks()
      if now - self.last_update > self.frame_rate:
         self.last_update = now
         self.frame += 1
         if self.frame == len(explosion_anim):
            self.kill() # if we get to the end of the animation we don't keep going.
         else:
            center = self.rect.center
            self.image = explosion_anim[self.frame]
            self.rect = self.image.get_rect()
            self.rect.center = center

meteor_images = []
meteor_list = ["assets/meteorGrey_big1.png", "assets/meteorGrey_big2.png", "assets/meteorGrey_big3.png", "assets/meteorGrey_big4.png",
            "assets/meteorGrey_med1.png", "assets/meteorGrey_med2.png", "assets/meteorGrey_small1.png", "assets/meteorGrey_small2.png",
            "assets/meteorGrey_tiny1.png", "assets/meteorGrey_tiny2.png"]
for img in meteor_list:
   meteor_images.append(pygame.image.load(img).convert())

## --------------- CARGAR IMAGENES EXPLOSIÓN -------------------------- ##
explosion_anim = []
for i in range(9):
   file = "assets/regularExplosion0{}.png".format(i)
   img = pygame.image.load(file).convert()
   img.set_colorkey(BLACK)
   img_scale = pygame.transform.scale(img, (70, 70))
   explosion_anim.append(img_scale)


# Cargar fondo.
background = pygame.image.load("assets/background.png").convert()

# Cargar sonidos
laser_sound = pygame.mixer.Sound("assets/laser5.org")
explosion_sound = pygame.mixer.Sound("assets/explosion.wav")
pygame.mixer.music.load("assets/music.org")
pygame.mixer.music.set_volume(0.1)

all_sprites = pygame.sprite.Group()
meteor_list = pygame.sprite.Group()
bullets = pygame.sprite.Group()

player = Player()
all_sprites.add(player)

for i in range(8):
   meteor = Meteor()
   all_sprites.add(meteor)
   meteor_list.add(meteor)

#Marcador / Score
score = 0
#pygame.mixer.music.play(loops=-1)

# Game Loop
running = True
while running:
   # Keep loop running at the right speed
   clock.tick(60)
   # Process input (events)
   for event in pygame.event.get():
      # check for closing window
      if event.type == pygame.QUIT:
         running = False
      
      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            player.shoot()
      

   # Update
   all_sprites.update()

   # Colisiones meteoro - laser
   hits = pygame.sprite.groupcollide(meteor_list, bullets, True, True)
   for hit in hits:
      score += 1
      #explosion_sound.play()
      explosion = Explosion(hit.rect.center)
      all_sprites.add(explosion)

      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)

      
   # Colisiones jugador - meteoro


   ################## CHANGES HERE ################################
   hits = pygame.sprite.spritecollide(player, meteor_list, True) # Change here
   for hit in hits:
      player.shield -= 25
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)
      if player.shield <= 0:
         running = False

   #Draw / Render
   screen.blit(background, [0, 0])
   all_sprites.draw(screen)

   # Marcador
   draw_text(screen, str(score), 25, WIDTH // 2, 10)

   # ESCUDO.
   draw_shield_bar(screen, 5, 5, player.shield)


   pygame.display.flip()

pygame.quit()

09_gameover.py

import pygame, random

WIDTH = 800
HEIGHT = 600

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)


pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shooter")
clock = pygame.time.Clock()


def draw_text(surface, text, size, x, y):
   font = pygame.font.SysFont("serif", size)
   text_surface = font.render(text, True, (255, 255, 255))
   text_rect = text_surface.get_rect()
   text_rect.midtop = (x, y)
   surface.blit(text_surface, text_rect)

def draw_shield_bar(surface, x, y, percentage):
   BAR_LENGHT = 100
   BAR_HEIGHT = 10
   fill = (percentage / 100) * BAR_LENGHT
   border = pygame.Rect(x, y, BAR_LENGHT, BAR_HEIGHT)
   fill = pygame.Rect(x, y, fill, BAR_HEIGHT)
   pygame.draw.rect(surface, GREEN, fill)
   pygame.draw.rect(surface, WHITE, border, 2)


class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0

      self.shield = 100

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

   def shoot(self):
      bullet = Bullet(self.rect.centerx, self.rect.top)
      all_sprites.add(bullet)
      bullets.add(bullet)

      #Agregamos sonido
      #laser_sound.play()

class Meteor(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = random.choice(meteor_images)
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.x = random.randrange(WIDTH - self.rect.width)
      self.rect.y = random.randrange(-100, -40)
      self.speedy = random.randrange(1, 10)
      self.speedx = random.randrange(-5, 5)

   def update(self):
      self.rect.x += self.speedx
      self.rect.y += self.speedy
      if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 22 :
         self.rect.x = random.randrange(WIDTH - self.rect.width)

         #Change this variable
         self.rect.y = random.randrange(-150, -100)
         self.speedy = random.randrange(1, 8)

class Bullet(pygame.sprite.Sprite):
   def __init__(self, x, y):
      super().__init__()
      self.image = pygame.image.load("assets/laser1.png")
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.y = y
      self.rect.centerx = x
      self.speedy = -10

   def update(self):
      self.rect.y += self.speedy
      if self.rect.bottom < 0:
         self.kill()

class Explosion(pygame.sprite.Sprite):
   def __init__(self, center):
      super().__init__()
      self.image = explosion_anim[0]
      self.rect = self.image.get_rect()
      self.rect.center = center
      self.frame = 0
      self.last_update = pygame.time.get_ticks()
      self.frame_rate = 50 # how long to wait for the next frame VELOCITY OF THE EXPLOSION

   def update(self):
      now = pygame.time.get_ticks()
      if now - self.last_update > self.frame_rate:
         self.last_update = now
         self.frame += 1
         if self.frame == len(explosion_anim):
            self.kill() # if we get to the end of the animation we don't keep going.
         else:
            center = self.rect.center
            self.image = explosion_anim[self.frame]
            self.rect = self.image.get_rect()
            self.rect.center = center


def show_go_screen():
   screen.blit(background, [0, 0])
   draw_text(screen, "SHOOTER", 65, WIDTH // 2, HEIGHT / 4)
   draw_text(screen, "(Instructions)", 27, WIDTH // 2, HEIGHT // 2)
   draw_text(screen, "Press key to begin", 17, WIDTH // 2, HEIGHT * 3/4)
   pygame.display.flip()
   waiting = True
   while waiting:
      clock.tick(60)
      for event in pygame.event.get():
         if event.type == pygame.QUIT:
            pygame.quit()
         if event.type == pygame.KEYUP:
            waiting = False

meteor_images = []
meteor_list = ["assets/meteorGrey_big1.png", "assets/meteorGrey_big2.png", "assets/meteorGrey_big3.png", "assets/meteorGrey_big4.png",
            "assets/meteorGrey_med1.png", "assets/meteorGrey_med2.png", "assets/meteorGrey_small1.png", "assets/meteorGrey_small2.png",
            "assets/meteorGrey_tiny1.png", "assets/meteorGrey_tiny2.png"]
for img in meteor_list:
   meteor_images.append(pygame.image.load(img).convert())

## --------------- CARGAR IMAGENES EXPLOSIÓN -------------------------- ##
explosion_anim = []
for i in range(9):
   file = "assets/regularExplosion0{}.png".format(i)
   img = pygame.image.load(file).convert()
   img.set_colorkey(BLACK)
   img_scale = pygame.transform.scale(img, (70, 70))
   explosion_anim.append(img_scale)


# Cargar fondo.
background = pygame.image.load("assets/background.png").convert()

# Cargar sonidos
laser_sound = pygame.mixer.Sound("assets/laser5.org")
explosion_sound = pygame.mixer.Sound("assets/explosion.wav")
pygame.mixer.music.load("assets/music.org")
pygame.mixer.music.set_volume(0.1)


#pygame.mixer.music.play(loops=-1)

# Game Loop
game_over = True
running = True
while running:
   if game_over:
      show_go_screen()
      game_over = False
      all_sprites = pygame.sprite.Group()
      meteor_list = pygame.sprite.Group()
      bullets = pygame.sprite.Group()

      player = Player()
      all_sprites.add(player)

      for i in range(8):
         meteor = Meteor()
         all_sprites.add(meteor)
         meteor_list.add(meteor)

      #Marcador / Score
      score = 0
   # Keep loop running at the right speed
   clock.tick(60)
   # Process input (events)
   for event in pygame.event.get():
      # check for closing window
      if event.type == pygame.QUIT:
         running = False
      
      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            player.shoot()
      

   # Update
   all_sprites.update()

   # Colisiones meteoro - laser
   hits = pygame.sprite.groupcollide(meteor_list, bullets, True, True)
   for hit in hits:
      score += 1
      #explosion_sound.play()
      explosion = Explosion(hit.rect.center)
      all_sprites.add(explosion)

      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)

      
   # Colisiones jugador - meteoro


   ################## CHANGES HERE ################################
   hits = pygame.sprite.spritecollide(player, meteor_list, True) # Change here
   for hit in hits:
      player.shield -= 25
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)
      if player.shield <= 0:
         #running = False


         game_over = True

   #Draw / Render
   screen.blit(background, [0, 0])
   all_sprites.draw(screen)

   # Marcador
   draw_text(screen, str(score), 25, WIDTH // 2, 10)

   # ESCUDO.
   draw_shield_bar(screen, 5, 5, player.shield)


   pygame.display.flip()

pygame.quit()

shooter.py

import pygame, random
pygame.init()
WIDTH = 800
HEIGHT = 600
BLACK = (0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = (0, 255, 0)

pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("星际生存")
clock = pygame.time.Clock()

def draw_text(surface, text, size, x, y):
   font = pygame.font.SysFont("serif", size)
   text_surface = font.render(text, True, WHITE)
   text_rect = text_surface.get_rect()
   text_rect.midtop = (x, y)
   surface.blit(text_surface, text_rect)

def draw_shield_bar(surface, x, y, percentage):
   BAR_LENGHT = 100
   BAR_HEIGHT = 10
   fill = (percentage / 100) * BAR_LENGHT
   border = pygame.Rect(x, y, BAR_LENGHT, BAR_HEIGHT)
   fill = pygame.Rect(x, y, fill, BAR_HEIGHT)
   pygame.draw.rect(surface, GREEN, fill)
   pygame.draw.rect(surface, WHITE, border, 2)

class Player(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = pygame.image.load("assets/player.png").convert()
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.centerx = WIDTH // 2
      self.rect.bottom = HEIGHT - 10
      self.speed_x = 0
      self.shield = 100

   def update(self):
      self.speed_x = 0
      keystate = pygame.key.get_pressed()
      if keystate[pygame.K_LEFT]:
         self.speed_x = -5
      if keystate[pygame.K_RIGHT]:
         self.speed_x = 5
      self.rect.x += self.speed_x
      if self.rect.right > WIDTH:
         self.rect.right = WIDTH
      if self.rect.left < 0:
         self.rect.left = 0

   def shoot(self):
      bullet = Bullet(self.rect.centerx, self.rect.top)
      all_sprites.add(bullet)
      bullets.add(bullet)
      #laser_sound.play()

class Meteor(pygame.sprite.Sprite):
   def __init__(self):
      super().__init__()
      self.image = random.choice(meteor_images)
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.x = random.randrange(WIDTH - self.rect.width)
      self.rect.y = random.randrange(-140, -100)
      self.speedy = random.randrange(1, 10)
      self.speedx = random.randrange(-5, 5)

   def update(self):
      self.rect.y += self.speedy
      self.rect.x += self.speedx
      if self.rect.top > HEIGHT + 10 or self.rect.left < -40 or self.rect.right > WIDTH + 40:
         self.rect.x = random.randrange(WIDTH - self.rect.width)
         self.rect.y = random.randrange(-140, - 100)
         self.speedy = random.randrange(1, 10)

class Bullet(pygame.sprite.Sprite):
   def __init__(self, x, y):
      super().__init__()
      self.image = pygame.image.load("assets/laser1.png")
      self.image.set_colorkey(BLACK)
      self.rect = self.image.get_rect()
      self.rect.y = y
      self.rect.centerx = x
      self.speedy = -10

   def update(self):
      self.rect.y += self.speedy
      if self.rect.bottom < 0:
         self.kill()

class Explosion(pygame.sprite.Sprite):
   def __init__(self, center):
      super().__init__()
      self.image = explosion_anim[0]
      self.rect = self.image.get_rect()
      self.rect.center = center 
      self.frame = 0
      self.last_update = pygame.time.get_ticks()
      self.frame_rate = 50 # VELOCIDAD DE LA EXPLOSION

   def update(self):
      now = pygame.time.get_ticks()
      if now - self.last_update > self.frame_rate:
         self.last_update = now
         self.frame += 1
         if self.frame == len(explosion_anim):
            self.kill()
         else:
            center = self.rect.center
            self.image = explosion_anim[self.frame]
            self.rect = self.image.get_rect()
            self.rect.center = center


def show_go_screen():
   screen.blit(background, [0,0])
   draw_text(screen, "SHOOTER", 65, WIDTH // 2, HEIGHT // 4)
   draw_text(screen, "Instruciones van aquí", 27, WIDTH // 2, HEIGHT // 2)
   draw_text(screen, "Press Key", 20, WIDTH // 2, HEIGHT * 3/4)
   pygame.display.flip()
   waiting = True
   while waiting:
      clock.tick(60)
      for event in pygame.event.get():
         if event.type == pygame.QUIT:
            pygame.quit()
         if event.type == pygame.KEYUP:
            waiting = False


meteor_images = []
meteor_list = ["assets/meteorGrey_big1.png", "assets/meteorGrey_big2.png", "assets/meteorGrey_big3.png", "assets/meteorGrey_big4.png",
            "assets/meteorGrey_med1.png", "assets/meteorGrey_med2.png", "assets/meteorGrey_small1.png", "assets/meteorGrey_small2.png",
            "assets/meteorGrey_tiny1.png", "assets/meteorGrey_tiny2.png"]
for img in meteor_list:
   meteor_images.append(pygame.image.load(img).convert())


####----------------EXPLOSTION IMAGENES --------------
explosion_anim = []
for i in range(9):
   file = "assets/regularExplosion0{}.png".format(i)
   img = pygame.image.load(file).convert()
   img.set_colorkey(BLACK)
   img_scale = pygame.transform.scale(img, (70,70))
   explosion_anim.append(img_scale)

# Cargar imagen de fondo
background = pygame.image.load("assets/background.png").convert()

# Cargar sonidos
laser_sound = pygame.mixer.Sound("assets/laser5.org")
explosion_sound = pygame.mixer.Sound("assets/explosion.wav")
pygame.mixer.music.load("assets/music.org")
pygame.mixer.music.set_volume(0.2)


#pygame.mixer.music.play(loops=-1)

#### ----------GAME OVER
game_over = True
running = True
while running:
   if game_over:
      show_go_screen()
      game_over = False
      all_sprites = pygame.sprite.Group()
      meteor_list = pygame.sprite.Group()
      bullets = pygame.sprite.Group()

      player = Player()
      all_sprites.add(player)
      for i in range(8):
         meteor = Meteor()
         all_sprites.add(meteor)
         meteor_list.add(meteor)
      score = 0


   clock.tick(60)
   for event in pygame.event.get():
      if event.type == pygame.QUIT:
         running = False

      elif event.type == pygame.KEYDOWN:
         if event.key == pygame.K_SPACE:
            player.shoot()


   all_sprites.update()

   #colisiones - meteoro - laser
   hits = pygame.sprite.groupcollide(meteor_list, bullets, True, True)
   for hit in hits:
      score += 10
      #explosion_sound.play()
      explosion = Explosion(hit.rect.center)
      all_sprites.add(explosion)
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)

   # Checar colisiones - jugador - meteoro
   hits = pygame.sprite.spritecollide(player, meteor_list, True)
   for hit in hits:
      player.shield -= 25
      meteor = Meteor()
      all_sprites.add(meteor)
      meteor_list.add(meteor)
      if player.shield <= 0:
         game_over = True

   screen.blit(background, [0, 0])

   all_sprites.draw(screen)

   #Marcador
   draw_text(screen, str(score), 25, WIDTH // 2, 10)

   # Escudo.
   draw_shield_bar(screen, 5, 5, player.shield)

   pygame.display.flip()
pygame.quit()

项目运行截图:

Python星际生存小游戏开发

Python星际生存小游戏开发

Python星际生存小游戏开发 

Python星际生存小游戏开发 

 文章来源地址https://www.toymoban.com/news/detail-463326.html

需要源代码项目的同学可以私信我,项目写的比较早了,可能有些函数方法写的并不是很完美简洁还请多多包涵。 

 

到了这里,关于Python星际生存小游戏开发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • python项目分享 - python坦克大战小游戏

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 坦克大战小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取:

    2024年02月03日
    浏览(47)
  • python项目分享 python走迷宫小游戏

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 走迷宫小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: htt

    2024年01月16日
    浏览(40)
  • python项目分享 - python走迷宫小游戏

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 走迷宫小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: htt

    2024年02月02日
    浏览(47)
  • python项目分享 python打地鼠小游戏

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 打地鼠小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: htt

    2024年02月03日
    浏览(60)
  • python项目分享 - python五子棋小游戏

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 五子棋小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: htt

    2024年02月03日
    浏览(61)
  • python项目分享 python五子棋小游戏

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 五子棋小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: htt

    2024年02月03日
    浏览(47)
  • python小游戏开发(飞机大战)

    目录 一:背景       1. pygame 模块初识 二.项目设计内容 开发一款飞机大战游戏,要求: (1)实现飞机的移动、子弹发射,对本次游戏有一个初步的编写及认识。 (2)飞机的持续按键移动和飞机自爆优化。 (3)进行基类的抽取,使代码更具有层次性和简化重复代码。 (

    2024年02月16日
    浏览(49)
  • 【Java项目介绍和界面搭建】拼图小游戏完结——源代码分析以及资料上传

    目录 以后工作的时候拿到一个需求之后: 1,所需要的技术点 2,分析业务逻辑 项目实现步骤: 添加组件 绑定事件: 分析业务逻辑 分析所需要的技术点 在这个界面中,我们需要哪些技术点: 整个的菜单就是JMenuBar 功能,关于我们:JMenu 更换图片:JMenu 重新游戏,重新登录

    2024年03月11日
    浏览(63)
  • python项目分享 - python外星人入侵小游戏

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 外星人入侵小游戏设计与实现 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: https://gitee.com/sinonfin/s

    2024年02月03日
    浏览(56)
  • python项目分享 扫雷小游戏设计与实现 (源码)

    🔥 Hi,各位同学好呀,这里是L学长! 🥇今天向大家分享一个今年(2022)最新完成的毕业设计项目作品 python小游戏毕设 扫雷小游戏设计与实现 (源码) 🥇 学长根据实现的难度和等级对项目进行评分(最低0分,满分5分) 难度系数:3分 工作量:3分 创新点:4分 项目获取: https:

    2024年01月19日
    浏览(59)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包