-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtanks.py
More file actions
208 lines (181 loc) · 7.35 KB
/
tanks.py
File metadata and controls
208 lines (181 loc) · 7.35 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import math
import pygame
class Tank:
def __init__(self, x = 100, y = 100, vx = 0, vy = 0, a = 500, b = 20):
"""Constructor of Tank class"""
"""self.a - acceleration"""
"""self.b - site of square"""
self.x, self.y, self.vx, self.vy, self.a, self.b = x, y, vx, vy, a, b
def update(self, game):
"""Update Player state"""
if game.pressed[pygame.K_LEFT]:
self.vx -= game.delta * self.a
self.vy = 0
if game.pressed[pygame.K_RIGHT]:
self.vx += game.delta * self.a
self.vy = 0
if game.pressed[pygame.K_UP]:
self.vy -= game.delta * self.a
self.vx = 0
if game.pressed[pygame.K_DOWN]:
self.vy += game.delta * self.a
self.vx = 0
self.vx -= game.delta * self.vx
self.vy -= game.delta * self.vy
self.x += self.vx * game.delta
self.y += self.vy * game.delta
"""Do not let Tank get out of the Game window"""
if self.x < self.b:
if self.vx < 0:
self.vx = 0
self.x = self.b
if self.y < self.b:
if self.vy < 0:
self.vy = 0
self.y = self.b
if self.x > game.width - self.b:
if self.vx > 0:
self.vx = 0
self.x = game.width - self.b
if self.y > game.height - self.b:
if self.vy > 0:
self.vy = 0
self.y = game.height - self.b
def render(self, game):
"""Draw Player on the Game window"""
pygame.draw.rect(game.screen, [150, 75, 0], [self.x - self.b, self.y - self.b, 50, 50], 0)
if game.pressed[pygame.K_RIGHT] and not game.pressed[pygame.K_DOWN] \
and not game.pressed[pygame.K_LEFT] and not game.pressed[pygame.K_UP]:
pygame.draw.rect(game.screen, [150, 100, 0], [self.x + 30, self.y, 20, 10], 0)
if game.pressed[pygame.K_DOWN] and not game.pressed[pygame.K_UP] \
and not game.pressed[pygame.K_RIGHT] and not game.pressed[pygame.K_LEFT]:
pygame.draw.rect(game.screen, [150, 100, 0], [self.x, self.y + 30, 10, 20], 0)
if game.pressed[pygame.K_LEFT] and not game.pressed[pygame.K_UP] \
and not game.pressed[pygame.K_RIGHT]:
pygame.draw.rect(game.screen, [150, 100, 0], [self.x - 40, self.y, 20, 10], 0)
if game.pressed[pygame.K_UP]:
pygame.draw.rect(game.screen, [150, 100, 0], [self.x, self.y - 40, 10, 20], 0)
class Bullet:
def __init__(self, flag = 0, x1 = 100, y1 = 100, vx1 = 0, vy1 = 0):
self.flag, self.x1, self.y1, self.vx1, self.vy1 = flag, x1, y1, vx1, vy1
def update(self, game):
"""Update Player state"""
if game.pressed[pygame.K_SPACE]:
if game.pressed[pygame.K_LEFT]:
self.vx1 = -1000
self.vy1 = 0
if game.pressed[pygame.K_RIGHT]:
self.vx1 = 1000
self.vy1 = 0
if game.pressed[pygame.K_UP]:
self.vy1 = -1000
self.vx1 = 0
if game.pressed[pygame.K_DOWN]:
self.vy1 = 1000
self.vx1 = 0
self.x1 += self.vx1 * game.delta
self.y1 += self.vy1 * game.delta
"""Do not let Bullet get out of the Game window"""
if self.x1 < -20:
if self.vx1 < 0:
self.vx1 = 0
self.x1 = -20
if self.y1 < -20:
if self.vy1 < 0:
self.vy1 = 0
self.y1 = -20
if self.x1 > game.width + 10:
if self.vx1 > 0:
self.vx1 = 0
self.x1 = game.width + 10
if self.y1 > game.height + 10:
if self.vy1 > 0:
self.vy1 = 0
self.y1 = game.height + 10
def shot(self, game):
"""Draw bullet on the Game window"""
if game.pressed[pygame.K_SPACE]:
self.x1, self.y1 = game.player.x, game.player.y
if game.pressed[pygame.K_RIGHT] or game.pressed[pygame.K_LEFT]:
pygame.draw.ellipse(game.screen, [200, 200, 200], [self.x1, self.y1, 20, 10], 0)
self.flag = 1
if game.pressed[pygame.K_UP] or game.pressed[pygame.K_DOWN]:
pygame.draw.ellipse(game.screen, [200, 200, 200], [self.x1, self.y1, 10, 20], 0)
self.flag = 0
if (game.pressed[pygame.K_UP] or game.pressed[pygame.K_DOWN]) and \
(game.pressed[pygame.K_RIGHT] or game.pressed[pygame.K_LEFT]):
pygame.draw.ellipse(game.screen, [150, 75, 0], [self.x1, self.y1, 20, 10], 0)
if self.flag == 1:
pygame.draw.ellipse(game.screen, [200, 200, 200], [self.x1, self.y1, 20, 10], 0)
if self.flag == 0:
pygame.draw.ellipse(game.screen, [200, 200, 200], [self.x1, self.y1, 10, 20], 0)
#if (self.x1 != game.player.x) or (self.y1 != game.player.y):
#class Hard_wall:
#def
class Game:
def tick(self):
"""Return time in seconds since previous call
and limit speed of the game to 50 fps"""
self.delta = self.clock.tick(50) / 1000.0
def __init__(self):
"""Constructor of the Game"""
self._running = True
self.size = self.width, self.height = 640, 400
# create main display - 640x400 window
# try to use hardware acceleration
self.screen = pygame.display.set_mode(self.size, pygame.HWSURFACE)
# set window caption
pygame.display.set_caption('TANKS')
# get object to help track time
self.clock = pygame.time.Clock()
# set default tool
self.tool = 'run'
self.player = Tank()
self.bullet = Bullet()
self.ar = pygame.PixelArray(self.screen)
def event_handler(self, event, game):
"""Handling one pygame event"""
if event.type == pygame.QUIT:
# close window event
self.exit()
elif event.type == pygame.KEYDOWN:
# keyboard event on press ESC
if event.key == pygame.K_ESCAPE:
self.exit()
#if event.type == pygame.KEYDOWN:
#if event.key == pygame.K_SPACE:
#self.bullet.x1 += 10 * game.delta
#self.bullet.y1 += 10 * game.delta
#game.bullet.x1, game.bullet.y1 = game.player.x, game.player.y
def move(self):
"""Here game objects update their positions"""
self.tick()
self.pressed = pygame.key.get_pressed()
self.player.update(self)
self.bullet.update(self)
def render(self):
"""Render the scene"""
self.screen.fill((0, 0, 0))
self.player.render(self)
self.bullet.shot(self)
self.ar[int(self.player.x/10.0),int(self.player.y/10.0)] = (200,200,200)
self.ar[int(self.bullet.x1/10.0),int(self.bullet.y1/10.0)] = (200,200,200)
pygame.display.flip()
def exit(self):
"""Exit the game"""
self._running = False
def cleanup(self):
"""Cleanup the Game"""
pygame.quit()
def execute(self):
"""Execution loop of the game"""
while(self._running):
# get all pygame events from queue
for event in pygame.event.get():
self.event_handler(event, game)
self.move()
self.render()
self.cleanup()
if __name__ == "__main__":
game = Game()
game.execute()