Created
March 16, 2018 13:24
-
-
Save MrValdez/0f9c2f16becae908d6464de02e4d9afd to your computer and use it in GitHub Desktop.
2018 - Pizza, Beer and Code talks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import random | |
import math | |
pygame.init() | |
display = pygame.display.set_mode([1000, 800]) | |
clock = pygame.time.Clock() | |
isRunning = True | |
bg = pygame.image.load("bg.jpg").convert() | |
hero = pygame.image.load("saitama.jpg").convert() | |
hero.set_colorkey([255, 255, 255]) | |
hero_pos = [0, 0] | |
class Enemy: | |
def __init__(self, pos): | |
self.pos = pos | |
self.original_pos = list(pos) | |
self.image = pygame.image.load("dora.png").convert() | |
self.image.set_colorkey([255, 128, 128]) | |
self.velocity = [random.randint(-10, -1), 0] | |
self.has_collide = False | |
def update(self): | |
if not self.has_collide: | |
self.pos[0] += self.velocity[0] | |
self.pos[1] += self.velocity[1] | |
else: | |
self.pos[0] -= self.velocity[0] | |
self.pos[1] = self.original_pos[1] + (math.sin(self.pos[0] * 0.2) * random.randint(10, 50)) | |
def draw(self, display): | |
display.blit(self.image, self.pos) | |
enemies = [] | |
for i in range(100): | |
x = random.randint(1000, 4000) | |
y = random.randint(-100, 700) | |
enemy = Enemy([x, y]) | |
enemies.append(enemy) | |
while isRunning: # game loop; iteration = frame | |
#display.fill([0, 0, 0]) | |
display.blit(bg, [0, 0]) | |
events = pygame.event.get() | |
for event in events: | |
if event.type == pygame.QUIT: | |
isRunning = False | |
keys = pygame.key.get_pressed() | |
if keys[pygame.K_ESCAPE]: | |
isRunning = False | |
speed = 3 | |
if keys[pygame.K_LEFT]: | |
hero_pos[0] -= speed | |
if keys[pygame.K_RIGHT]: | |
hero_pos[0] += speed | |
if keys[pygame.K_UP]: | |
hero_pos[1] -= speed | |
if keys[pygame.K_DOWN]: | |
hero_pos[1] += speed | |
for enemy in enemies: | |
enemy.update() | |
for enemy in enemies: | |
hero_rect = hero.get_rect() | |
enemy_rect = enemy.image.get_rect() | |
hero_rect.move_ip(hero_pos) | |
enemy_rect.move_ip(enemy.pos) | |
if enemy_rect.colliderect(hero_rect): | |
enemy.has_collide = True | |
display.blit(hero, hero_pos) | |
for enemy in enemies: | |
enemy.draw(display) | |
pygame.display.update() | |
clock.tick(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment