Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import sys
- import pygame
- import math
- from pygame.locals import *
- FPS = 60.0
- SCREEN_SIZE = (1280, 720)
- class Colors:
- BLACK = (0, 0, 0)
- BLUE = (0, 191, 255)
- class Astronaut:
- def __init__(self):
- image = pygame.image.load('astronaut.png')
- astronaut_scale = (1 / 4)
- scale_tuple = (int(image.get_width() * astronaut_scale), int(image.get_height() * astronaut_scale))
- self.image = pygame.transform.scale(image, scale_tuple)
- self.position = (100, 350)
- self.velocity = (0, 0)
- def move(self, right, down):
- speed = 10
- vx = self.velocity[0]
- vy = self.velocity[1]
- vx += right * speed
- vy += down * speed
- self.velocity = (vx, vy)
- def update(self):
- vx = self.velocity[0]
- vy = self.velocity[1]
- # Inertia
- k = 0.45
- t = 1 / FPS
- vx *= math.exp(- k * t)
- vy *= math.exp(- k * t)
- # Movement
- x = self.position[0] + (vx / FPS)
- y = self.position[1] + (vy / FPS)
- # Prevent escaping up or down
- if y < (self.image.get_height() / 2):
- y = (self.image.get_height() / 2)
- if vy < 0:
- vy = 0
- elif y > SCREEN_SIZE[1] - (self.image.get_height() / 2):
- y = SCREEN_SIZE[1] - (self.image.get_height() / 2)
- if vy > 0:
- vy = 0
- self.velocity = (vx, vy)
- self.position = (x, y)
- def draw(self, screen):
- rotated_astronaut = pygame.transform.rotate(self.image, 0)
- rect = rotated_astronaut.get_rect()
- rect.center = self.position
- screen.blit(rotated_astronaut, rect)
- class Laser:
- def __init__(self, image, pos):
- self.image = image
- self.damage = 100
- self.position = pos
- self.size = (self.image.get_width(), self.image.get_height())
- def update(self) -> bool:
- closest_alien: Alien = None
- fulldist = 1000 / FPS
- while fulldist > 0 and self.damage > 0:
- dist = fulldist
- for alien in game.aliens:
- if alien.health <= 0:
- continue
- if math.fabs(alien.position[1] - self.position[1]) > (self.size[1] + alien.size[1]) / 2:
- continue
- alien_dist = alien.position[0] - self.position[0] - (self.size[0] + alien.size[1]) / 2
- if alien_dist < dist:
- dist = alien_dist
- closest_alien = alien
- self.position = (self.position[0] + dist, self.position[1])
- fulldist -= dist
- if closest_alien is not None:
- if closest_alien.health >= self.damage:
- closest_alien.health -= self.damage
- self.damage = 0
- else:
- self.damage -= closest_alien.health
- closest_alien.health = 0
- return self.damage > 0 and self.position[0] < SCREEN_SIZE[0] * 2
- def draw(self, screen):
- screen.blit(self.image, self.position)
- class Alien:
- def __init__(self, pos):
- self.image = game.alien_image
- self.position = pos
- self.size = (self.image.get_width(), self.image.get_height())
- self.health = 100
- def update(self) -> bool:
- self.position = (self.position[0] - 100 / FPS, self.position[1])
- return self.health > 0 and self.position[0] > -100
- def draw(self, screen):
- screen.blit(self.image, self.position)
- class Game:
- def __init__(self):
- pygame.init()
- self.clock = pygame.time.Clock()
- self.screen = pygame.display.set_mode(SCREEN_SIZE)
- # Astronaut
- self.astronaut = Astronaut()
- # Background
- background_image = pygame.image.load("space.jpeg").convert()
- self.background_image = pygame.transform.scale(background_image, SCREEN_SIZE)
- self.background_move = 0
- # Lasers
- self.lasers = []
- self.laser_image = pygame.image.load("space_bullet.png")
- self.laser_image = pygame.transform.scale(self.laser_image,
- (int(self.laser_image.get_width() / 2),
- int(self.laser_image.get_height() / 2)))
- self.laser_sound = pygame.mixer.Sound("blaster.wav")
- self.laser_sound.set_volume(0.05)
- self.laser_reload = 0
- # Aliens
- self.aliens = []
- self.alien_image = pygame.image.load("ufo.png")
- self.last_alien_spawn_time = 0.0
- # Music
- pygame.mixer.music.load('spacesong.mp3')
- pygame.mixer.music.set_volume(0.02)
- pygame.mixer.music.play(-1)
- # Display
- icon = pygame.image.load("astronaut.png")
- pygame.display.set_icon(icon)
- pygame.display.set_caption("Astronaut Invaders")
- pygame.display.update()
- self.timer = 0.0
- def game_intro(self):
- intro = True
- text_surf = pygame.image.load("astronaut_invaderstext.png")
- intro_image = pygame.image.load("moon.jpg").convert()
- astro_x = 200
- astro_y = 200
- astro_vx = 0
- astro_vy = 0
- t = 0
- while intro:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- if event.type == pygame.KEYDOWN:
- intro = False
- t += 1
- astro_vx += random.uniform(-0.2, 0.2)
- astro_vy += random.uniform(-0.2, 0.2)
- astro_vx += math.cos(t * 0.01) * 0.01
- astro_vy += math.sin(t * 0.01) * 0.01
- astro_x += astro_vx
- astro_y += astro_vy
- astro_width = self.astronaut.image.get_width()
- astro_height = self.astronaut.image.get_height()
- if astro_x < 0:
- astro_x = 0
- astro_vx = 0
- if astro_x > SCREEN_SIZE[0] - astro_width:
- astro_x = SCREEN_SIZE[0] - astro_width
- astro_vx = 0
- if astro_y < 0:
- astro_y = 0
- astro_vy = 0
- if astro_y > SCREEN_SIZE[1] - astro_height:
- astro_y = SCREEN_SIZE[1] - astro_height
- astro_vy = 0
- self.screen.blit(intro_image, (0, 0))
- self.screen.blit(self.astronaut.image, (astro_x, astro_y))
- self.screen.blit(text_surf, (80, 300))
- pygame.display.update()
- pygame.display.flip()
- self.clock.tick(60)
- intro_voice = pygame.mixer.Sound('Intro_voice.wav')
- intro_voice.set_volume(0.5)
- # Loop 1/100 times
- intro_voice.play(math.ceil(random.uniform(0.01, 1.01) - 1))
- def start(self):
- while True:
- self.loop()
- def loop(self):
- self.clock.tick(FPS)
- self.update()
- self.draw()
- def update(self):
- self.timer += (1/FPS)
- self.background_move += (10 / FPS)
- if self.laser_reload > 0:
- self.laser_reload -= (1 / FPS)
- for event in pygame.event.get():
- if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and self.laser_reload <= 0:
- self.fire_laser()
- self.laser_reload = 0.3
- keys = pygame.key.get_pressed()
- down = 0
- if keys[pygame.K_UP]:
- down -= 1
- if keys[pygame.K_DOWN]:
- down += 1
- if keys[pygame.K_ESCAPE]:
- pygame.quit()
- sys.exit(0)
- pygame.event.pump()
- self.astronaut.move(0, down)
- self.astronaut.update()
- new_lasers = []
- for laser in self.lasers:
- if laser.update():
- new_lasers.append(laser)
- self.lasers = new_lasers
- new_aliens = []
- for alien in self.aliens:
- if alien.update():
- new_aliens.append(alien)
- self.aliens = new_aliens
- if self.timer - self.last_alien_spawn_time > 0.5:
- self.last_alien_spawn_time = self.timer
- self.aliens.append(Alien((SCREEN_SIZE[0], random.randint(0, SCREEN_SIZE[1] - 50))))
- def fire_laser(self):
- pos = (self.astronaut.position[0], self.astronaut.position[1])
- self.lasers.append(Laser(self.laser_image, pos))
- self.laser_sound.play()
- def draw(self):
- self.screen.fill(Colors.BLACK)
- if self.background_move > SCREEN_SIZE[0]:
- self.background_move -= SCREEN_SIZE[0]
- self.screen.blit(self.background_image, [-self.background_move, 0])
- self.screen.blit(self.background_image, [SCREEN_SIZE[0] - self.background_move, 0])
- for alien in self.aliens:
- alien.draw(self.screen)
- for laser in self.lasers:
- laser.draw(self.screen)
- self.astronaut.draw(self.screen)
- pygame.display.flip()
- game = Game()
- game.game_intro()
- game.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement