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
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# InteractiveProgramming
This is the base repo for the interactive programming project for Software Design, Spring 2016 at Olin College.

Project Proposal - Ilya Besancon
02/23/17
What is the main idea of your project? What topics will you explore and what will you generate? What is your minimum viable product? What is a stretch goal?
The main idea is to create a game similar to snake. I want to explore different forms of user input, anything past using arrow keys for motion. The MVP is to make the regular snake game without having it change length. Our stretch goals are: growing snake from eating blocks, different color blocks that change the color of the snake, and high scores from how many blocks it eats.
What are your learning goals for this project (for each member)?
-Better understand objects and classes
-Have an end product that we enjoy
-Push ourselves to be more creative and better coders through making this project
What libraries are you planning to use? (if you don’t know enough yet, please outline how you will decide this question during the beginning phase of the project).
We are going to primarily use pygame for this project.
What do you plan to accomplish by the mid-project check-in? (See below for some generic goals; edit to suit your particular project)
We want to have a basic but running version of the game snake by the mid-project check-in.
What do you view as the biggest risks to you being successful on this project?
Current lack of comfort in understanding classes and objects.

This project was then modified to be a game where the snake is a cartoon that eats hearts, which are counted. User interaction involves arrow keys for motion of the snake, and the letters r and q for restarting and quiting the game.
81 changes: 81 additions & 0 deletions bgload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import pygame
from pygame.locals import*
import os, sys
# testing
class Node:
def __init__(self,x,y,width,height,next_node=None):
self.x=x
self.y=y
self.width=width
self.height=height
self.next_node=next_node

img = pygame.image.load('snake.png')
self.img=pygame.transform.scale(img,(self.width,self.height))

def render(self):
screen.blit(self.img,(self.x, self.y))
pygame.display.update()

class LinkedList:
def __init__(self,head,size):
self.head=head
self.size=size
current = head;
for i in range(size):
newnode=Node(current.x-100,current.y,100,100,None)
current.next_node = newnode;
current = current.next_node;
def render(self):
current = self.head
for i in range(self.size):
render(current)
current = current.next_node

def movetail(self):
current=head
while current.next_node!=None:
current = current.next_node
current.next_node=None
newhead=Node(head.x+100,head.y,100,100,self.head)
self.head=newhead


class Snake:
def __init__(self,x,y,width,height):



def move(self, direction, distance):
if direction==pygame.K_RIGHT:
self.x+=distance
if direction==pygame.K_DOWN:
self.y+=distance
if direction==pygame.K_UP:
self.y-=distance
if direction==pygame.K_LEFT:
self.x-=distance

if __name__ == "__main__":
pygame.init()
screen = pygame.display.set_mode((800, 600)) # window size, as a tuple
pygame.display.set_caption('PyGame App!')
# snake = Snake(0, 0, 100, 100)
# models=[snake]
head = Node(0, 0, 100, 100, None)
snakelist = LinkedList(head, 3)
models=[snakelist]
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT: # user presses the x-button
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
else:
# snake.move(event.key, 3)
snakelist.movetail()
screen.fill((0,0,0))
snakelist.render()
pygame.quit()
3 changes: 3 additions & 0 deletions pyman/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# pyman
@ilya-besancon
This is a simple game where the snake moves aroound and eats the yellow hearts in the grid!
155 changes: 155 additions & 0 deletions pyman/data/PyMan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
"""
@ilya-besancon
A simple game where a snake eats oranges!
"""

import os, sys
import pygame
import pygame.locals
from helpers import*
# from helpers import *

if not pygame.font:
print('Warning, fonts disabled')
if not pygame.mixer:
print('Warning, sound disabled')


class PyManMain:
""" The Main PyMan Class – This class handles the
main initialization and creating of the Game"""

def __init__(self, width=640, height=480):
""" Initialize
Initialize PyGame """
pygame.init()
# Set the window Size
self.width = width
self.height = height
# Create the Screen
self.screen = pygame.display.set_mode((self.width, self.height))

def MainLoop(self):
""" This is the Main Loop of the Game """
""" Load All of our Sprites """
self.LoadSprites()
"""tell pygame to keep sending up keystrokes when they are
held down"""
pygame.key.set_repeat(100, 20)
"""Create the background"""
self.background = pygame.Surface(self.screen.get_size())
self.background = self.background.convert()
self.background.fill((0, 0, 0))

