Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 19 additions & 0 deletions Arena.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#This will be the boundary in which our snake navigates

class Arena(object):
"""
Defines a boundary object to confine the snake

Takes attributes height, width, padding
padding refers to wall thickness
"""

def __init__(self, width=640, height=800, padding = 20):
self.height = height
self.width = width
self.x = 0
self.y = 0
self.padding = padding

def __str__(self):
return "The Boundary is %f by %f and is %f units thick" % (self.height, self.width, self.padding)
Binary file added Interactive Programming Reflection.pdf
Binary file not shown.
44 changes: 44 additions & 0 deletions Project Proposal.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Project Proposal\n",
"\n",
"For our project we will create a standard calculator snake game, where the snake has to capture a mouse and its movement is controlled by arrow keys. The snake must not contact its own body or the boundary. \n",
"\n",
"Junwon's learning goal: Get familiar with pair programming and Pygame.\n",
"\n",
"Cusai's learning goal: Get familiar with creating user interfacing interactive elements\n",
"\n",
"Libraries that will be used: Pygame. More libraries will be used if needed.\n",
"\n",
"By the mid-project check in, we will try to create the frame of the game, where it will contain the space that contains the space, the \"mouse,\" and the snake. We will create classes that generate these figures, but we probably won't figure out the game mechanics by then (movement of snake, catching the \"mouse\", etc).\n",
"\n",
"The biggest risk is working on Github with someone else before. We hope that we don't have merge conflicts with the program and drag us from working on this project. "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,25 @@
# InteractiveProgramming
This is the base repo for the interactive programming project for Software Design, Spring 2018 at Olin College.

Note: Pygame must be installed to operate this code.

To do so, type the following code in the command window:

$ pip install pygame

To run the code, type the following to the command window:

$ python SnakeWithBoundary.py

To play the game:

1. Press Enter to start the game.

2. Control the snake with keyboard arrows. DO NOT COLLIDE WITH THE BLUE BOUNDARY OR YOURSELF.

3. Catch the "mouse." (the red block). Once you catch the mouse, your length will increase and the mouse will be placed in random location.

4. To restart the game, close the window and type the given command again.


Link to project reflection: /home/junwonlee/GameProject/Link to Interactive Programming Reflection.pdf
181 changes: 181 additions & 0 deletions Snake.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import pygame
from pygame.locals import *
import time


class SnakeGameWindowView(object):

def __init__(self, model, width, height):
self.model = model
self.screen = pygame.display.set_mode((width, height))
def draw(self):
"""
This function in the class will draw necessary materials
and update every change in the window.
"""

self.screen.fill(pygame.Color(0, 0, 0))
for material in self.model.Mouse:
pygame.draw.rect(self.screen,
pygame.Color(255, 0, 0),
pygame.Rect(material.x, material.y, material.height, material.width))
for material in self.model.Snake:
pygame.draw.rect(self.screen,
pygame.Color(0, 255, 0),
pygame.Rect(material.x, material.y, material.height, material.width))
pygame.display.update()

class SnakeGameModel(object):

def __init__(self):
"""This funciton will create dimensions for
the blocks for the snake and the mouse."""
self.Mouse = []
self.Snake = []
snake = Snake(100, 10, 300, 300)
self.Mouse.append(Mouse(10, 10, 100, 100))
for i in range(int(snake.height / snake.width)):
self.Snake.append(Snake(snake.height / snake.width, 10, i*snake.width+snake.x, snake.y))
def update(self):
"""
This function will update the function.
"""
for part in self.Snake:
part.update()
def __str__(self):
output_lines = []
for p in self.Mouse:
output_lines.append(str(p))
for p in self.Snake:
output_lines.append(str(p))
return "\n".join(output_lines)

class Mouse(object):

def __init__(self, height, width, x, y):
"""
This creates a mouse object.
"""
self.height = height
self.width = width
self.x = x
self.y = y

def __str__(self):
return 'Mouse is in %f, %f' % (self.x, self.y)

