Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
7787bab
Submitted project proposal
dberny Mar 6, 2018
5cede9a
main game file
rwon869 Mar 6, 2018
edac488
added pygame packages
rwon869 Mar 6, 2018
22b9d05
Added Diego's file
dberny Mar 6, 2018
d6df61a
Adding Rachel's file
rwon869 Mar 6, 2018
862ab2b
Defining classes
rwon869 Mar 8, 2018
3cc1db1
Added a draw screen thing and copied code from other places
dberny Mar 9, 2018
9e6922e
cleaned it up slightly
dberny Mar 9, 2018
0a44f39
created Sprite class with penguin image, added test code
rwon869 Mar 9, 2018
13bac20
added discustinc code
dberny Mar 9, 2018
7cce0d1
rock obstacle
rwon869 Mar 9, 2018
705dbe4
Made obstacles and penguins and it moves and it detects collisions.
dberny Mar 9, 2018
8554b58
Moved game to main file
dberny Mar 9, 2018
bbc250e
Added rotate and todo list
rwon869 Mar 9, 2018
78293c6
added ice patches, speed up, random track generates, etc
rwon869 Mar 13, 2018
ecbe614
added crash animation and transparent pictures
rwon869 Mar 14, 2018
d47c8ec
slightly smaller hitboxes, timer
dberny Mar 16, 2018
29ce4ae
disgust code
dberny Mar 16, 2018
241821e
Reconstructed code to put into MVC structure
rwon869 Mar 16, 2018
89aca27
added bump functionality
rwon869 Mar 16, 2018
3f4de0f
logs and stuff
dberny Mar 16, 2018
fcfee7c
Added logs and ramps, obstacle interference detection
rwon869 Mar 16, 2018
640e28d
adding README
rwon869 Mar 16, 2018
6422f0d
Project reflection
dberny Mar 16, 2018
4643d01
idk what's happening
dberny Mar 16, 2018
6b9cf7b
added to readme
dberny Mar 16, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
269 changes: 269 additions & 0 deletions CPSled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
#THIS FILE IS DOPE
import pygame
from pygame.locals import *
import time
from random import *
clock = pygame.time.Clock()

class Penguin(pygame.sprite.Sprite):
"""
Penguin is the sprite that the user controls using the left and right arrows
It moves up and down (respectively) the screen
"""
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.image.load("penguin_smol.png")
self.rect = self.image.get_rect()
self.rect.y = 170

def moveUp(self, pixels):
if self.rect.y <= 0:
self.rect.y = 0
else:
self.rect.y -= pixels
def moveDown(self, pixels):
if self.rect.y >= 340:
self.rect.y = 340
else:
self.rect.y += pixels

class Obstacles(pygame.sprite.Sprite):
"""
Obstacles is the parent class of the many different types of obstacles
The obstacles continuously move towards the left of the screen (at varying speeds),
giving the impression that the penguin is moving towards the right of the screen
"""
def __init__(self, image_name, rect = None):
super().__init__()
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
if rect != None:
self.rect = rect

def moveLeft(self, pixels = 7):
self.rect.x -= pixels
def slowDown(self, pixels = 3):
self.rect.x -= pixels
def speedUp(self, pixels = 10):
self.rect.x -= pixels

class Powerups(Obstacles):
"""
PowerUps inherits from Obstacles and makes the obstacles move faster across the screen,
making it seem like the penguin is speeding up
"""
def speedUp(self, pixels = 10):
self.rect.x -= pixels

class FinishLine(Obstacles):
pass

class Bump(Obstacles):
pass

class Log(Obstacles):
pass

class Ramp(Obstacles):
pass

class KeyController(object):
"""
allows the player to move the penguin using the left and right keys
"""
def __init__(self, model):
self.model = model

def event_type(self, keys, state): #state determines whether the penguin hit an obstacle or powerup
if keys[pygame.K_LEFT] and state == 1:
#if penguin hits obstacle, prevent the player from moving off the obstacle by "disabling" the keyboard controls
self.model.penguin.moveUp(0)
if keys[pygame.K_RIGHT] and state == 1:
self.model.penguin.moveDown(0)
if keys[pygame.K_LEFT] and state == 2:
#if the penguin hits a powerup, the player can still potentially move off the powerup (just to add a bit more challenge)
self.model.penguin.moveUp(5)
if keys[pygame.K_RIGHT] and state == 2:
self.model.penguin.moveDown(5)

class CPSledView(object):
"""
View class draws all the sprites/screens that the model class updates
"""
def __init__(self, model, width, height):
self.model = model
size = (width, height) #viewing frame is 500 x 400 pixels
self.model.WHITE = pygame.Color(255, 255, 255)
self.model.screen = pygame.display.set_mode(size)

