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
- 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
- vx *= 0.99
- vy *= 0.99
- # 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 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)
- # 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()
- def game_intro(self):
- intro = True
- large_text = pygame.font.Font('freesansbold.ttf', 115)
- text_surf = large_text.render("Astronaut Invaders", False, Colors.BLACK)
- noob_text = pygame.font.Font('freesansbold.ttf', 48)
- intro_text = noob_text.render("Press Any key to continue", False, Colors.BLACK)
- intro_image = pygame.image.load("moon.jpg").convert()
- astro_x = 200
- astro_y = 200
- while intro:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- quit()
- if event.type == pygame.KEYDOWN:
- intro = False
- astro_x += random.randint(-10, 10)
- astro_y += random.randint(-10, 10)
- self.screen.blit(intro_image, (0, 0))
- self.screen.blit(self.astronaut.image, (astro_x, astro_y))
- self.screen.blit(text_surf, (100, 300))
- self.screen.blit(intro_text, (100, 400))
- pygame.display.update()
- pygame.display.flip()
- self.clock.tick(60)
- def start(self):
- while True:
- self.loop()
- def loop(self):
- self.clock.tick(FPS)
- self.update()
- self.draw()
- def update(self):
- 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()
- def draw(self):
- self.screen.fill(Colors.BLACK)
- self.screen.blit(self.background_image, [0, 0])
- 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