Python星际生存小游戏开发

news2024/12/24 4:01:24

项目介绍:

项目名称: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()

项目运行截图:

 

 

 

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

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/565953.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

深入探索Apache Flume:大数据领域的数据采集神器【上进小菜猪大数据系列】

&#x1f4ec;&#x1f4ec;我是上进小菜猪&#xff0c;沈工大软件工程专业&#xff0c;爱好敲代码&#xff0c;持续输出干货&#xff0c;欢迎关注。 引言&#xff1a; 随着大数据技术的快速发展&#xff0c;企业和组织需要从各种来源采集海量数据。数据采集是大数据处理流程中…

DTO、VO、BO、PO、DO理解等相关的转换

1、什么是DTO、VO、BO、PO、DO、POJO pojo(domain/entity)一般定义实体类&#xff0c;实体类又被分为VO、BO、 PO、 DTO、DO&#xff1b;通过各层POJO的使用&#xff0c;有助于提高代码的可读性和可维护性。 2、阿里巴巴Java开发规范 DO(Data Object):此对象与数据库表结构一…

chatgpt赋能python:Python的BeautifulSoup库和find_all()方法

Python的Beautiful Soup库和find_all()方法 在Web爬虫中&#xff0c;我们需要从网页中找到特定的HTML标记或属性&#xff0c;以便提取我们需要的数据。对于Python开发人员而言&#xff0c;Beautiful Soup是最流行的解析HTML和XML的库之一。该库可以让我们轻松地从HTML解析器中…

ECMAScript 6 新特性详解

目录 ECMAScript 6 简介 1、箭头函数 2、类 3、增强对象字面量 4、模板字符串 5、解构赋值 6、默认参数、剩余参数、展开操作符 7、let、const 8、迭代器&#xff08;迭代器&#xff09;、for of 9、Generators&#xff08;生成器&#xff09; 10、Unicode 11、模块…

GPC_APDU_Transport_over_SPI-I2C_v1.0_PublicRelease

GPC_APDU_Transport_over_SPI-I2C_v1.0_PublicRelease.pdf 目录 1 简介 越来越多的设备&#xff0c;如移动设备、可穿戴设备或其他 IoT&#xff08;物联网&#xff09;设备现在正在使用焊接安全元件 (SE)。 这产生了支持 SPI 或 I2C 等物理接口的新需求&#xff0c;以代替以前…

高阶python | 字符串高级功能

不可变的字符串 python中的数据类型分为可变的和不可变的两种&#xff0c;字符串属于不可变的数据类型。 不可变的数据类型可以作为字典的键使用&#xff0c;例如&#xff1a; my_dict {Lily: 68, Harry: 98, Lucy: 79} 不可变类型的另一个优点就是有助于进行内部消化&…

ISO_IEC_7816-3

介绍 ISO/IEC 7816 是一系列标准&#xff0c;规定了集成电路卡和此类卡的使用 互换。 这些卡是用于在外部世界和卡中的集成电路之间协商的信息交换的识别卡。 作为信息交换的结果&#xff0c;卡传递信息&#xff08;计算结果、存储的数据&#xff09;和/或修改其内容&#xff0…

高级第一个月考试题

1.什么是Vue框架&#xff1f; Vue是一套用于构建用户界面的渐进式框架。与其它大型框架不同的是&#xff0c;Vue 被设计为可以自底向上逐层应用。Vue 的核心库只关注视图层&#xff0c;并且还便于与第三方库或既有项目整合。另一方面&#xff0c;当与现代化的工具链以及各种支持…

Unity之ShaderGraph节点介绍 Artistic艺术效果

前言 Artistic&#xff08;艺术效果&#xff09;  1、Adjustments&#xff08;图像调节&#xff09;   1) Chennel Mixer&#xff08;通道混合器&#xff09;   2) Contrast&#xff08;对比度&#xff09;   3) Hue&#xff08;色调偏移&#xff09;   4) Invert Co…

layui框架学习(24:弹出层模块_消息框提示框)