def draw(self, model, timer):
self.model = model
self.model.screen.blit(timer, (425, 20))
self.model.boulders.draw(self.model.screen)
self.model.ice_patches.draw(self.model.screen)
self.model.bumps.draw(self.model.screen)
self.model.logs.draw(self.model.screen)
self.model.ramps.draw(self.model.screen)
self.model.all_penguins.draw(self.model.screen)
self.model.finish_line_group.draw(self.model.screen)
pygame.display.update()

class CPSledModel(object):
"""
Model class updates as the player control the sprite
Creates all the sprites
Randomly generates a track of obstacles
"""
def __init__(self, width, height):
pygame.init()
size = (width, height)
self.WHITE = pygame.Color(255, 255, 255)
self.screen = pygame.display.set_mode(size)
self.height = height
self.width = width

def loadSprites(self):
self.penguin = Penguin()
#self.penguin.image = pygame.transform.rotate(self.penguin.image, -30)
self.all_penguins = pygame.sprite.RenderPlain(self.penguin)
self.boulders = pygame.sprite.RenderPlain()
self.ice_patches = pygame.sprite.RenderPlain()
self.finish_line_group = pygame.sprite.RenderPlain()
self.bumps = pygame.sprite.RenderPlain()
self.logs = pygame.sprite.RenderPlain()
self.ramps = pygame.sprite.RenderPlain()

for num_boulders in range(18): #creating 20 boulders
x_position = (num_boulders+1) * randint(410, 440) #randomly generate an x-position within a certain range
y_boulders = randint(1,3) #randomly decide how many boulders to put in a straight line
for i in range(y_boulders):
y_position = randrange(0, 340, 70) #randomly choose a y-position for each boulder
self.boulder = Obstacles("rock.png", pygame.Rect(x_position, y_position, 60, 60))
self.boulder.rect.inflate(0, -10)
self.boulders.add(self.boulder) #add boulder to the boulder sprite group

for num_ice_patches in range(13):
x_position = (num_ice_patches+1) * randint(640, 670)
y_position = randint(0, 340)
self.ice_patch = Powerups("ice_patch_flat.png", pygame.Rect(x_position, y_position, 280, 70))
self.ice_patches.add(self.ice_patch)

for num_bumps in range(12):
x_position = (num_bumps + 1) * randint(710, 730)
y_position = randint(0, 300)
self.bump = Bump("bump.png", pygame.Rect(x_position, y_position, 132, 98))
self.bumps.add(self.bump)

for num_logs in range(6):
x_position = (num_logs+1) * 1100
y_position = 0
self.log = Log("log.png", pygame.Rect(x_position, y_position, 170, 400))
self.logs.add(self.log)
self.ramp = Ramp("log_jump_test.png", pygame.Rect(x_position, 110, 170, 167))
self.ramps.add(self.ramp)

self.finish_line = FinishLine("finish_line.png", pygame.Rect(8750, -2, 144, 404))
self.finish_line_group.add(self.finish_line)

def update(self):
self.all_penguins.update()
self.boulders.update()
self.ice_patches.update()
self.bumps.update()
self.screen.fill(self.WHITE)

if __name__ == '__main__':

def main_loop():
pygame.init()
model = CPSledModel(500,400)
view = CPSledView(model, 500, 400)
controller = KeyController(model)
model.loadSprites()
#create a master list of all the randomly generated obstacles
list_of_obstacles = model.boulders.sprites()
list_of_obstacles.extend(model.ice_patches.sprites())
list_of_obstacles.extend(model.bumps.sprites())
list_of_obstacles.extend(model.logs.sprites())

#this loop parses through the master obstacle list and removes any obstacle that collides with another obstacle to minimize overlapping obstacles
index = 0
while index < (len(list_of_obstacles) - 1):
j = index + 1
while j < (len(list_of_obstacles) - 1):
if list_of_obstacles[index].rect.colliderect(list_of_obstacles[j].rect):
list_of_obstacles.pop(j)
j -= 1
j += 1
index += 1

list_of_obstacles.extend(model.ramps.sprites())
list_of_obstacles.append(model.finish_line)

font = pygame.font.Font(None, 32)
pygame.display.set_caption("Club Penguin Sledding Game")
running = True
while running:
#run a timer of how long the penguin takes to get to the finish line
current_time = str(pygame.time.get_ticks()/1000)
timer = font.render(current_time, True, (0,0,0))
for event in pygame.event.get():
if event.type is pygame.QUIT:
running = False

#if peguin passes the finish line, then break out of loop
if model.penguin.rect.left > model.finish_line.rect.left+120:
while True:
for event in pygame.event.get():
if event.type is pygame.QUIT:
pygame.quit()
running = False

#different flags to identify which obstacle hit
hit = False
ice = False
finish = False
bump = False
ramp = False
for obstacle in list_of_obstacles:
if model.penguin.rect.colliderect(obstacle.rect):
hit = True
if type(obstacle) == Ramp:
ramp = True
if type(obstacle) == Powerups:
ice = True
elif type(obstacle) == FinishLine:
finish = True
elif type(obstacle) == Bump:
bump = True