while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == KEYDOWN:
if ((event.key == K_RIGHT)
or (event.key == K_LEFT)
or (event.key == K_UP)
or (event.key == K_DOWN)):
self.snake.move(event.key)
if (event.key == K_q):
sys.exit()
if (event.key == K_r):
MainWindow.MainLoop()

"""Check for collision"""
lstCols = pygame.sprite.spritecollide(self.snake
, self.pellet_sprites
, True)

"""Update the amount of pellets eaten"""
self.snake.pellets = self.snake.pellets + len(lstCols)

"""Do the Drawging"""
self.screen.blit(self.background, (0, 0))
if pygame.font:
font = pygame.font.Font(None, 45)
text = font.render("Hearts: %s" %
self.snake.pellets, 1, (32, 178, 170))
textpos = text.get_rect(centerx=self.background.get_width()/2,
centery=20)
self.screen.blit(text, textpos)
Vert = int(self.width/73)
Horz = int(self.height/73)
victory_y = 200
offset = 50
if self.snake.pellets == Vert * Horz:
font = pygame.font.Font(None, 100)
victory = font.render('You Won!', 1, (95, 168, 160))
textpos_vic = victory.get_rect(centerx=self.background.get_width()/2,
centery=victory_y)
font_res = pygame.font.Font(None, 40)
restart = font_res.render('(Hit r to restart)', 1, (176, 226, 230))
textpos_res = restart.get_rect(centerx=self.background.get_width()/2,
centery=victory_y + offset)
quit = font_res.render('(Hit q to quit)', 1, (176, 226, 230))
textpos_quit = quit.get_rect(centerx=self.background.get_width()/2,
centery=victory_y + 2*offset)
self.screen.blit(victory, textpos_vic)
self.screen.blit(restart, textpos_res)
self.screen.blit(quit, textpos_quit)
# pygame.display.flip()
self.pellet_sprites.draw(self.screen)
self.snake_sprites.draw(self.screen)
pygame.display.flip()

def LoadSprites(self):
# Load the sprites that we need
self.snake = Snake()
self.snake_sprites = pygame.sprite.RenderPlain((self.snake))
# figure out how many pellets we can display:
nNumHorizontal = int(self.width/73)
nNumVertical = int(self.height/73)
# Create the Pellet group:
self.pellet_sprites = pygame.sprite.Group()
# Create all of the pellets and add them to the pellet_sprites group:
for x in range(nNumHorizontal):
for y in range(nNumVertical):
self.pellet_sprites.add(Pellet(
pygame.Rect(x*(64 + 10) + 10, y*(64 +10) + 35, 64, 64)))


class Snake(pygame.sprite.Sprite):
"""This is our snake that will move around the screen """

def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('snake.png', -1)
self.pellets = 0
self.x_dist = 10
self.y_dist = 10

def move(self, key):
""" Move your self in one of the 4 directions according to key
Key is the pyGame define for either up,down,left, or right key
we will adjust ourselves in that direction """
xMove = 0
yMove = 0
if (key == K_RIGHT):
xMove = self.x_dist
elif (key == K_LEFT):
xMove = -self.x_dist
elif (key == K_UP):
yMove = -self.y_dist
elif (key == K_DOWN):
yMove = self.y_dist
self.rect.move_ip(xMove, yMove)


class Pellet(pygame.sprite.Sprite):
""" These are the pellets that the snake will eat! """

def __init__(self, rect=None):
pygame.sprite.Sprite.__init__(self)
self.image, self.rect = load_image('heart.png', -1)
if rect != None:
self.rect = rect


if __name__ == "__main__":
MainWindow = PyManMain(1000,1000)
MainWindow.MainLoop()
38 changes: 38 additions & 0 deletions pyman/data/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""
@ilya-besancon
helper functions for PyMan
"""

import os, sys
import pygame
from pygame.locals import*


def load_image(name, colorkey=None):
fullname = os.path.join('data', name)
try:
# image = pygame.image.load(fullname)
image = pygame.image.load(os.path.join("images", name)).convert()
except pygame.error as message:
print('Cannot load image:', name)
raise SystemExit(message)
image = image.convert()
if colorkey is not None:
if colorkey is -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()


def load_sound(name):
class NoneSound:
def play(self): pass
if not pygame.mixer:
return NoneSound()
fullname = os.path.join('data', name)
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error as message:
print('Cannot load sound:', wav)
raise SystemExit(message)
return sound
Binary file added pyman/data/images/heart.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 pyman/data/images/logo.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 pyman/data/images/pellet.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 pyman/data/images/snake.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 pyman/data/images/sphere.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.