Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
weiwi21 committed Aug 17, 2021
1 parent c0ca6f4 commit b822243
Show file tree
Hide file tree
Showing 45 changed files with 840 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ dmypy.json

# Pyre type checker
.pyre/

.DS_Store
46 changes: 46 additions & 0 deletions src/display.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import pygame

DIMENSION = 60


class Display():
# create base variables and sets the font
def __init__(self):
self.screen = None
self.board = None
self.X_DIMENSION = None
self.Y_DIMENSION = None
self.font = pygame.font.Font('font/joystix_monospace.ttf', 68)

# gets board from game booard class
def set_board(self, board):
self.board = board

# creates screen from scale
def set_screen(self, scale):
x = int(scale[0]) * DIMENSION
y = int(scale[1]) * DIMENSION

self.X_DIMENSION = x
self.Y_DIMENSION = y
screen = pygame.display.set_mode((x, y))
self.screen = screen

# draws grid board based off scale and dimension size
def draw_board(self):
y = 0
for row in self.board:
x = 0
for cell in row:
self.screen.blit(cell.get_image(), pygame.Rect(x * DIMENSION, y * DIMENSION, DIMENSION, DIMENSION))
x += 1
y += 1
pygame.display.update()

# draws inputed tex
def draw_text(self, text):
text = self.font.render(text, False, (0, 0, 255), None)
text_rect = text.get_rect()
text_rect.center = (660 // 2, 660 // 2)
self.screen.blit(text, text_rect)
pygame.display.update()
93 changes: 93 additions & 0 deletions src/event_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import pygame
import threading


class EventHandler():
def __init__(self, display, clock, gameboard):
# Set some defaults
self.lock = threading.Lock()
self.clock = clock
self.display = display
self.gameboard = gameboard
self.game_over = False
self.snakes = []
self.dead = False

# Listener class that is being called by the main class.
def listen(self, event):

# Checks event type
if event.type == pygame.QUIT:
return False
if event.type == pygame.KEYDOWN:
# If the game is over it will lock the player out from doing anything that can interact with the character
if not self.game_over:
# Takes the movement keys, tells the game board to move the game pieces and checks for win or death
if event.key == pygame.K_LEFT or event.key == pygame.K_a:
self.gameboard.lolo_move(-1, 0, "LEFT")
self.check()
elif event.key == pygame.K_RIGHT or event.key == pygame.K_d:
self.gameboard.lolo_move(1, 0, "RIGHT")
self.check()
elif event.key == pygame.K_UP or event.key == pygame.K_w:
self.gameboard.lolo_move(0, -1, "UP")
self.check()
elif event.key == pygame.K_DOWN or event.key == pygame.K_s:
self.gameboard.lolo_move(0, 1, "DOWN")
self.check()
elif event.key == pygame.K_SPACE:
# Checks if the shot lands and starts timer if shot lands
shot = self.gameboard.shoot()
if shot is not None:
self.snakes.append(shot.occupied)
t = threading.Timer(5, self.convert_snake)
t.start()

# If the game is over, press return to exit the game
if event.key == pygame.K_RETURN:
if self.game_over:
return False
# If the user hits the escape exit the game
elif event.key == pygame.K_ESCAPE:
return False

# If the user hits the r key the game resets
elif event.key == pygame.K_r:
self.gameboard.reset()
self.game_over = False
self.display.set_board(self.gameboard.board)
self.display.draw_board()

return True

# Runs all the checks after the move
def check(self):
if self.gameboard.check_medusa():
self.die()
self.dead = True
if self.gameboard.win and not self.dead:
self.win_game()

# tells the game board to end the game, displays the text, and locks the movement and shooting keys
def die(self):
self.gameboard.end_game()
self.display.draw_text('Game Over')
self.game_over = True

# Tells the game board to end/win the game, displays the text, locks the movement and shooting keys
def win_game(self):
self.gameboard.win_game()
self.display.draw_text('You Win!')
self.game_over = True

# locks other threads to prevent a 2nd call when the method is running, tells egg to go back to snake
def convert_snake(self):
self.lock.acquire()

self.gameboard.snake_return(self.snakes[0])
self.snakes.pop(0)

self.lock.release()



Binary file added src/font/joystix_monospace.ttf
Binary file not shown.
38 changes: 38 additions & 0 deletions src/game_cell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from game_object_factory import *


class GameCell():
# set values
def __init__(self, locationX, locationY, id):
self.locationX = locationX
self.locationY = locationY
gameObjectFactory = GameObjectFactory(id)
self.occupied = gameObjectFactory.get()
self.old = None

# replaces old piece with new piece, old piece goes to self.old or goes to None
def change_cell(self, piece):
if self.occupied is not None:
if "HEART" in self.occupied.type:
self.old = None
if self.occupied.type == "BRIDGE" or self.occupied.type == "TREASURE":
self.old = self.occupied
self.occupied = piece

# empties cell and sets old cell back to current
def empty(self):
self.occupied = self.old

# get image from game piece in the cell
def get_image(self):
if self.occupied is not None:
return self.occupied.current_image
else:
return pygame.transform.scale(pygame.image.load("images/background.gif"), (DIMENSION, DIMENSION))

# gets the name of the game piece in the cell
def get_name(self):
if self.occupied is not None:
return self.occupied.name()
else:
return None
211 changes: 211 additions & 0 deletions src/game_object.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
import pygame.transform

DIMENSION = 60


# set parent class for all game objects/pieces
class GameObject:
def __init__(self, id, type, block_vision, movable, walk_overable):
self.block_vision = block_vision
self.movable = movable
self.walk_overable = walk_overable
self.type = type
self.id = id
self.dimension = (DIMENSION, DIMENSION)

def name(self):
return self.type


# all following classes are subclasses of the game piece and will set some base values
class Bridge(GameObject):
def __init__(self):
super().__init__(6, "BRIDGE", False, False, True)
self.IMAGES = []
image = pygame.transform.scale(pygame.image.load("images/bridge.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
self.current_image = self.IMAGES[0]


class Rock(GameObject):
def __init__(self):
super().__init__(8 , "ROCK",True, False, False)
self.IMAGES = []
image = pygame.transform.scale(pygame.image.load("images/rock.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
self.current_image = self.IMAGES[0]


class River(GameObject):
def __init__(self):
super().__init__(11 , "RIVER", False, False, False)
self.IMAGES = []
image = pygame.transform.scale(pygame.image.load("images/water.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
self.current_image = self.IMAGES[0]


class Box(GameObject):
def __init__(self):
super().__init__(2, "BOX", True, True, False)
self.IMAGES = []
image = pygame.transform.scale(pygame.image.load("images/box.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
self.current_image = self.IMAGES[0]


class Heart(GameObject):
def __init__(self):
super().__init__(4, "HEART", False, False, True)
self.IMAGES = []
image = pygame.transform.scale(pygame.image.load("images/heart.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
self.current_image = self.IMAGES[0]


# subclass of heart class re-sets some defaults
class PowerHeart(Heart):
def __init__(self):
super().__init__()
self.type = "SUPER_HEART"
self.id = 10
self.IMAGES = []
image = pygame.transform.scale(pygame.image.load("images/powerheart.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
self.current_image = self.IMAGES[0]
self.power_count = 2


class Lolo(GameObject):
# stores locations for all of the images that lolo uses
def __init__(self):
super().__init__(1, "LOLO", False, True, False)
self.power_count = 0
self.IMAGES = []
self.direction = "DOWN"
location = ["lolo_s_l", "lolo_s_r", "lolo_n_l", "lolo_n_r", "lolo_e_l",
"lolo_e_r", "lolo_w_l", "lolo_w_r", "lolo_win",
"dead_w", "dead_e"]

for image_location in location:
file_location = "images/" + image_location + ".gif"
image = pygame.transform.scale(pygame.image.load(file_location), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
self.current_image = self.IMAGES[0]
self.current_image_index = 0

# some base classes to update values in the lolo
def obtain_heart(self):
self.power_count += 2

def shoot(self):
self.power_count -= 1

def set_image(self, index):
self.current_image = self.IMAGES[index]
self.current_image_index = index

# used to change image for lolo
def update(self, direction):
if direction == "UP":
self.direction = direction
if self.current_image_index != 2:
self.set_image(2)
else:
self.set_image(3)
if direction == "DOWN":
self.direction = direction
if self.current_image_index != 0:
self.set_image(0)
else:
self.set_image(1)
if direction == "LEFT":
self.direction = direction
if self.current_image_index != 6:
self.set_image(6)
else:
self.set_image(7)
if direction == "RIGHT":
self.direction = direction
if self.current_image_index != 4:
self.set_image(4)
else:
self.set_image(5)

if direction == "DEAD":
if self.current_image_index % 2 == 0:
self.set_image(9)
else:
self.set_image(10)

if direction == "WIN":
self.set_image(8)


class Snake(GameObject):
# set images for snake
def __init__(self):
super().__init__(3, "SNAKE", True, False, False)
self.IMAGES = []
snake_east = pygame.transform.scale(pygame.image.load("images/snake_east.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(snake_east)
snake_west = pygame.transform.scale(pygame.image.load("images/snake_east.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(snake_west)
egg = pygame.transform.scale(pygame.image.load("images/egg.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(egg)
self.current_image = self.IMAGES[0]
self.is_egg = False
self.number = 0

# changes image for snake
def shot(self):
self.is_egg = True
self.type = "EGG"
self.movable = True
self.current_image = self.IMAGES[2]

def back(self):
self.is_egg = False
self.type = "SNAKE"
self.movable = False
self.current_image = self.IMAGES[0]


class Medusa(GameObject):
def __init__(self):
super().__init__(7, "MEDUSA", True, False, False)
self.IMAGES = []
image = pygame.transform.scale(pygame.image.load("images/medusa.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
smile = pygame.transform.scale(pygame.image.load("images/smiling_medusa.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(smile)
self.current_image = self.IMAGES[0]

def update(self):
self.current_image = self.IMAGES[1]


class Tree(GameObject):
def __init__(self):
super(Tree, self).__init__(9, "TREE", False, False, False)
self.IMAGES = []
image = pygame.transform.scale(pygame.image.load("images/tree.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
self.current_image = self.IMAGES[0]


class Treasure(GameObject):
def __init__(self):
super().__init__(5, "TREASURE", False, False, True)
self.available = False
self.IMAGES = []
image = pygame.transform.scale(pygame.image.load("images/treasure.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(image)
open = pygame.transform.scale(pygame.image.load("images/open_treasure.gif"), (DIMENSION, DIMENSION))
self.IMAGES.append(open)
self.current_image = self.IMAGES[0]

def complete(self):
self.available = True
self.type = "OPEN_TREASURE"
self.current_image = self.IMAGES[1]
Loading

0 comments on commit b822243

Please sign in to comment.