From b20616146d924c03ff34d8381c8bee92969c23f6 Mon Sep 17 00:00:00 2001 From: cecilkorik Date: Wed, 8 Feb 2006 07:21:37 +0000 Subject: [PATCH] bouncy test program with pygame --HG-- branch : bouncy --- bouncy.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 bouncy.py diff --git a/bouncy.py b/bouncy.py new file mode 100644 index 0000000..d787840 --- /dev/null +++ b/bouncy.py @@ -0,0 +1,110 @@ +import pygame +from pygame.locals import * +import os +import sys +import math +import random + + +pygame.init() + +def handle_keys(keys): + global bounce, speed_player, trails, dir_player, color_player + for key in keys: + if key == K_b: + bounce = not bounce + elif key == K_q: + sys.exit(0) + elif key == K_c: + color_player = (random.randint(0,255), random.randint(0,255), random.randint(0,255)) + elif key == K_t: + trails = not trails + elif key == K_UP: + speed_player += 0.01 + elif key == K_DOWN: + speed_player -= 0.01 + elif key == K_LEFT: + dir_player -= (math.pi / 180.0) + elif key == K_RIGHT: + dir_player += (math.pi / 180.0) + +def move(coords, dir, mag): + return [coords[0] + (math.sin(dir) * mag), coords[1] - (math.cos(dir) * mag)] + +def limitdir(dir): + while dir < 0: + dir += math.pi*2.0 + while dir > math.pi*2.0: + dir -= math.pi*2.0 + return dir + +def bouncedir(normal, dir): + inv_normal = limitdir(normal + math.pi) + return limitdir(normal + (inv_normal - dir)) + + +loc_player = [100.0,150.0] +dir_player = math.pi * 0.80 + +size = width, height = (640,480) +scr = pygame.display.set_mode(size) + +#options +color_player = (255,255,255) +bounce = True +speed_player = 5.0 +trails = False + +scr.fill((0,0,0)) + +keys = [] + +while True: + events = pygame.event.get((KEYDOWN, KEYUP)) + for ev in events: + if ev.type == KEYUP: + try: + del keys[keys.index(ev.key)] + except IndexError: + pass + else: + keys += [ev.key] + + + handle_keys(keys) + pygame.event.pump() + + loc_new = move(loc_player, dir_player, speed_player) + + if loc_new[0] < 5: + if not bounce: + loc_new[0] = width-6 + else: + dir_player = bouncedir(math.pi*0.5, dir_player) + elif loc_new[1] < 5: + if not bounce: + loc_new[1] = height-6 + else: + dir_player = bouncedir(math.pi, dir_player) + elif loc_new[0] > width - 5: + if not bounce: + loc_new[0] = 6 + else: + dir_player = bouncedir(math.pi*1.5, dir_player) + elif loc_new[1] > height - 5: + if not bounce: + loc_new[1] = 6 + else: + dir_player = bouncedir(0, dir_player) + + if bounce: + loc_new = move(loc_player, dir_player, speed_player) + + if not trails: + pygame.draw.circle(scr, (0,0,0), loc_player, 2, 1) + pygame.draw.circle(scr, color_player, loc_new, 2, 1) + + + loc_player = loc_new + + pygame.display.flip() \ No newline at end of file