-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasciiart.py
140 lines (121 loc) · 3.79 KB
/
asciiart.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
140
import pygame
import anim
def makeImg(buff, colors, pixel_size=1):
if type(buff) == str:
buff = buff.split('\n')
maxLen = 0
for ln in buff:
if len(ln) > maxLen:
maxLen = len(ln)
s = pygame.Surface((pixel_size*maxLen, pixel_size*len(buff)), )
s.fill((255,0,255))
for y,ln in enumerate(buff):
for x,px in enumerate(ln):
if px == ' ':
continue
if colors.has_key(px):
c = colors[px]
else:
c = (0,0,0)
s.fill(c, (x*pixel_size,y*pixel_size, pixel_size, pixel_size))
#endfor
#endfor
s.set_colorkey((255, 0, 255), pygame.RLEACCEL)
return s.convert()
#enddef
def load(fName, pixel_size=1):
colors = {
'W': (255,255,255), 'w': (128,128,128),
'R': (255,0,0), 'r': (128,0,0),
'G': (0,255,0), 'g': (0,128,0),
'B': (0,0,255), 'b': (0,0,128) }
out = {}
oName = ''
buff = []
duration = 1
f = open(fName, 'r')
lnNo = 0
for ln in f:
lnNo += 1
if ln.startswith('#'):
continue
ln = ln.rstrip()
if ln=='':
if buff and oName:
out[oName].append(makeImg(buff, colors, pixel_size), duration)
buff = []
continue
if ln.startswith('@'):
if ln.startswith('@duration'):
duration = int(ln[9:].strip())
elif ln.startswith('@name'):
oName = ln[5:].strip()
out[oName] = anim.Animation()
elif ln.startswith('@end') and oName:
out[oName].cycle = False
elif ln.startswith('@color'):
(ch, r, g, b) = ln[6:].strip().split(' ')
colors[ch] = (int(r), int(g), int(b))
else:
raise "Wrong directive '%s' on line %d, name '%s'"\
%(ln, lnNo, oName)
continue
#endif
buff.append(ln.strip("'"))
#endfor
if buff and oName:
out[oName].append(makeImg(buff), duration)
return out
#enddef
if __name__ == "__main__":
import sys
pygame.init()
screen = pygame.display.set_mode((640,480), pygame.HWSURFACE|pygame.DOUBLEBUF)
clock = pygame.time.Clock()
aag = load('./graphics.txt', 4)
cavySprite = anim.Slot(aag['cavy'])
cavySprite2 = anim.Slot(aag['cavy'])
cavySprite2.rewind(20)
invaderSprite = anim.Slot(aag['invader'])
cx = 0
cy = 300
left = False
right = False
up = False
down = False
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit(0)
if event.type == pygame.KEYDOWN:
key = event.key
if key==27: sys.exit(0) #self.exit = True
if key==273: up = True
if key==274: down = True
if key==276: left = True
if key==275: right = True
# if key==305: self.fire = True
if event.type == pygame.KEYUP:
key = event.key
if key==273: up = False
if key==274: down = False
if key==276: left = False
if key==275: right = False
if event.type == pygame.MOUSEBUTTONDOWN:
#print pygame.mouse.get_pressed()
(cx, cy) = pygame.mouse.get_pos()
#endfor
if up: cy -= 1
if down: cy += 1
if left: cx -= 1
if right: cx += 1
screen.fill((0,0,0))
cavySprite.draw(screen, cx, cy)
invaderSprite.draw(screen, 0, 350)
cavySprite2.draw(screen, 100, 350)
pygame.display.flip()
clock.tick(30)
cavySprite.behave()
cavySprite2.behave()
invaderSprite.behave()
#endwhile
#endmain