Skip to content

Commit 53a4cc5

Browse files
committed
Derived TrivialPong from PygameFeedback rather than MainloopFeedback
1 parent d76ffd2 commit 53a4cc5

File tree

1 file changed

+27
-30
lines changed

1 file changed

+27
-30
lines changed

src/Feedbacks/TrivialPong/TrivialPong.py

+27-30
Original file line numberDiff line numberDiff line change
@@ -24,56 +24,53 @@
2424

2525
import pygame
2626

27-
from FeedbackBase.MainloopFeedback import MainloopFeedback
27+
from FeedbackBase.PygameFeedback import PygameFeedback
2828

2929

30-
class TrivialPong(MainloopFeedback):
30+
class TrivialPong(PygameFeedback):
3131

3232
def on_control_event(self, data):
33-
self.val = self._data["data"][-1]
34-
33+
self.val = data["clout"]
34+
3535
def init(self):
36-
self.FPS = 60
37-
self.val = 0.0
38-
39-
def pre_mainloop(self):
40-
pygame.init()
41-
self.size = self.width, self.height = 800, 600
36+
PygameFeedback.init(self)
37+
self.caption = "Trivial Pong"
38+
# set the initial speeds for ball and bar
39+
self.barspeed = [3, 0]
4240
self.speed = [2, 2]
43-
black = 0, 0, 0
44-
path = os.path.dirname( globals()["__file__"] )
45-
self.screen = pygame.display.set_mode(self.size)
41+
# set initial value for cl output
42+
self.val = 0.0
43+
44+
def init_graphics(self):
45+
# load graphics
46+
path = os.path.dirname( globals()["__file__"] )
4647
self.ball = pygame.image.load(os.path.join(path, "ball.png"))
4748
self.ballrect = self.ball.get_rect()
4849
self.bar = pygame.image.load(os.path.join(path, "bar.png"))
4950
self.barrect = self.bar.get_rect()
50-
self.barspeed = [3, 0]
51-
self.clock = pygame.time.Clock()
52-
53-
def post_mainloop(self):
54-
pygame.quit()
55-
56-
def tick(self):
57-
self.clock.tick(self.FPS)
58-
pygame.event.pump()
59-
51+
6052
def play_tick(self):
61-
w_half = self.screen.get_width() / 2
53+
width, height = self.screenSize
54+
w_half = width / 2.
55+
# move bar and ball
6256
pos = w_half + w_half * self.val
63-
self.barrect.center = pos, self.height - 20
57+
self.barrect.center = pos, height - 20
6458
self.ballrect = self.ballrect.move(self.speed)
65-
if self.ballrect.left < 0 or self.ballrect.right > self.width:
59+
# collision detection walls
60+
if self.ballrect.left < 0 or self.ballrect.right > width:
6661
self.speed[0] = -self.speed[0]
67-
if self.ballrect.top < 0 or self.ballrect.bottom > self.height:
62+
if self.ballrect.top < 0 or self.ballrect.bottom > height:
6863
self.speed[1] = -self.speed[1]
69-
if self.barrect.left < 0 or self.barrect.right > self.width:
64+
if self.barrect.left < 0 or self.barrect.right > width:
7065
self.barspeed[0] = -self.barspeed[0]
71-
if self.barrect.top < 0 or self.barrect.bottom > self.height:
66+
if self.barrect.top < 0 or self.barrect.bottom > height:
7267
self.barspeed[1] = -self.barspeed[1]
68+
# collision detection for bar vs ball
7369
if self.barrect.colliderect(self.ballrect):
7470
self.speed[0] = -self.speed[0]
7571
self.speed[1] = -self.speed[1]
76-
self.screen.fill( (0,0,0) )
72+
# update the screen
73+
self.screen.fill(self.backgroundColor)
7774
self.screen.blit(self.ball, self.ballrect)
7875
self.screen.blit(self.bar, self.barrect)
7976
pygame.display.flip()

0 commit comments

Comments
 (0)