Created
January 12, 2016 05:59
-
-
Save mikemedina/63b094306e7be4d47c16 to your computer and use it in GitHub Desktop.
This file contains 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 | |
from random import randint | |
class Player(): | |
def __init__(self, player, it, x, y, width, height, sprite): | |
players.append(self) | |
self.player = player | |
self.it = it | |
self.sprite = sprite | |
self.rect = pygame.Rect(x, y, width, height) | |
self.gravity = 1 | |
self.timer = 465 # Time since being tagged | win condition | |
self.dx = 10 | |
self.dy = -20 | |
def move(self, dx, dy): | |
# Move each axis separately | |
if dx != 0: | |
self.move_single_axis(dx, 0) | |
if dy != 0: | |
self.move_single_axis(0, dy) | |
def move_single_axis(self, dx, dy): | |
# Move the rect | |
self.rect.x += dx | |
self.rect.y += dy | |
# If you collide with a wall, set sides equal | |
for block in blocks: | |
if self.rect.colliderect(block.rect): | |
if dx > 0: # Moving right; Hit the left side of the wall | |
self.rect.right = block.rect.left | |
if self.player == 1: | |
if not key[pygame.K_SPACE]: | |
self.can_jump = True | |
else: | |
if not key[pygame.K_KP0]: | |
self.can_jump = True | |
if dx < 0: # Moving left; Hit the right side of the wall | |
self.rect.left = block.rect.right | |
if self.player == 1: | |
if not key[pygame.K_SPACE]: | |
self.can_jump = True | |
else: | |
if not key[pygame.K_KP0]: | |
self.can_jump = True | |
if dy > 0: # Moving down; Hit the top side of the wall | |
self.rect.bottom = block.rect.top | |
self.dy = 0 | |
self.gravity = 1 | |
if self.player == 1: | |
if not key[pygame.K_SPACE]: | |
self.can_jump = True | |
else: | |
if not key[pygame.K_KP0]: | |
self.can_jump = True | |
if dy < 0: # Moving up; Hit the bottom side of the wall | |
self.rect.top = block.rect.bottom | |
self.dy = 0 | |
for player in players: | |
if player.player != self.player: | |
if self.rect.colliderect(player.rect): | |
if dx > 0: # Moving right; Hit the left side of the wall | |
self.rect.right = player.rect.left | |
if not key[pygame.K_SPACE]: | |
self.can_jump = True | |
if dx < 0: # Moving left; Hit the right side of the wall | |
self.rect.left = player.rect.right | |
if not key[pygame.K_SPACE]: | |
self.can_jump = True | |
if dy > 0: # Moving down; Hit the top side of the wall | |
self.rect.bottom = player.rect.top | |
self.dy = 0 | |
self.gravity = 1 | |
if not key[pygame.K_SPACE]: | |
self.can_jump = True | |
if dy < 0: # Moving up; Hit the bottom side of the wall | |
self.rect.top = player.rect.bottom | |
self.dy = 0 | |
player.it = True | |
self.it = False | |
player1.timer = 465 | |
if self.rect.x < 0: | |
self.rect.x = 0 | |
if self.rect.x > size[0] - self.rect.width: | |
self.rect.x = size[0] - self.rect.width | |
if self.rect.y < 0: | |
self.rect.y = 0 | |
if self.rect.y > size[1] - self.rect.height: | |
self.rect.y = size[1] - self.rect.height | |
class Block(): | |
def __init__(self, x, y, width, height): | |
blocks.append(self) | |
self.rect = pygame.Rect(x, y, width, height) | |
class Power_Up: | |
def __init__(self, x, y, buff_type): | |
power_ups.append(self) | |
self.buff = ['speed'] | |
self.buff_type = buff_type | |
self.rect = pygame.Rect(x, y, 64, 64) | |
def check_powerup(self): | |
for player in players: | |
if player.rect.colliderect(self.rect): | |
power_ups.remove(self) | |
if self.buff[self.buff_type] == 'speed': | |
player.dx += 10 | |
pygame.init() | |
size = (1280, 768) | |
screen = pygame.display.set_mode(size) | |
font = pygame.font.Font('freesansbold.ttf', 32) | |
clock = pygame.time.Clock() | |
background = pygame.image.load('background.png') | |
sprites = [pygame.image.load('player1.png'), pygame.image.load('player2.png'), | |
pygame.image.load('it.gif'), pygame.image.load('block.png'), | |
pygame.image.load('power.png')] | |
power_ups = [] | |
blocks = [] | |
players = [] | |
player1 = Player(1, True, 64, 64, 48, 60, sprites[0]) | |
player2 = Player(2, False, size[0]-112, 64, 48, 60, sprites[1]) | |
level = [ | |
"WWWWWWWWWWWWWWWWWWWW", | |
"W W", | |
"W P WW P W", | |
"W W", | |
"W WWW WWW W", | |
"W W", | |
"W W WW W W", | |
"W WWW WWW W", | |
"W W W W", | |
"W W", | |
"WW WW WW", | |
"WWWWWWWWWWWWWWWWWWWW", | |
] | |
# Parse the level string above. W = Wall, P = PowerUp | |
x = y = 0 | |
for row in level: | |
for col in row: | |
if col == "W": | |
Block(x, y, 64, 64) | |
elif col == "P": | |
Power_Up(x, y, randint(0, 0)) | |
x += 64 | |
y += 64 | |
x = 0 | |
# Start game loop | |
while True: | |
clock.tick(60) | |
if player1.timer > 0: player1.timer -=1 | |
key = pygame.key.get_pressed() | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
pygame.quit() | |
exit() | |
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE: | |
pygame.quit() | |
exit() | |
# Move player1 | |
if key[pygame.K_a]: | |
player1.move(-player1.dx, 0) | |
if key[pygame.K_d]: | |
player1.move(player1.dx, 0) | |
if key[pygame.K_SPACE] and player1.can_jump: | |
player1.dy = -20 | |
player1.gravity = 1 | |
player1.can_jump = False | |
player1.move(0, player1.dy) | |
if player1.dy <= 40: | |
player1.dy += player1.gravity | |
player1.gravity += .1 | |
# Move player2 | |
if key[pygame.K_LEFT]: | |
player2.move(-player2.dx, 0) | |
if key[pygame.K_RIGHT]: | |
player2.move(player2.dx, 0) | |
if key[pygame.K_KP0] and player2.can_jump: | |
player2.dy = -20 | |
player2.gravity = 1 | |
player2.can_jump = False | |
player2.move(0, player2.dy) | |
if player2.dy <= 40: | |
player2.dy += player2.gravity | |
player2.gravity += .1 | |
# PowerUp testing | |
# if key[pygame.K_TAB]: | |
# Power_Up(player1.rect.x + 66, player1.rect.y, randint(0, 0)) | |
# player1.timer = 150 | |
# if key[pygame.K_w]: | |
# Power_Up(player1.rect.x + 66, player1.rect.y, randint(0, 0)) | |
screen.blit(background, (0, 0)) | |
screen.blit(player1.sprite, (player1.rect.x, player1.rect.y)) | |
screen.blit(player2.sprite, (player2.rect.x, player2.rect.y)) | |
if player1.it: | |
screen.blit(sprites[2], (player1.rect.x, player1.rect.y)) | |
else: | |
screen.blit(sprites[2], (player2.rect.x, player2.rect.y)) | |
for block in blocks: | |
screen.blit(sprites[3], (block.rect.x, block.rect.y)) | |
for power in power_ups: | |
power.check_powerup() | |
screen.blit(sprites[4], (power.rect.x, power.rect.y)) | |
screen.blit(font.render(str(player1.timer//30), False, (0, 0, 0)), | |
(size[0]/2, 64)) | |
if player1.timer <= 0: | |
if player1.it: | |
screen.blit(font.render('Player 2 Wins!!!', False, (0, 0, 0)), | |
((size[0]/2) - 96, (size[1]/2) - 64)) | |
else: | |
screen.blit(font.render('Player 1 Wins!!!', False, (0, 0, 0)), | |
((size[0]/2) - 96, (size[1]/2) - 64)) | |
pygame.display.flip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment