Skip to content

Instantly share code, notes, and snippets.

@Mekire
Last active March 8, 2016 00:02
Show Gist options
  • Save Mekire/5ca82b1c7f471cac2f08 to your computer and use it in GitHub Desktop.
Save Mekire/5ca82b1c7f471cac2f08 to your computer and use it in GitHub Desktop.
import pygame, sys, random
from pygame.locals import *
#set up pygame
pygame.init()
mainClock = pygame.time.Clock()
#set up the window
WINDOWIDTH = 600
height = 600
screen = pygame.display.set_mode((WINDOWIDTH, height), 0, 32)
pygame.display.set_caption('hungry hippos')
#set up direction variables
DOWNLEFT = 1
DOWNRIGHT = 1
UPLEFT = 1
UPRIGHT = 1
MOVESPEED = 5
#set up the colors
BLACK = (0, 0, 0)
PURPLE = (72, 61, 139)
WHITE = (255, 255, 255)
GREEN = (152, 251, 152)
ORANGE = (255, 99, 71)
#set up the square and food data structures
foodCounter = 0
NEWFOOD = 1
fsize = 10
class MyRect(object):
def __init__(self, x, y, l, vx, vy, color=(0, 0, 0)):
self.rect = pygame.Rect(x, y, l, l)
self.vx = vx
self.vy = vy
self.color = color
def move(self):
self.rect.x += self.vx
self.rect.y += self.vy
def draw(self, screen):
pygame.draw.rect(screen, self.color, self.rect)
class Square(MyRect):
def __init__(self, x, y, l, vx, vy, color=(0, 0, 0)):
super().__init__(x, y, l, vx, vy, color=color)
self.iw = 0
self.ih = 0
def registerIncrease(self, iw, ih):
self.iw += iw
self.ih += ih
if self.iw > 1:
self.rect.w += 1
self.iw -= 1
if self.ih > 1:
self.rect.h += 1
self.ih -= 1
def update(self, screen, foods):
# move the square data structure
self.move()
# check if the square has move out of the window
if self.rect.top < 0 or self.rect.bottom > height:
self.vy *= -1
self.rect.top = max(self.rect.top, 0)
self.rect.bottom = min(self.rect.bottom, height)
if self.rect.left < 0 or self.rect.right > WINDOWIDTH:
self.vx *= -1
self.rect.left = max(self.rect.left, 0)
self.rect.right = min(self.rect.right, WINDOWIDTH)
# draw the square onto the surface
self.draw(screen)
# check if the square has intersected with any food squares.
for food in foods:
if self.rect.colliderect(food.rect):
food.setVel(self)
self.registerIncrease(0.1, 0.1)
class Food(MyRect):
def setVel(self, square):
self.vx = square.vx
self.vy = square.vy
def update(self, screen, foods):
if self.vx != 0 or self.vy != 0:
# move the square data structure
self.move()
# check if the square has move out of the window
if self.rect.bottom < 0 or self.rect.top > height or self.rect.right < 0 or self.rect.left > WINDOWIDTH:
foods.remove(self)
# draw the square onto the surface
self.draw(screen)
square = Square(300, 100, 25, -MOVESPEED, MOVESPEED, color=ORANGE)
foods = []
for i in range(20):
foods.append(Food(random.randint(0, WINDOWIDTH - fsize), random.randint(0, height - fsize), fsize, 0, 0, color=PURPLE))
#run the game loop
while True:
# check for the QUIT event
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
foodCounter += 1
if foodCounter >= NEWFOOD:
# add new food
foodCounter = 0
foods.append(Food(random.randint(0, WINDOWIDTH - fsize), random.randint(0, height - fsize), fsize, 0, 0, color=PURPLE))
# draw the black background onto the surface
screen.fill(GREEN)
square.update(screen, foods)
# draw the food
for food in foods:
food.update(screen, foods)
# draw the window onto the screen
pygame.display.update()
mainClock.tick(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment