/
game.py
238 lines (178 loc) · 6.84 KB
/
game.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
from tkinter.constants import NONE
import turtle
from Game.Main.constants import *
from Game.Character.player import Player
from Game.Objects.arena import Arena
from Game.Objects.score_turtle import ScoreTurtle
from Game.Character.bullet import Bullet
from Game.Handling.keyboard import Keyboard
from math import sin, cos, pi
from Game.Objects.tutorial import arena
# Create the screen
screen = turtle.Screen()
# Get the canvas
canvas = turtle.getcanvas()
# Create a keyboard
kb = Keyboard(canvas)
# Setup the game!
def setup():
# Create the window
turtle.setup(WINDOW_SIZE, WINDOW_SIZE)
# Hide the default turtle
turtle.hideturtle()
# Change the background color
screen.bgcolor("white")
# Set the window title
screen.title("Turtle Tanks")
# Turn on manuel draw mode for more control
screen.tracer(False)
# Remove the window borders
canvas.config(borderwidth=0, highlightthickness=0)
def register_shapes():
def rotate(point, angle, origin=(0, 0)):
"""
Rotate a point counterclockwise by a given angle around a given origin.
The angle should be given in radians.
"""
ox, oy = origin
px, py = point
sa, ca = sin(angle), cos(angle)
dx, dy = px - ox, py - oy
qx = ox + ca * dx - sa * dy
qy = oy + sa * dx + ca * dy
return qx, qy
def map_transform(points, angle=pi / 2, dx=0, dy=0, scale=1):
return list(map(lambda x: rotate((scale * (x[0] + dx), scale * (x[1] + dy)), pi / 2), points))
def build_tank(side, objects):
tank = turtle.Shape("compound")
for o in objects:
tank.addcomponent(*o)
turtle.addshape("tank_" + side, tank)
# Bullet
b = turtle.Shape("polygon", ((-5, -5), (-5, 5), (5, 5), (5, -5)))
turtle.addshape("bullet", b)
circle = screen._shapes["circle"]._data
# 3 wheels
components = []
components.append((map_transform(circle, dy=-5), "black"))
components.append((map_transform(circle, dx=20, dy=-5), "yellow"))
components.append((map_transform(circle, dx=-20, dy=-5), "black"))
# Tank body
trapezoid = ((-35, 5), (35, 5), (30, 25), (-30, 25))
components.append((map_transform(trapezoid), "red"))
# Tank turret / gun
rectangle = ((30, 15), (30, 20), (60, 20), (60, 15))
components.append((map_transform(rectangle), "red"))
build_tank("left", components)
components.pop()
components.pop()
components.append((map_transform(trapezoid), "blue"))
rectangle = ((-30, 15), (-30, 20), (-60, 20), (-60, 15))
components.append((map_transform(rectangle), "blue"))
# Uncomment to draw bounding box for debugging
#components.append((map_transform(circle, 0, 0, 0, 3), "yellow"))
build_tank("right", components)
player_1 = None
player_2 = None
def create_players(game_arena):
global player_1, player_2
player_1 = Player("p1", (LEFT_EDGE + 100, BOTTOM_EDGE + 100), game_arena, kb)
kb.register_key("p1", "a", "left")
kb.register_key("p1", "d", "right")
kb.register_key("p1", "w", "up")
kb.register_key("p1", "s", "down")
kb.register_key("p1", "space", "fire")
player_2 = Player("p2", (RIGHT_EDGE - 100, TOP_EDGE - 100), game_arena, kb)
kb.register_key("p2", "Left", "left")
kb.register_key("p2", "Right", "right")
kb.register_key("p2", "Up", "up")
kb.register_key("p2", "Down", "down")
kb.register_key("p2", "Return", "fire")
turtle.listen()
def input():
window = turtle.Screen()
name = turtle.textinput("1st","1st tank's name")
return name
def input_2():
window = turtle.Screen()
name_2 = turtle.textinput("2nd","2nd tank's name")
return name_2
def stop(x,y):
turtle.bye()
score = ScoreTurtle()
def draw():
turtle.clear()
style = ("Arial bold", 15)
gameover = False
winner = None
score_tanks_P1 = 0
score_tanks_P2 = 0
a = input()
b = input_2()
text_p1 = turtle.Turtle()
text_p2 = turtle.Turtle()
text_p1.penup()
text_p2.penup()
text_p1.setpos(-345,320)
text_p1.write("Score of "+str(a)+ ": " + str(score_tanks_P1),font=style)
text_p2.setpos(-346,300)
text_p2.write("Score of "+str(b)+ ": " +str(score_tanks_P2),font=style)
while gameover is False :
for entity in screen.turtles():
if isinstance(entity, Bullet) and entity.alive and entity.update() is None:
gameover = False
winner = None
if entity.distance(player_1) <= (player_1.radius + entity.radius) and entity.owner is not "p1":
entity.alive = False
entity.hideturtle()
gameover = player_1.hit()
score_tanks_P1 +=1
text_p1.clear()
text_p1.setpos(-340,320)
text_p1.penup()
text_p1.write("Score of "+str(a)+ " " + str(score_tanks_P1),font=style)
if gameover :
winner = a
elif entity.distance(player_2) <= (player_2.radius + entity.radius) and entity.owner is not "p2":
entity.alive = False
entity.hideturtle()
gameover = player_2.hit()
score_tanks_P2 +=1
text_p2.clear()
text_p2.penup()
text_p2.setpos(-340,300)
text_p2.write("Score of "+str(b)+ " " +str(score_tanks_P2),font=style)
if gameover:
winner = b
if gameover is True and winner is not None:
screen.clear()
if winner == a:
score.game_over(winner,score_tanks_P1)
if winner == b:
score.game_over(winner,score_tanks_P2)
if turtle.onscreenclick(stop,3) is True:
pass
return
player_1.update()
player_2.update()
# Draw the objects on the screen!
screen.update()
# Redraw every 20 milliseconds
#canvas.after(20, draw)
def start():
#Main function to make game start
screen.clearscreen()
setup()
register_shapes()
game_arena = None
game_arena = arena()
# Basic checks so that the students didn't accidentally mess things up by returning the wrong object
if not isinstance(game_arena, Arena):
raise RuntimeError
create_players(game_arena)
draw()
turtle.listen()
turtle.onkey(start, 'r')
start()
# Required for every turtle program
turtle.mainloop()