class Snake(object):

def __init__(self, height, width, x, y):
"""
This creates a snake object with position, length, width, and velocity.
"""
self.height = height
self.width = width
self.x = x
self.y = y
self.vx = 0.0
self.vy = 0.0

def update(self):
self.x += self.vx
self.y += self.vy
def __str__(self):
return 'Snake is in %f, %f and is %f long.' % (self.x, self.y, self.height)

class SnakeController(object):

def __init__(self, model):
self.model = model

def handle_event(self, event):
"""
This creates different operations for different keys in keyboard.
"""
if event.type != KEYDOWN:
return
if event.key == pygame.K_DOWN:
if abs(self.model.Snake[-1].vy) == -.1:
return
if self.model.Snake[-1].vx == -.1:
self.model.Snake.reverse()
while self.model.Snake[0].x != self.model.Snake[-1].x:
for i in range(len(self.model.Snake)-1):
self.model.Snake[i].x += self.model.Snake[i+1].x - self.model.Snake[i].x
self.model.Snake[i].y += self.model.Snake[i+1].y - self.model.Snake[i].y
if self.model.Snake[i].x == self.model.Snake[-1].x:
self.model.Snake[i].y += self.model.Snake[i].width

if self.model.Snake[0].x == self.model.Snake[-1].x:
for part in self.model.Snake:
part.vy = .1
part.vx = 0


if event.key == pygame.K_LEFT:
if abs(self.model.Snake[-1].vx) == -.1:
return
if self.model.Snake[-1].vy == .1:
self.model.Snake.reverse()
while self.model.Snake[0].y != self.model.Snake[-1].y:
for i in range(len(self.model.Snake)-1):
self.model.Snake[i].x += self.model.Snake[i+1].x - self.model.Snake[i].x
self.model.Snake[i].y += self.model.Snake[i+1].y - self.model.Snake[i].y
if self.model.Snake[i].y == self.model.Snake[-1].y:
self.model.Snake[i].x -= self.model.Snake[i].width

if self.model.Snake[0].y == self.model.Snake[-1].y:
for part in self.model.Snake:
part.vy = 0
part.vx = -.1

if event.key == pygame.K_RIGHT:
if abs(self.model.Snake[-1].vx) == -.1:
return
if self.model.Snake[-1].vy == -.1:
self.model.Snake.reverse()
while self.model.Snake[0].y != self.model.Snake[-1].y:
for i in range(len(self.model.Snake)-1):
self.model.Snake[i].x += self.model.Snake[i+1].x - self.model.Snake[i].x
self.model.Snake[i].y += self.model.Snake[i+1].y - self.model.Snake[i].y
if self.model.Snake[i].y == self.model.Snake[-1].y:
self.model.Snake[i].x += self.model.Snake[i].width

if self.model.Snake[0].y == self.model.Snake[-1].y:
for part in self.model.Snake:
part.vy = 0
part.vx = .1

if event.key == pygame.K_UP:
if abs(self.model.Snake[-1].vy) == -.1:
return
if self.model.Snake[-1].vx == -.1:
self.model.Snake.reverse()
while self.model.Snake[0].x != self.model.Snake[-1].x:
for i in range(len(self.model.Snake)-1):
self.model.Snake[i].x += self.model.Snake[i+1].x - self.model.Snake[i].x
self.model.Snake[i].y += self.model.Snake[i+1].y - self.model.Snake[i].y
if self.model.Snake[i].x == self.model.Snake[-1].x:
self.model.Snake[i].y -= self.model.Snake[i].width

if self.model.Snake[0].x == self.model.Snake[-1].x:
for part in self.model.Snake:
part.vx = 0
part.vy = -.1

if __name__ == '__main__':
pygame.init()
model = SnakeGameModel()
window = SnakeGameWindowView(model, 640, 800)
run = True
control = SnakeController(model)

while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
control.handle_event(event)
model.update()
window.draw()
time.sleep(0.001)
pygame.quit()
Loading