弹出层模块layer是Layui的重要模块&#xff0c;layui官网教程中的原话是&#xff1a;“layer 作为 Layui 的代表性组件”。弹出层模块layer的作用主要是在页面中以消息框、弹出框等形式进行信息提醒、信息交互等操作&#xff0c;类似于C/S架构中的MessageBox.Show、自定义窗口的…

AcrelCloud-6000安全用电云平台在某景区的应用

摘要 我国历史文化悠久&#xff0c;拥有相当丰富的历史文化遗产&#xff0c;而古建筑作为文化遗产的重要组成部分&#xff0c;体现着中华民族的生命力和创造力&#xff0c;蕴含了丰富的物质价值和精神价值。根据国家文物局近些年公布的数据&#xff0c;每年都发生十多起文物建筑…

ThingsBoard教程(五二):规则节点解析 AWS SNS Node, AWS SQS Node

AWS SNS Node Since TB Version 2.0 节点将消息发布到AWS SNS(亚马逊简单通知服务)。 配置: 主题ARN模式 - 可以直接设置消息发布的主题名称,也可以使用模式,该模式将使用消息元数据解析为实际的ARN主题名称。 AWS Access Key ID和AWS Secret Access Key是具有编程访问…

chatgpt赋能python:PythonUSBKey:安全可靠的数字身份验证

Python USBKey&#xff1a;安全可靠的数字身份验证 介绍 Python USBKey是一种安全可靠的数字身份验证工具&#xff0c;它基于Python编程语言开发&#xff0c;便于跨平台使用&#xff0c;并支持多种加密算法&#xff0c;使得加密安全性更高。 Python USBKey能够保护您的隐私&…

C++中string的用法

博主简介&#xff1a;Hello大家好呀&#xff0c;我是陈童学&#xff0c;一个与你一样正在慢慢前行的人。 博主主页&#xff1a;陈童学哦 所属专栏&#xff1a;CSTL 前言&#xff1a;Hello各位小伙伴们好&#xff01;欢迎来到本专栏CSTL的学习&#xff0c;本专栏旨在帮助大家了解…

chatgpt赋能python:Pythonthreading:什么是线程及其使用

Python threading&#xff1a;什么是线程及其使用 在计算机科学中&#xff0c;线程是指操作系统能够进行调度和分派的最小单位。在 Python 中&#xff0c;线程允许程序在执行过程中并行完成多个任务。线程是并发编程的核心元素之一&#xff0c;使开发人员能够轻松地编写并行代…

Golang中互斥锁和读写互斥锁

目录 互斥锁 使用互斥锁的示例代码 读写互斥锁 读写互斥锁的示例代码 互斥锁 在Golang中&#xff0c;互斥锁&#xff08;Mutex&#xff09;是一种基本的同步原语&#xff0c;用于实现对共享资源的互斥访问。互斥锁通过在代码中标记临界区来控制对共享资源的访问&#xff0c…

LC-3 机器码编程实验

一、实验目的 分析和理解试验指定的需解决问题。利用LC-3的机器代码设计实现相关程序。通过LC-3仿真器调试和运行相关程序并得到正确的结果。 二、实验内容 利用LC-3的机器代码计算一个16位的字中有多少位是“1”&#xff0c;程序从x3000开始&#xff0c;需计算的字存储在x3…

智能集成式电力电容器在山东某环保材料制造厂中的应用

摘要&#xff1a;分析智能集成式电力电容的工作原理及功能&#xff0c;结合山东环保材料制造厂配电现状&#xff0c;选择经济可靠的方案&#xff0c;智能电容过零投切与低功耗&#xff0c;解决了继电器投切产生涌流的问题&#xff1b;接线简单&#xff0c;扩容方便&#xff0c;…

chatgpt赋能python:Python实现Word合并

Python实现Word合并 在日常工作和生活中&#xff0c;我们经常需要处理Word文档。有一种情况是需要将多个Word文档合并成一个&#xff0c;并且保留原有的格式和样式。这个时候&#xff0c;Python就派上用场了。 Word文档的结构 在了解怎样合并多个Word文档之前&#xff0c;我…