#using the flags, determine how the penguin responds to the obstacle hit
for obstacle in list_of_obstacles:
if ramp:
obstacle.moveLeft()
elif ice or finish:
obstacle.speedUp()
elif hit:
if bump:
obstacle.slowDown(5)
else:
obstacle.slowDown()
else:
obstacle.moveLeft()
if hit and not ice and not finish and not ramp:
if not bump:
model.penguin.image = pygame.image.load("fallen_penguin_smol_demanding_diego.png")
controller.event_type(pygame.key.get_pressed(), 1)
else:
controller.event_type(pygame.key.get_pressed(), 2)
else:
model.penguin.image = pygame.image.load("penguin_smol.png")
controller.event_type(pygame.key.get_pressed(), 2)

model.update()
view.draw(model, timer)
clock.tick_busy_loop(60)
pygame.quit()

main_loop()
Binary file added MP4 Club Penguin Reflection.pdf
Binary file not shown.
Binary file added MP4_Club_Penguin_Reflection.pdf
Binary file not shown.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
# InteractiveProgramming
This is the base repo for the interactive programming project for Software Design, Spring 2018 at Olin College.
Club Penguin Sledding Game
In order to setup the environment to run this code, run the following in the terminal:
$ apt-get build-dep python-pygame
$ apt-get install mercurial python-dev python-numpy ffmpeg libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev libsdl1.2-dev libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev
$ pip install pygame

In order to run the actual code, run the following in the terminal:
python CPSled.py

[Project Reflection](MP4_Club_Penguin_Reflection.pdf)

Enjoy!
Binary file added Soft Des MP4 Project Proposal.pdf
Binary file not shown.
Binary file added bump(1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bump.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added croproc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 109 additions & 0 deletions diego.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#THIS FILE IS DOPE
import pygame
from pygame.locals import *
import time
clock = pygame.time.Clock()

class Penguin(pygame.sprite.Sprite): # code is from pygame documenta
# Constructor. Pass in the color of the block,
# and its x and y position
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()

# Create an image of the block, and fill it with a color
self.image = pygame.image.load("penguin_smol.png")
self.rect = self.image.get_rect()
def moveUp(self, pixels):
if self.rect.y <= 0:
self.rect.y = 0
else:
self.rect.y -= pixels

def moveDown(self, pixels):
if self.rect.y >= 340:
self.rect.y = 340
else:
self.rect.y += pixels



class Obstacles(pygame.sprite.Sprite):
def __init__(self, rect = None):
super().__init__()
self.image = pygame.image.load("rock.png")
self.rect = self.image.get_rect()
if rect != None:
self.rect = rect

def moveLeft(self, pixels):
self.rect.x -= pixels
# class Model:
# def __init__(self):
# self.all_penguins = pygame.sprite.Group()
# penguin = Penguin()


class Sled_Main:
def __init__(self):
pygame.init()
size = (500, 400)
self.WHITE = pygame.Color(255, 255, 255)
self.screen = pygame.display.set_mode(size)

def loadSprites(self):
self.penguin = Penguin()
self.all_penguins = pygame.sprite.RenderPlain(self.penguin)

self.boulders = pygame.sprite.RenderPlain()
self.boulders.add(Obstacles(pygame.Rect(100, 100, 60, 60)))

def main_loop(self):
self.loadSprites()
running = True
pygame.display.set_caption("Club Penguing Sledding Game")
while running:
#self._redraw()
for event in pygame.event.get():
if event.type is pygame.QUIT:
running = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.penguin.moveUp(15)
if keys[pygame.K_RIGHT]:
self.penguin.moveDown(15)
self.all_penguins.update()

list_of_boulders = self.boulders.sprites()


if self.penguin.rect.colliderect(list_of_boulders[0].rect):
list_of_boulders[0].moveLeft(0)
else:
list_of_boulders[0].moveLeft(1)
self.boulders.update()

self.screen.fill(self.WHITE)
self.boulders.draw(self.screen)
self.ice_patches.draw(self.screen)
self.all_penguins.draw(self.screen)
pygame.display.update()
clock.tick(60)
pygame.quit()




if __name__ == '__main__':
game = Sled_Main()
game.main_loop()


# running = True
# while running:
# for event in pygame.event.get():
# if event.type == QUIT:
# running = False
# time.sleep(.001)
#
# pygame.quit()
Binary file added fallen_penguin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fallen_penguin.v2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fallen_penguin_smol.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added fallen_penguin_smol_demanding_diego.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added finish_line.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ice_patch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ice_patch_flat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added log(1).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added log_jump.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added log_jump_test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added penguin.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added penguin_smol.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added penguin_smoler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading