-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
97 lines (82 loc) · 2.79 KB
/
main.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
import pymunk
import pygame
from elements import Ground, Player, Rock, Walls, Bullet, Environment
pygame.init()
screen = pygame.display.set_mode((1000,800))
clock = pygame.time.Clock()
space = pymunk.Space()
space.gravity = (0,100)
env = Environment(space,screen)
action = 0.5
# score = 0
def coll_begin(arbiter,space,data):
# global score
global env
if arbiter.shapes[1].radius == 20 and arbiter.shapes[0].radius == 5:
env.reward += 5
env.rock.zindgi -= 1
# print(score)
elif arbiter.shapes[0].radius == 20 and arbiter.shapes[1].radius == 5:
env.reward += 5
env.rock.zindgi -= 1
# print(score)
elif arbiter.shapes[0].id == 3 and arbiter.shapes[1].id == 2:
env.done = True
env.player.collided = True
env.reward -= 5
elif arbiter.shapes[1].id == 3 and arbiter.shapes[0].id == 2:
env.done = True
env.player.collided = True
env.reward -= 5
if env.rock.zindgi == 0:
env.done = True
env.reward += 10
return True
def coll_post(arbiter,space,data):
#print('post solve')
if arbiter.shapes[1].radius == 20 and arbiter.shapes[0].radius == 5:
for b in range(len(env.player.bullets)):
if int(env.player.bullets[b].b_circle_shape.body.velocity[0]) != 0:
space.remove(arbiter.shapes[0].body,arbiter.shapes[0])
env.player.bullets.pop(b)
break
elif arbiter.shapes[0].radius == 20 and arbiter.shapes[1].radius == 5:
for b in range(len(env.player.bullets)):
if int(env.player.bullets[b].b_circle_shape.body.velocity[0]) != 0:
space.remove(arbiter.shapes[1].body,arbiter.shapes[1])
env.player.bullets.pop(b)
break
handler = space.add_default_collision_handler()
handler.begin = coll_begin
handler.post_solve = coll_post
counter = 0
for i in range(1):
done = False
score = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
action = 0
if event.key == pygame.K_RIGHT:
action = 1
if event.key == pygame.K_SPACE:
action = 2
# print(env.get_state())
counter += 1
screen.fill((50,50,50))
# env.draw_env()
observation_, reward, done = env.step(action,counter%30) #30
score += reward
# print(observation_, reward, done)
action = 0.5
space.step(1/50)
pygame.display.update()
clock.tick(120)
# if done == True:
# break
print(reward)
observation = env.reset()
# print(observation)