Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from time import *
- #Sæþór Bergsson
- # Class for the Player(the orange dude)
- class Player(object):
- def __init__(self):
- self.rect = pygame.Rect(32, 32, 16, 16)
- def move(self, dx, dy):
- # Move each axis separately. Note that this checks for collisions both times.
- 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 rectangle
- self.rect.x += dx
- self.rect.y += dy
- # If you collide with a wall
- for a_brick in bricks:
- if self.rect.colliderect(a_brick.rect):
- if dx > 0: # Moving right; Hit the left side of the wall
- self.rect.right = a_brick.rect.left
- if dx < 0: # Moving left; Hit the right side of the wall
- self.rect.left = a_brick.rect.right
- if dy > 0: # Moving down; Hit the top side of the wall
- self.rect.bottom = a_brick.rect.top
- if dy < 0: # Moving up; Hit the bottom side of the wall
- self.rect.top = a_brick.rect.bottom
- # if you collide with the end_rect and don't have 15 points, the end_rect will act like a brick
- if stig != 15:
- if self.rect.colliderect(end_rect):
- if dx > 1: # Moving right; Hit the left side of the wall
- self.rect.right = end_rect.left
- elif dx < 1: # Moving left; Hit the right side of the wall
- self.rect.left = end_rect.right
- elif dy > 1: # Moving down; Hit the top side of the wall
- self.rect.bottom = end_rect.top
- elif dy < 0: # Moving up; Hit the bottom side of the wall
- self.rect.top = end_rect.bottom
- #if you collide with the end_rect and have 15 points, the game will end
- elif self.rect.colliderect(end_rect):
- if stig == 15:
- print("-----YOU WIN-----")
- sleep(0.5)
- print(".")
- sleep(0.5)
- print(".")
- sleep(0.5)
- print(".")
- sleep(0.5)
- print("-----STATS-----")
- print("POINTS:", stig)
- print("TIME:", sec, "seconds")
- raise SystemExit
- # A class to hold a wall rectangle
- class Brick(object):
- def __init__(self, pos):
- self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
- class Magic(object):
- def __init__(self, pos):
- self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
- # A class to hold the shields the player can pick up
- class Shield(object):
- def __init__(self, pos):
- self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
- # Initialize
- pygame.init()
- # Set up the display
- pygame.display.set_caption("Get to the red square!")
- screen = pygame.display.set_mode((673, 500))
- clock = pygame.time.Clock()
- bricks = list() # List to hold the walls
- shields = list() # List to hold the shields
- magic = list() # List to hold the magic boxes
- playerInv = list() # list to hold the sheilds for player
- player = Player() # Create the player
- # Holds the level layout in a list of strings.
- maze = [
- "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
- "W W S M S W W",
- "WW WWWW WWWWWWWWWWWWWWWWWWWWMWW W W W",
- "W WSW W S W WS W WS W",
- "WMWW WW WWMWWWWWWWWWWWWW WWWWWWMWWWWWW",
- "W W W W W",
- "WW WWWWWWWWWWWWWWWWMWWWWWWWWWWWWWWWWW WWWW",
- "W W M S SWW W W",
- "W WWW WWWWWWWWWW W WWWWWWWWWW WWW W",
- "W W WWMWW SW WW W",
- "WWWWWWWW S W W SW W M W",
- "W WWWWWWWWWWWWWW WMMMWWW W WWMMW",
- "W W W W WS W",
- "W W WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW W",
- "W WWWWWS W W S M W W W SWWWWW",
- "W W M W W W W M M E",
- "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
- ]
- # Parse the maze string above. W = wall, E = exit, M = magic box, S = shield
- x = 0
- y = 0
- for row in maze:
- for col in row:
- if col == "W":
- bricks.append(Brick((x, y)))
- if col == "E":
- end_rect = pygame.Rect(x, y, 16, 16)
- if col == "M":
- magic.append(Magic((x, y)))
- if col == "S":
- shields.append(Shield((x, y)))
- x += 16
- y += 16
- x = 0
- stig = 0# points
- start_ticks = pygame.time.get_ticks() #starter tick
- running = True
- while running:
- clock.tick(60)
- sec = (pygame.time.get_ticks() - start_ticks) / 1000 # calculate how many seconds
- for e in pygame.event.get():
- if e.type == pygame.QUIT:
- running = False
- if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
- running = False
- # Move the player if an arrow key is pressed
- key = pygame.key.get_pressed()
- if key[pygame.K_LEFT]:
- player.move(-2, 0)
- if key[pygame.K_RIGHT]:
- player.move(2, 0)
- if key[pygame.K_UP]:
- player.move(0, -2)
- if key[pygame.K_DOWN]:
- player.move(0, 2)
- # Draw the scene
- screen.fill((0, 0, 0))
- # every brick in the walls is drawn
- for brick in bricks:
- pygame.draw.rect(screen, (255, 255, 255), brick.rect)
- # every shield is drawn
- for shield in shields:
- pygame.draw.rect(screen, (0, 0, 255), shield.rect)
- # every magic box is drawn
- for box in magic:
- pygame.draw.rect(screen, (0, 255, 0), box.rect)
- # the others are drawn
- #if player has more or equal to 1 point he changes color, yellow
- if len(playerInv) >= 1:
- pygame.draw.rect(screen, (255, 255, 0), player.rect)
- #if player does not have a point he will have the same color as he started with, stating he has no shield, pink
- else:
- pygame.draw.rect(screen, (255, 0, 255), player.rect)
- if stig == 15:
- pygame.draw.rect(screen, (0, 255, 0), end_rect)
- else:
- pygame.draw.rect(screen, (255, 0, 0), end_rect)
- #if you collide with shield
- for a_shield in shields:
- if player.rect.colliderect(a_shield.rect):
- print("Got myself a Shield")
- playerInv.append(a_shield)
- shields.remove(a_shield)
- #if you collide with magic box
- for a_magic in magic:
- if len(playerInv) >= 1:
- if player.rect.colliderect(a_magic.rect):
- stig = stig + 1
- for i in magic:
- if player.rect.colliderect(i):
- magic.remove(i)
- print(stig)
- for x in playerInv:
- playerInv.remove(x)
- #if player collides with magic box without having a shield, you loose
- elif len(playerInv) == 0:
- if player.rect.colliderect(a_magic.rect):
- print("-----GAME OVER-----")
- sleep(0.5)
- print(".")
- sleep(0.5)
- print(".")
- sleep(0.5)
- print(".")
- sleep(0.5)
- print("-----STATS-----")
- print("POINTS:", stig)
- print("TIME:", sec,"seconds")
- raise SystemExit
- #if the game timer runs out, you loose
- if sec > 150:
- print("-----GAME OVER-----")
- sleep(0.5)
- print(".")
- sleep(0.5)
- print(".")
- sleep(0.5)
- print(".")
- sleep(0.5)
- print("-----STATS-----")
- print("POINTS:",stig)
- print("TIME:",sec,"seconds")
- raise SystemExit
- pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement