-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmountain_fly.py
154 lines (148 loc) · 6.41 KB
/
mountain_fly.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
import wrapped_mountain as game
import numpy as np
import pygame
import sys
import mountain_utils
import random
from collections import deque
ACTIONS = 2 # number of valid actions
game_state = game.GameState()
IMAGES, SOUNDS, HITMASKS = mountain_utils.load()
SCREENWIDTH = 1920 #288
SCREENHEIGHT = 1080 #512
def hitbtn(x, y):
if int(SCREENWIDTH-40-IMAGES['back'].get_width()) <= x <= int(SCREENWIDTH-40) and int(SCREENHEIGHT-40-IMAGES['back'].get_height()) <= y <= int(SCREENHEIGHT-40):
if HITMASKS['back'][x-int(SCREENWIDTH-40-IMAGES['back'].get_width())][y-int(SCREENHEIGHT-40-IMAGES['back'].get_height())]:
return 1
elif int(SCREENWIDTH-40-IMAGES['back'].get_width()-40-IMAGES['stop'].get_width()) <= x <= int(SCREENWIDTH-40-IMAGES['back'].get_width()) and int(SCREENHEIGHT-40-IMAGES['stop'].get_height()) <= y <= int(SCREENHEIGHT-40):
if HITMASKS['stop'][x-int(SCREENWIDTH-40-IMAGES['back'].get_width()-40-IMAGES['stop'].get_width())][y-int(SCREENHEIGHT-40-IMAGES['stop'].get_height())]:
return 2
elif int(SCREENWIDTH-40-IMAGES['back'].get_width()-40-IMAGES['stop'].get_width()-40-IMAGES['start'].get_width()) <= x <= int(SCREENWIDTH-40-IMAGES['back'].get_width()-40-IMAGES['stop'].get_width()-40) and int(SCREENHEIGHT-40-IMAGES['start'].get_height()) <= y <= int(SCREENHEIGHT-40):
if HITMASKS['start'][x-int(SCREENWIDTH-40-IMAGES['back'].get_width()-40-IMAGES['stop'].get_width()-40-IMAGES['start'].get_width())][y-int(SCREENHEIGHT-40-IMAGES['start'].get_height())]:
return 3
return 0
def hitPixel(x, y, u0, l0, t0):
uHitmask = HITMASKS['pipe'][t0*2+0]
lHitmask = HITMASKS['pipe'][t0*2+1]
try:
if uHitmask[x-int(u0['x'])][y-int(u0['y'])]:
return True
except IndexError:
try:
if lHitmask[x-int(l0['x'])][y-int(l0['y'])]:
return True
except IndexError:
return False
return False
def hit(x, y, u, l, ty):
for i in range(0, len(u)):
if(hitPixel(x, y, u[i], l[i], ty[i])):
return i
return -1
def main():
start = False
level = 9
# start training
t = 0
py = 0
drag = False
which = -1
# get the first state by doing nothing and preprocess the image to 80x80x4
game_state.__init__()
do_nothing = np.zeros(ACTIONS)
do_nothing[0] = 1
x_t, r_0, terminal, u, l,ty,score, clear = game_state.frame_step(do_nothing, start, level)
die = True
while not terminal:
t = np.zeros(ACTIONS)
t[0] = 1
t[1] = 0
if start:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.key == pygame.K_SPACE:
t[0] = 0
t[1] = 1
elif event.type == pygame.K_UP:
t[0] = 1
t[1] = 0
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
try:
tmpt = hitbtn(pos[0], pos[1])
except Exception:
tmpt = 0
if tmpt != 0:
if tmpt == 1:
terminal = True
die = False
pygame.mixer.music.load(SOUNDS['start'])
pygame.mixer.music.play()
elif tmpt == 2:
start = False
pygame.mixer.music.load(SOUNDS['stop'])
pygame.mixer.music.play()
x_t, r_0, terminal, u, l,ty,score,clear = game_state.frame_step(t, start, level)
if clear == 1:
level = 0
level = level + 1
else:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
if not drag:
try:
tmpt = hitbtn(pos[0], pos[1])
except Exception:
tmpt = 0
if tmpt != 0:
if tmpt == 1:
terminal = True
die = False
pygame.mixer.music.load(SOUNDS['start'])
pygame.mixer.music.play()
elif tmpt == 3:
start = True
pygame.mixer.music.load(SOUNDS['start'])
pygame.mixer.music.play()
else:
which = hit(pos[0], pos[1], u, l, ty)
if -1 != which:
py = pos[1]
drag = True
pygame.mixer.music.load(SOUNDS['change'])
pygame.mixer.music.play()
elif event.type == pygame.MOUSEMOTION:
if drag:
pos = pygame.mouse.get_pos()
game_state.changePipe(pos[1]-py, which)
py = pos[1]
_, _, terminal, u, l, ty, score, clear = game_state.frame_step(do_nothing, start, level)
if clear == 1:
level = 0
level = level + 1
elif event.type == pygame.MOUSEBUTTONUP:
drag = False
while die:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_t:
die = False
elif event.key == pygame.K_ESCAPE:
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
try:
tmpt = hitbtn(pos[0], pos[1])
except Exception:
tmpt = 0
if tmpt == 1:
terminal = True
die = False
pygame.mixer.music.load(SOUNDS['start'])
pygame.mixer.music.play()