-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmetagame.py
140 lines (119 loc) · 4.5 KB
/
metagame.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# metagame.py - backdrops and gamechunk parent classes
import pygame, sys, os
from pygame.locals import *
from math import *
from random import random
twopi = 2*pi
def randomcolor( targetbrightness = 200):
Rcolor = int( random() * 300 )
Gcolor = int( random() * 300 )
Bcolor = int( random() * 300 )
norm = 1.0*targetbrightness / (Rcolor + Gcolor + Bcolor)
Rcolor = min(int(norm*Rcolor), 255)
Gcolor = min(int(norm*Gcolor), 255)
Bcolor = min(int(norm*Bcolor), 255)
return (Rcolor, Gcolor, Bcolor)
def randomphase():
return random()*twopi
def Error(msg):
pygame.quit()
sys.exit(msg)
def Warn(msg):
print "WARNING!", msg
class BackDropClass:
def __init__( self, **kwargs ):
self.allowedchanges = []
self.image = 0
def setstate( self, **kwargs ):
for key, value in kwargs.iteritems():
if key in self.allowedchanges:
setattr( self, key, value )
else:
Warn("in BackDropClass:setstate - key "+ key +" is protected!!")
def update( self, dt ):
pass
def addimage( self, image, loc="center" ):
self.image = image
self.imagerect = self.image.get_rect()
self.imageloc = loc
def drawimage( self, screen ):
if self.image:
screenwidth, screenheight = screen.get_size()
if self.imageloc == "center":
self.imagerect.centerx = screenwidth*0.5
self.imagerect.centery = screenheight*0.5
elif self.imageloc == "centerright":
self.imagerect.right = screenwidth-10
self.imagerect.centery = screenheight*0.5
elif self.imageloc == "centerleft":
self.imagerect.left = 10
self.imagerect.centery = screenheight*0.5
elif self.imageloc == "bottomright":
self.imagerect.right = screenwidth-10
self.imagerect.bottom = screenheight-10
screen.blit( self.image, self.imagerect )
def draw( self, screen ):
screen.fill( (0,0,0) )
self.drawimage( screen )
class GameChunkClass:
def __init__( self ):
self.backdrop = BackDropClass()
def update( self, dt, midi ):
self.backdrop.update( dt )
def setbackspaceaction( self, action ):
self.backspaceaction = action
def process( self, event, midi ):
return {}
def processmidi( self, midi ):
return {}
def draw( self, screen ):
#black out screen
self.backdrop.draw( screen )
#draw stuff... (none for default)
def quit( self ):
pass
class GameElementClass:
def __init__( self, **kwargs ):
self.allowedchanges = []
def setstate( self, **kwargs ):
for key, value in kwargs.iteritems():
if key in self.allowedchanges:
setattr( self, key, value )
else:
Warn("in GameElementClass:setstate - key "+ key +" is protected!!")
def update( self, dt ):
pass
def draw( self, screen, x, y ):
pass
class PianoKeyClass( GameElementClass ):
def __init__( self, **kwargs ):
self.allowedchanges = [ "on", #set to the notes velocity when struck
"fadespeed", # how quickly the note's "on" deteriorates
"width",
"white",
"length",
"whitekeyindex",
"fillcoloroff",
"fillcoloron" ]
# setting defaults
self.on = 0
self.fadespeed = 0.06
self.width = 30
self.length = 100
self.whitekeyindex = 0
self.white = True
self.fillcoloroff = (200,200,200)
self.fillcoloron = (255,100,100)
# now set whatever the user tells you to
self.setstate( **kwargs )
self.fillcolor = (0,0,0) #will be set later
def update( self, dt ):
if self.on > 0:
self.on -= self.fadespeed * dt
else:
self.on = 0
#max velocity is 127
onpercentage = self.on * 1.0 / 127
self.fillcolor = ( self.fillcoloron[0]*onpercentage + self.fillcoloroff[0]*(1-onpercentage),
self.fillcoloron[1]*onpercentage + self.fillcoloroff[1]*(1-onpercentage),
self.fillcoloron[2]*onpercentage + self.fillcoloroff[2]*(1-onpercentage) )