diff --git a/README.md b/README.md index 61ec120..b02c532 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/bgload.py b/bgload.py new file mode 100644 index 0000000..c7f4bf9 --- /dev/null +++ b/bgload.py @@ -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() diff --git a/pyman/README.md b/pyman/README.md new file mode 100644 index 0000000..6f89ec0 --- /dev/null +++ b/pyman/README.md @@ -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! diff --git a/pyman/data/PyMan.py b/pyman/data/PyMan.py new file mode 100644 index 0000000..6c53846 --- /dev/null +++ b/pyman/data/PyMan.py @@ -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() diff --git a/pyman/data/helpers.py b/pyman/data/helpers.py new file mode 100644 index 0000000..b7954ce --- /dev/null +++ b/pyman/data/helpers.py @@ -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 diff --git a/pyman/data/images/heart.png b/pyman/data/images/heart.png new file mode 100644 index 0000000..f5a8886 Binary files /dev/null and b/pyman/data/images/heart.png differ diff --git a/pyman/data/images/logo.png b/pyman/data/images/logo.png new file mode 100644 index 0000000..a697eba Binary files /dev/null and b/pyman/data/images/logo.png differ diff --git a/pyman/data/images/pellet.png b/pyman/data/images/pellet.png new file mode 100644 index 0000000..78e40a1 Binary files /dev/null and b/pyman/data/images/pellet.png differ diff --git a/pyman/data/images/snake.png b/pyman/data/images/snake.png new file mode 100644 index 0000000..ef58933 Binary files /dev/null and b/pyman/data/images/snake.png differ diff --git a/pyman/data/images/sphere.png b/pyman/data/images/sphere.png new file mode 100644 index 0000000..c1ba682 Binary files /dev/null and b/pyman/data/images/sphere.png differ