-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathTron.py
181 lines (154 loc) · 5.65 KB
/
Tron.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
# -----------------------------------------------------------------------------
#
# Tron
# Language - Python
# Modules - pygame, sys
#
# Controls - Arrow Keys for Player 2(Yellow) and WASD Keys for Player 1(Red)
#
# By - Jatin Kumar Mandav
#
# Website - https://jatinmandav.wordpress.com
#
# YouTube Channel - https://www.youtube.com/channel/UCdpf6Lz3V357cIZomPwjuFQ
# Twitter - @jatinmandav
#
# -----------------------------------------------------------------------------
import pygame
import sys
pygame.init()
width = 600
height = 600
display = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tron 2D")
clock = pygame.time.Clock()
background = (27, 79, 114)
white = (236, 240, 241)
yellow = (241, 196, 15)
darkYellow = (247, 220, 111)
red = (231, 76, 60)
darkRed = (241, 148, 138)
darkBlue = (40, 116, 166)
font = pygame.font.SysFont("Agency FB", 65)
w = 10
# Tron Bike Class
class tronBike:
def __init__(self, number, color, darkColor, side):
self.w = w
self.h = w
self.x = abs(side - 100)
self.y = height/2 - self.h
self.speed = 10
self.color = color
self.darkColor = darkColor
self.history = [[self.x, self.y]]
self.number = number
self.length = 1
# Draw / Show the Bike
def draw(self):
for i in range(len(self.history)):
if i == self.length - 1:
pygame.draw.rect(display, self.darkColor, (self.history[i][0], self.history[i][1], self.w, self.h))
else:
pygame.draw.rect(display, self.color, (self.history[i][0], self.history[i][1], self.w, self.h))
# Move the Bike
def move(self, xdir, ydir):
self.x += xdir*self.speed
self.y += ydir*self.speed
self.history.append([self.x, self.y])
self.length += 1
if self.x < 0 or self.x > width or self.y < 0 or self.y > height:
gameOver(self.number)
# Check if Bike Collides with Route
def checkIfHit(self, opponent):
if abs(opponent.history[opponent.length - 1][0] - self.history[self.length - 1][0]) < self.w and abs(opponent.history[opponent.length - 1][1] - self.history[self.length - 1][1]) < self.h:
gameOver(0)
for i in range(opponent.length):
if abs(opponent.history[i][0] - self.history[self.length - 1][0]) < self.w and abs(opponent.history[i][1] - self.history[self.length - 1][1]) < self.h:
gameOver(self.number)
for i in range(len(self.history) - 1):
if abs(self.history[i][0] - self.x) < self.w and abs(self.history[i][1] - self.y) < self.h and self.length > 2:
gameOver(self.number)
def gameOver(number):
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.key == pygame.K_r:
tron()
if number == 0:
text = font.render("Both the Players Collided!", True, white)
else:
text = font.render("Player %d Lost the Tron!" %(number), True, white)
display.blit(text, (50, height/2))
pygame.display.update()
clock.tick(60)
def drawGrid():
squares = 50
for i in range(width/squares):
pygame.draw.line(display, darkBlue, (i*squares, 0), (i*squares, height))
pygame.draw.line(display, darkBlue, (0, i*squares), (width, i*squares))
def close():
pygame.quit()
sys.exit()
def tron():
loop = True
bike1 = tronBike(1, red, darkRed, 0)
bike2 = tronBike(2, yellow, darkYellow, width)
x1 = 1
y1 = 0
x2 = -1
y2 = 0
while loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
close()
if event.key == pygame.K_UP:
if not (x2 == 0 and y2 == 1):
x2 = 0
y2 = -1
if event.key == pygame.K_DOWN:
if not (x2 == 0 and y2 == -1):
x2 = 0
y2 = 1
if event.key == pygame.K_LEFT:
if not (x2 == 1 and y2 == 0):
x2 = -1
y2 = 0
if event.key == pygame.K_RIGHT:
if not (x2 == -1 and y2 == 0):
x2 = 1
y2 = 0
if event.key == pygame.K_w:
if not (x1 == 0 and y1 == 1):
x1 = 0
y1 = -1
if event.key == pygame.K_s:
if not (x1 == 0 and y1 == -1):
x1 = 0
y1 = 1
if event.key == pygame.K_a:
if not (x1 == 1 and y1 == 0):
x1 = -1
y1 = 0
if event.key == pygame.K_d:
if not (x1 == -1 and y1 == 0):
x1 = 1
y1 = 0
display.fill(background)
drawGrid()
bike1.draw()
bike2.draw()
bike1.move(x1, y1)
bike2.move(x2, y2)
bike1.checkIfHit(bike2)
bike2.checkIfHit(bike1)
pygame.display.update()
clock.tick(10)
tron()