This repository has been archived by the owner on May 30, 2024. It is now read-only.
forked from jav/pybotwar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgview.py
187 lines (151 loc) · 5 KB
/
pgview.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copyright 2009-2014 Lee Harr
#
# This file is part of pybotwar.
# http://pybotwar.googlecode.com/
#
# Pybotwar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pybotwar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Pybotwar. If not, see <http://www.gnu.org/licenses/>.
import pygame
from pygsear.Game import Game
from pygsear.Drawable import RotatedImage, Square, Rectangle, Stationary, Multi, Image, String, Circle
from pygsear.Widget import ProgressBar
from pygsear.locals import RED, ORANGE, YELLOW
import pygsear.conf
pygsear.conf.MAX_FPS = 60
m = 15 # 1 meter
size = 2 * m
import conf
def trans(pos):
try:
px, py = pos.x, pos.y
except AttributeError:
px, py = pos[0], pos[1]
sz = size / 2
x, y = (px*sz)+300, (py*sz)+300
return x, y
def scale(s):
w, h = s
sw, sh = (w*size), (h*size)
return sw, sh
class Robot(RotatedImage):
nrobots = 0
def __init__(self, pos, ang):
Robot.nrobots += 1
filename = 'r{0:02d}.png'.format(Robot.nrobots)
steps = 360
RotatedImage.__init__(self, filename=filename, steps=steps)
if size != 30:
self.stretch(size=(size, size))
self.turr = Turret(pos, 0)
self.setpos(pos)
self.set_rotation(ang)
def setpos(self, pos):
x, y = trans(pos)
self.set_position(x, y)
self.turr.set_position(x, y)
def set_turr_rot(self, ang):
self.turr.set_rotation(self.rotation+ang)
def kill(self):
self.turr.kill()
RotatedImage.kill(self)
class Turret(RotatedImage):
def __init__(self, pos, ang):
filename = 'turret.png'
steps = 360
RotatedImage.__init__(self, filename=filename, steps=steps)
#self.setpos(pos)
#self.set_rotation(ang)
class HealthBar(ProgressBar):
def __init__(self, n):
ProgressBar.__init__(self, width=150, steps=conf.maxhealth,
position=(640, (60*n+34)), color=(100,100,100))
def step(self, steps):
self.stepsLeft -= steps-1
ProgressBar.step(self)
if self.stepsLeft <= 0.30 * self.steps:
self.set_color(RED)
self.show()
class RobotInfo(object):
def __init__(self, n, name):
self.n = n
filename = 'r{0:02d}.png'.format(n)
iconsprite = Image(filename=filename)
self.icon = Stationary(sprite=iconsprite)
self.icon.set_position((630, (60*n+0)))
self.icon.draw()
namesprite = String(message=name, fontSize=25)
self.name = Stationary(sprite=namesprite)
self.name.set_position((680, (60*self.n+10)))
self.name.draw()
health = HealthBar(n)
self.health = health
class Bullet(Square):
def __init__(self, pos):
Square.__init__(self, side=2)
self.setpos(pos)
def setpos(self, pos):
x, y = trans(pos)
self.set_position(x, y)
class Explosion(Circle):
def __init__(self, pos):
r = conf.explosion_radii[-1] # largest explosion radius
rt = r * m
Circle.__init__(self, radius=rt)
self.setpos(pos)
def setpos(self, pos):
x, y = trans(pos)
self.set_position(x-45, y-45)
def paint(self):
image = self.image
x, y = int(self.radius), int(self.radius)
r3 = zip(conf.explosion_radii, (RED, ORANGE, YELLOW))
for r, color in r3:
rt = int(m * r)
pygame.draw.circle(image, color, (x, y), rt, 2)
class Wall(Stationary):
def __init__(self, pos, size):
cx, cy = trans(pos)
w, h = scale(size)
x = cx-(w/2)
y = cy-(h/2)
sq = Rectangle(width=w, height=h, color=(120, 50, 50))
Stationary.__init__(self, sprite=sq)
self.set_position((x, y))
self.draw()
class Arena(Game):
splash_filename = 'splash.png'
def __init__(self):
Game.__init__(self)
self.set_background(color=(40, 40, 80))
self.set_title('pybotwar')
titleicon = String(message="pybotwar", fontSize=32)
title = Stationary(sprite=titleicon)
title.set_position((660, 10))
title.draw()
def addrobot(self, pos, ang):
v = Robot(pos, ang)
self.sprites.add(v)
self.sprites.add(v.turr, level=1)
return v
def addrobotinfo(self, n, name):
i = RobotInfo(n, name)
self.sprites.add(i.health)
return i
def addbullet(self, pos):
v = Bullet(pos)
self.sprites.add(v)
return v
def addexplosion(self, pos):
e = Explosion(pos)
self.sprites.add(e)
return e