Skip to content

Instantly share code, notes, and snippets.

@oleiade
Created November 21, 2011 14:28
Show Gist options
  • Save oleiade/1382765 to your computer and use it in GitHub Desktop.
Save oleiade/1382765 to your computer and use it in GitHub Desktop.
Flies will fly
#!/usr/bin/env python
import time
import random
import sys
import pygame
from pygame import gfxdraw
from pygame.time import Clock
from pygame import DOUBLEBUF, HWSURFACE
from pygame.locals import *
from pygame.color import THECOLORS
class Fly:
def __init__(self, screen, fly_size, velocity, bg_color):
self.screen = screen
self.screenwidth, self.screenheight = self.screen.get_size()
self.fly_height, self.fly_width = fly_size
# Calculate the max x and y position for our fly,
# in order for it not to get out of screen
self.absolute_max_x = self.screenwidth - self.fly_width
self.absolute_max_y = self.screenheight - self.fly_height
self.absolute_min_x, self.absolute_min_y = 0, 0
# minus the fly width/height in order to be able to still
# place a fly when the max_x/y is very high
self.min_x = random.randint(self.absolute_min_x, int(self.absolute_max_y * 0.5))
self.max_x = random.randint(int(self.absolute_max_x * 0.5), self.absolute_max_x)
self.min_y = random.randint(self.absolute_min_y, int(self.absolute_min_y * 0.5))
self.max_y = random.randint(int(self.absolute_max_y * 0.5), self.absolute_max_y)
# Generating a random (inscreen) x and y fly position
self.x, self.y = self.genRandomPos(self.max_x, self.max_y)
self.vel_x, self.vel_y = velocity
self.bg_color = bg_color
self.fly = pygame.rect.Rect(self.x, self.y,
self.fly_height, self.fly_width)
def genRandomLimits(self, min_value,
max_value, fly_border_size):
return random.randint(min_value, max_value * fly_border_size)
def genRandomPos(self, max_x, max_y):
rand_x = random.randint(0, max_x)
rand_y = random.randint(0, max_y)
return rand_x, rand_y
def clearScreen(self, screen, color, rect):
pygame.draw.rect(screen, color, rect)
return
def updateFlyPosition(self):
"""
"""
new_x, new_y = self.x + self.vel_x, self.y + self.vel_y
# If reaching and x border, invert the movement
# by inverting the velocity
if new_x >= self.max_x or new_x <= self.min_x:
self.max_x = random.randint(int(self.absolute_max_x * 0.5), self.absolute_max_x)
self.min_x = random.randint(self.absolute_min_x, int(self.absolute_max_y * 0.5))
self.vel_x *= -1
# print "new min_x: %s ; new max_x : %s\n" % (self.min_x, self.max_x)
else:
self.x = new_x
if new_y >= self.max_y or new_y <= self.min_y:
self.max_y = random.randint(int(self.absolute_max_y * 0.5), self.absolute_max_y)
self.min_y = random.randint(self.absolute_min_y, int(self.absolute_min_y * 0.5))
self.vel_y *= -1
# print "new min_y: %s ; new max_y : %s\n" % (self.min_y, self.max_y)
else:
self.y = new_y
return new_x, new_y
def draw(self):
# Updating position
new_x, new_y = self.updateFlyPosition()
# Drawing new fly position
self.fly = pygame.rect.Rect(new_x, new_y,
self.fly_height, self.fly_width)
pygame.draw.rect(self.screen, THECOLORS["black"], self.fly)
def main():
WINSIZE = 160, 120
pygame.init()
screen = pygame.display.set_mode(WINSIZE, DOUBLEBUF|HWSURFACE, 16)
pygame.display.set_caption('Flies from limbes')
screen.fill(THECOLORS["white"])
first_fly = Fly(screen, (8, 8), (1, 1),
THECOLORS["white"])
fps = 60
clock = Clock()
while 42:
events = pygame.event.get()
for e in events:
if (e.type == QUIT):
sys.exit()
elif (e.type == KEYDOWN):
if (e.key == K_ESCAPE):
sys.exit()
if (e.key == K_f):
pygame.display.toggle_fullscreen()
screen.fill(THECOLORS["white"])
first_fly.draw()
pygame.display.flip()
clock.tick(fps)
print "Exiting!"
return
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment