-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
192 lines (154 loc) · 6.83 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
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
# Window will spawn in exact center
import os
import config
import math
import pygame
import pygame.camera
from pygame import gfxdraw
from win32api import GetSystemMetrics
import keyboard
import mouse
import reinforcement_learning.environment_helper as env_helper
import numpy as np
from img import images
from logic import collisions
from objects.ball import Ball
from objects.brick import Brick
from objects.bumper import Bumper
from objects.flipper import Flipper
from objects.polygon import Polygon
from objects.rect import Rect
from matplotlib import pyplot as plt
windowX = GetSystemMetrics(0) / 2 - config.gameW / 2
windowY = GetSystemMetrics(1) / 2 - config.gameH / 2
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (windowX, windowY)
pygame.init()
from pygame.locals import DOUBLEBUF # FULLSCREEN
display = pygame.display.set_mode((config.gameW, config.gameH), DOUBLEBUF)
display.set_alpha(None)
pygame.display.set_caption("Pinball")
clock = pygame.time.Clock()
def listen(running):
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE):
running = False
# elif event.type == sounds.END_FLAG:
# sounds.changeMusic(sounds.overtureLoopTime)
else:
if not config.AUTONOMOUS_MODE:
keyboard.listen(event)
mouse.listen()
return running
def main():
# pygame.mixer.music.play()
# midline = Rect(199,0,2,600,(0,0,0))
import torch
print("CUDA is available: ", torch.cuda.is_available())
running = True
ballsLeft = 2
state = config.TITLE_SCREEN
playButton = Rect(config.gameW / 2 - 105, config.gameH - 200, 210, 63, None, images.playButton)
plunger = Rect(config.gameW - 30, config.gameH - 60, 20, 60, None, images.plunger)
ball = Ball(380, 550, 10, config.colors['ball'])
# PREP FOR FLIPPERS
leftX = -15 + config.gameW / 2 - 45
rightX = 15 + config.gameW / 2 + 45 + 2 * 35
flippers = [
Flipper(leftX, 550, 90, 20, 5 * math.pi / 36, -5 * math.pi / 36, config.colors['flipper'], "L"),
Flipper(rightX, 550, 90, 20, 31 * math.pi / 36, 41 * math.pi / 36, config.colors['flipper'], "R")
]
# PREP FOR BASES
leftHigh, left2ndHigh = flippers[0].getHighestPoints()
leftXRate = math.tan(flippers[0].angle)
leftmostTop = [0, leftHigh[1] - leftHigh[0] * leftXRate]
leftmostBot = [0, left2ndHigh[1] - left2ndHigh[0] * leftXRate]
rightHigh, right2ndHigh = flippers[1].getHighestPoints()
rightXRate = -math.tan(flippers[1].angle)
rightmostTop = [config.gameW, rightHigh[1] - (config.gameW - rightHigh[0]) * rightXRate]
rightmostBot = [config.gameW, right2ndHigh[1] - (config.gameW - right2ndHigh[0]) * rightXRate]
bases = [
Polygon([leftmostTop, leftHigh, left2ndHigh, leftmostBot], flippers[0].angle, config.colors['wall']),
Polygon([rightmostTop, rightHigh, right2ndHigh, rightmostBot], flippers[1].angle, config.colors['wall'])
]
walls = [
Rect(0, 0, config.gameW, 10, config.colors['wall']),
Rect(0, 0, 20, config.gameH, config.colors['wall']),
Rect(config.gameW - 40, 0, 40, config.gameH, config.colors['wall'])
]
bumpers = [Bumper(60, 60), Bumper(175, 145), Bumper(265, 130), Bumper(240, 210),
Bumper(100, 270, 50, 50, None, images.burst, "superbumper")]
bricks = [Brick(30, 160, 95, 36, 30 + 48, 30 + 95 - 48)]
while running:
running = listen(running)
if state == config.TITLE_SCREEN:
display.blit(images.menu, (0, 0))
playButton.go(display)
if mouse.mouse['click'] and collisions.rectPoint(playButton, mouse.mouse['pos']):
state = config.STAGE_ONE
elif state == config.STAGE_ONE:
# Game Logic
if mouse.mouse['click'] and ball.launching and ball.spd[1] == 0:
ball.spd[1] = np.random.uniform(-16.0, -8.0)
if keyboard.controls['keySpace'] and ball.launching and ball.spd[1] == 0:
ball.spd[1] = -14
if keyboard.controls['keyEnter']:
ball.reset()
display.fill(config.colors['bg'])
for b in bases:
b.go(display)
for w in walls:
w.go(display)
for bb in bumpers:
bb.go(display)
for bbb in bricks:
bbb.go(display)
# Flipper actions are decided here through either keyboard input or RL Agent
observation = env_helper.make_observation(display)
plt.imshow(observation)
plt.show()
env_helper.take_action(observation)
for f in flippers:
f.go(display, ball)
gfxdraw.filled_polygon(display, [[350, 0], [400, 0], [400, 50]], config.colors['releaser'])
gfxdraw.aapolygon(display, [[350, 0], [400, 0], [400, 50]], config.colors['releaser'])
plunger.go(display)
display.blit(images.button, (330, 320))
scoreTEXT = str(ballsLeft) + " | " + str(ball.score)
scoreRender = config.muli["30"].render(scoreTEXT, True, config.colors['score'])
scoreRECT = scoreRender.get_rect()
scoreRECT.right = config.gameW - 60
scoreRECT.top = 20
display.blit(scoreRender, scoreRECT)
ball.go(display, flippers, bases, walls, bumpers, bricks)
if ball.y > config.gameH or ball.x < 0 or ball.x > config.gameW:
if ballsLeft > 0:
ball.reset()
ballsLeft -= 1
else:
state = config.GAME_OVER
# Debug
# pygame.draw.rect(ctx,constants.colors['white'],(leftX-35,550,1,1))
# pygame.draw.rect(ctx,constants.colors['white'],(rightX-35,550,1,1))
pygame.draw.rect(display, config.colors['white'], (flippers[0].pivotX, flippers[0].y, 1, 1))
# midline.go(ctx)
fpsTEXT = str(round(clock.get_fps(), 1))
fps = config.muli["15"].render(fpsTEXT, True, config.colors['black'])
display.blit(fps, (25, 8))
elif state == config.GAME_OVER:
display.blit(images.gameOver, (0, 0))
playButton.go(display)
scoreTEXT = str(ball.score) + " points."
scoreRender = config.muli["30"].render(scoreTEXT, True, config.colors['score'])
scoreRECT = scoreRender.get_rect()
scoreRECT.center = (config.gameW / 2, config.gameH - 300)
display.blit(scoreRender, scoreRECT)
if mouse.mouse['click'] and collisions.rectPoint(playButton, mouse.mouse['pos']):
state = config.STAGE_ONE
ball.score = 0
ballsLeft = 3
# Update Window
pygame.display.update()
# input()
clock.tick(60)
pygame.quit()
main()