-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
131 lines (103 loc) · 3.35 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
import pygame
from pygame.locals import *
from pong import *
import sys
import signal
import matplotlib.pyplot as plt
# http://trevorappleton.blogspot.com/2014/04/writing-pong-using-python-and-pygame.html
# game = Pong('S')
game = Pong('NN')
def sigint_handler(signum, frame):
draw_plot()
signal.signal(signal.SIGINT, sigint_handler)
# number of frames per second
FPS = 300
# window size
WINDOWWIDTH = 500
WINDOWHEIGHT = 500
# line information
LINETHICKNESS = 10
PADDLESIZE = 100
# colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
# font
pygame.font.init()
FONT = pygame.font.SysFont("monospace", 16)
def drawBall(ball, game):
ball.x = game.ball.x * 500 - LINETHICKNESS / 2
ball.y = game.ball.y * 500 - LINETHICKNESS / 2
pygame.draw.rect(DISPLAYSURF, RED, ball)
def drawPaddle(paddle, game):
paddle.y = game.paddle.y * 500 - LINETHICKNESS / 2
pygame.draw.rect(DISPLAYSURF, BLACK, paddle)
def drawWall(wall):
pygame.draw.rect(DISPLAYSURF, BLACK, wall)
def draw_plot():
line_x = [0, game.ROUND]
line_y = [9, 9]
f, ax = plt.subplots()
ax.plot(game.x, game.y, 'b', label='Average Bounce')
ax.plot(line_x, line_y, 'r', label='9')
plt.ylabel('average rebounce')
plt.xlabel('num of games')
# plt.title(
# 'Alpha = ' + str(C) + '/(' + str(C) + '+N(s,a)), Gamma = ' + str(GAMMA) + ', Epsilon = ' + str(EPSILON))
plt.legend(loc='lower right')
# plt.ylim(0, 14)
plt.grid(True)
plt.show()
if __name__ == '__main__':
# train
if not game.all_finished:
while not game.all_finished:
game.update()
draw_plot()
#
# initial pygame and surface
pygame.init()
global DISPLAYSURF
# set up screen
FPSCLOCK = pygame.time.Clock()
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption("CS440_MP4_hwu63")
ball = pygame.Rect(game.state[0] * 600 - LINETHICKNESS / 2, game.state[1] * 600 - LINETHICKNESS / 2, LINETHICKNESS,
LINETHICKNESS)
paddle = pygame.Rect(500 - LINETHICKNESS, game.state[4] * 500 - LINETHICKNESS, LINETHICKNESS,
game.paddle.height * 500)
wall = pygame.Rect(0, 0, LINETHICKNESS, WINDOWHEIGHT)
# draw game
DISPLAYSURF.fill(WHITE)
drawBall(ball, game)
drawPaddle(paddle, game)
drawWall(wall)
game.ROUND = 200
game.round = 0
game.scores = []
game.all_finished = False
game.x = []
game.y = []
game.test_mode = True
while not game.all_finished: # main game loop
for event in pygame.event.get():
# exit game
if event.type == QUIT:
pygame.quit()
sys.exit()
# update game
game.update()
DISPLAYSURF.fill(WHITE)
drawBall(ball, game)
drawWall(wall)
drawPaddle(paddle, game)
# update the screen
roundtext = FONT.render("Round {0}".format(game.round), 1, (0, 0, 0))
DISPLAYSURF.blit(roundtext, (10, 10))
scoretext = FONT.render("Score {0}".format(game.score), 1, (0, 0, 0))
DISPLAYSURF.blit(scoretext, (10, 20))
pygame.display.update()
FPSCLOCK.tick(FPS)
draw_plot()
pygame.quit()
sys.exit(0)