-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtroy_game.py
145 lines (114 loc) · 3.89 KB
/
troy_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
import pygame
pygame.init()
white = pygame.Color(255, 255, 255)
black = pygame.Color(0, 0, 0)
red = pygame.Color(255, 0, 0)
green = pygame.Color(0, 255, 0)
window_height = 600
window_width = 600
up = (0, -1)
down = (0, 1)
left = (-1, 0)
right = (1, 0)
gameDisplay = pygame.display.set_mode((window_width, window_height))
pygame.display.set_caption('desTROY')
clock = pygame.time.Clock()
FPS = 10
block_size = 10
class Rider:
def __init__(self, color, position, direction):
self.color = color
self.lead = position
self.direction = direction
self.components = []
def render(self):
for component in self.components:
pygame.draw.rect(gameDisplay, self.color,
[component[0], component[1],
block_size, block_size])
def handle_input(self, event, leftkey, rightkey):
if event.type == pygame.KEYDOWN:
left_command = event.key == leftkey
right_command = event.key == rightkey
if left_command:
if self.direction == up:
self.direction = left
elif self.direction == down:
self.direction = right
elif self.direction == left:
self.direction = down
elif self.direction == right:
self.direction = up
elif right_command:
if self.direction == up:
self.direction = right
elif self.direction == down:
self.direction = left
elif self.direction == left:
self.direction = up
elif self.direction == right:
self.direction = down
def out_of_bounds(self):
if self.lead[0] >= window_width or self.lead[0] < 0 or \
self.lead[1] >= window_height or self.lead[1] < 0:
return True
return False
def advance(self):
self.lead[0] += self.direction[0] * block_size
self.lead[1] += self.direction[1] * block_size
self.components.append((self.lead[0], self.lead[1]))
def check_self_collision(self):
for each_segment in self.components[:-1]:
if each_segment == (self.lead[0], self.lead[1]):
return True
return False
def check_collision(self, other_components):
for each_segment in other_components:
if each_segment == (self.lead[0], self.lead[1]):
return True
return False
def step_game(rider1, rider2):
gameExit = False
reward = [0, 0]
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
rider1.handle_input(event, pygame.K_j, pygame.K_l)
rider2.handle_input(event, pygame.K_a, pygame.K_d)
if rider1.out_of_bounds():
gameExit = True
reward[1] = 100
elif rider2.out_of_bounds():
gameExit = True
reward[0] = 100
rider1.advance()
rider2.advance()
gameDisplay.fill(white)
if rider1.check_self_collision():
gameExit = True
reward[1] = 100
elif rider2.check_self_collision():
gameExit = True
reward[0] = 100
if rider1.check_collision(rider2.components):
gameExit = True
reward[1] = 100
elif rider2.check_collision(rider1.components):
gameExit = True
reward[0] = 100
rider1.render()
rider2.render()
pygame.display.update()
clock.tick(FPS)
# pixData = pygame.surfarray.array3d(gameDisplay).swapaxes(0, 1).shape
return gameExit, reward
gameExit = False
rider1 = Rider(black, [window_width/2-50, window_height/2-50], up)
rider2 = Rider(red, [window_width/2+50, window_height/2+50], down)
while not gameExit:
gameExit, reward = step_game(rider1, rider2)
print(reward)
if gameExit:
print('exit')
pygame.quit()
quit()