bouncy test program with pygame
--HG-- branch : bouncy
This commit is contained in:
commit
b20616146d
1 changed files with 110 additions and 0 deletions
110
bouncy.py
Normal file
110
bouncy.py
Normal file
|
@ -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()
|
Loading…
Add table
Reference in a new issue