-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpong_game.py
157 lines (132 loc) · 5.05 KB
/
pong_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
146
147
148
149
150
151
152
153
154
155
156
157
import numpy as np
import pygame
import os
from pygame.locals import *
from sys import exit
import random
import pygame.surfarray as surfarray
position = 5, 325
os.environ['SDL_VIDEO_WINDOW_POS'] = str(position[0]) + "," + str(position[1])
pygame.init()
screen = pygame.display.set_mode((640,480),0,32)
# Creating 2 bars, a ball and background.
back = pygame.Surface((640,480))
background = back.convert()
background.fill((0,0,0))
bar = pygame.Surface((10,50))
bar1 = bar.convert()
bar1.fill((0,255,255))
bar2 = bar.convert()
bar2.fill((255,255,255))
circ_sur = pygame.Surface((15,15))
circ = pygame.draw.circle(circ_sur,(255,255,255),(7,7), (7) )
circle = circ_sur.convert()
circle.set_colorkey((0,0,0))
font = pygame.font.SysFont("calibri",40)
# Variable initialization.
ai_speed = 15.
HIT_REWARD = 0
LOSE_REWARD = -1
SCORE_REWARD = 1
class GameState:
""" The pong game."""
def __init__(self):
self.bar1_x, self.bar2_x = 10. , 620.
self.bar1_y, self.bar2_y = 215. , 215.
self.circle_x, self.circle_y = 307.5, 232.5
self.bar1_move, self.bar2_move = 0. , 0.
self.bar1_score, self.bar2_score = 0,0
self.speed_x, self.speed_y = 7., 7.
def frame_step(self,input_vect):
""" Run one step of the game.
Args:
input_vect: an array with the actions taken
Returns:
image_data: the playground image.
reward: the reward obtained from the one move in input_vect.
terminal: the game terminated and the scores are reset.
"""
# Internally process pygame event handlers.
pygame.event.pump()
# Initialize the reward.
reward = 0
# Check that only one input action is given.
if sum(input_vect) != 1:
raise ValueError('Multiple input actions!')
# Key up.
if input_vect[1] == 1:
self.bar1_move = -ai_speed
# Key down.
elif input_vect[2] == 1:
self.bar1_move = ai_speed
# Don't move.
else:
self.bar1_move = 0
# Scores of the players.
self.score1 = font.render(str(self.bar1_score), True,(255,255,255))
self.score2 = font.render(str(self.bar2_score), True,(255,255,255))
# Draw the screen.
screen.blit(background,(0,0))
frame = pygame.draw.rect(screen,(255,255,255),Rect((5,5),(630,470)),2)
middle_line = pygame.draw.aaline(screen,(255,255,255),(330,5),(330,475))
screen.blit(bar1,(self.bar1_x,self.bar1_y))
screen.blit(bar2,(self.bar2_x,self.bar2_y))
screen.blit(circle,(self.circle_x,self.circle_y))
screen.blit(self.score1,(250.,210.))
screen.blit(self.score2,(380.,210.))
self.bar1_y += self.bar1_move
# AI of the computer.
if self.circle_x >= 305.:
if not self.bar2_y == self.circle_y + 7.5:
if self.bar2_y < self.circle_y + 7.5:
self.bar2_y += ai_speed
if self.bar2_y > self.circle_y - 42.5:
self.bar2_y -= ai_speed
else:
self.bar2_y == self.circle_y + 7.5
# Bounds of movement.
if self.bar1_y >= 420.: self.bar1_y = 420.
elif self.bar1_y <= 10. : self.bar1_y = 10.
if self.bar2_y >= 420.: self.bar2_y = 420.
elif self.bar2_y <= 10.: self.bar2_y = 10.
# The ball hitting bars case.
if self.circle_x <= self.bar1_x + 10.:
if self.circle_y >= self.bar1_y - 7.5 and self.circle_y <= self.bar1_y + 42.5:
self.circle_x = 20.
self.speed_x = -self.speed_x
reward = HIT_REWARD
if self.circle_x >= self.bar2_x - 15.:
if self.circle_y >= self.bar2_y - 7.5 and self.circle_y <= self.bar2_y + 42.5:
self.circle_x = 605.
self.speed_x = -self.speed_x
# Scoring.
if self.circle_x < 5.:
self.bar2_score += 1
reward = LOSE_REWARD
self.circle_x, self.circle_y = 320., 232.5
self.bar1_y,self.bar_2_y = 215., 215.
elif self.circle_x > 620.:
self.bar1_score += 1
reward = SCORE_REWARD
self.circle_x, self.circle_y = 307.5, 232.5
self.bar1_y, self.bar2_y = 215., 215.
# Collisions on sides.
if self.circle_y <= 10.:
self.speed_y = -self.speed_y
self.circle_y = 10.
elif self.circle_y >= 457.5:
self.speed_y = -self.speed_y
self.circle_y = 457.5
# Update the position of the circle.
self.circle_x += self.speed_x
self.circle_y += self.speed_y
# Get the playground.
image_data = pygame.surfarray.array3d(pygame.display.get_surface())
pygame.display.update()
# Reset the game.
terminal = False
if max(self.bar1_score, self.bar2_score) >= 20:
self.bar1_score = 0
self.bar2_score = 0
terminal = True
return image_data, reward, terminal