Files
@ 835bac5d3b3c
Branch filter:
Location: led-matrix-software/demos/pong.py
835bac5d3b3c
10.4 KiB
text/x-python
Added new demos and handle splash screens and controller input
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 | ### pong.py
### Author: Matthew Reed
### Game of pong, uses the D-Pad
### Adapted from http://trevorappleton.blogspot.com/2014/04/writing-pong-using-python-and-pygame.html
import sys
import time
import signal
import logging
import configparser
from enum import Enum
import math
from random import randint
import matrix
class Pong:
class Ball:
def __init__(self, x, y):
self.x = x
self.y = y
self.dir_x = -1 # -1=left 1=right
self.dir_y = -1 # -1=up 1=down
def move(self):
self.x += self.dir_x
self.y += self.dir_y
#check for collision with wall and bounce
if self.y <= 0:
self.y = 0
self.dir_y = 1
elif self.y >= 7:
self.y = 7
self.dir_y = -1
def draw(self, display):
display.set_pixel(self.x, self.y, matrix.Colors.RED.value)
class Paddle:
class DIRECTION(Enum):
NONE = 0
UP = 1
DOWN = 2
LEFT = 3
RIGHT = 4
def __init__(self, x, y):
self.size = 3
self.x = x
self.position = y
self.direction = self.DIRECTION.NONE
def move(self):
if self.direction == self.DIRECTION.UP:
self.position = self.position - 1
if self.position < 0:
self.position = 0
elif self.direction == self.DIRECTION.DOWN:
self.position = self.position + 1
if self.position + self.size > 8:
self.position = 8 - self.size
def auto_move(self, ball):
#If ball is moving away from paddle, center
if ball.dir_x == -1:
if self.position < 3:
self.position += 1
elif self.position > 3:
self.position -= 1
#if ball moving towards paddle, track its movement
elif ball.dir_x == 1:
if self.position + 1 - ball.dir_y < ball.y:
self.position += 1
elif self.position + 1 - ball.dir_y > ball.y:
self.position -= 1
if self.position < 0:
self.position = 0
elif self.position + self.size > 8:
self.position = 8 - self.size
def draw(self, display):
for i in range(0, self.size):
display.set_pixel(self.x, self.position + i, matrix.Colors.BLUE.value)
def __init__(self, config, parent, matrix, controller):
self.logger = logging.getLogger('snake')
self.config = config
self.parent = parent
self.matrix = matrix
self.controller = controller
def reset(self):
pass
def splash(self):
w = matrix.Colors.WHITE.value
l = matrix.Colors.WHITE_LOW.value
r = matrix.Colors.RED.value
b = matrix.Colors.BLUE.value
o = matrix.Colors.OFF.value
splash = [
[l, l, l, l, l, l, l, l],
[l, o, o, o, o, o, o, l],
[b, o, o, o, o, o, o, l],
[b, o, o, o, r, o, o, b],
[b, o, o, o, o, o, o, b],
[l, o, o, o, o, o, o, b],
[l, o, o, o, o, o, o, l],
[l, l, l, l, l, l, l, l],
]
for x in range(0, self.matrix.WIDTH):
for y in range(0, self.matrix.HEIGHT):
self.matrix.set_pixel(x, y, splash[y][x])
self.matrix.update()
def run(self):
#start timers and counters
self.start_time = time.time()
last_time = time.time()
delay_time = 0.1
led_iteration_count = 0
frame_count = 0
self.updateBallCountMax = 5
self.updateBallCount = 0
self.updateCompCountMax = 10
self.updateCompCount = 0
ball = self.Ball(4, 3)
paddle1 = self.Paddle(0, 3)
paddle2 = self.Paddle(7, 3)
score = 0
#draw display
self.matrix.set_matrix(matrix.Colors.OFF.value)
for x in range(0, 8):
self.matrix.set_pixel(x, 0, matrix.Colors.WHITE_LOW.value)
self.matrix.set_pixel(x, 7, matrix.Colors.WHITE_LOW.value)
for y in range(0, 8):
self.matrix.set_pixel(0, y, matrix.Colors.WHITE_LOW.value)
self.matrix.set_pixel(3, y, matrix.Colors.WHITE_LOW.value)
self.matrix.set_pixel(4, y, matrix.Colors.WHITE_LOW.value)
self.matrix.set_pixel(7, y, matrix.Colors.WHITE_LOW.value)
paddle1.draw(self.matrix)
paddle2.draw(self.matrix)
ball.draw(self.matrix)
self.matrix.update()
keep_going = True
while keep_going:
for event in self.controller.read_input():
if event.code == 313 and event.value == 1:
keep_going = False
elif event.code == 17:
if event.value == 1:
#dpad down
paddle1.direction = self.Paddle.DIRECTION.DOWN
if event.value == 0:
#dpad none
paddle1.direction = self.Paddle.DIRECTION.NONE
pass
if event.value == -1:
#dpad up
paddle1.direction = self.Paddle.DIRECTION.UP
if time.time() > last_time + delay_time:
last_time = time.time()
paddle1.move()
self.updateBallCount = self.updateBallCount + 1
if self.updateBallCount >= self.updateBallCountMax:
ball.move()
self.updateBallCount = 0
self.updateCompCount = self.updateCompCount + 1
if self.updateCompCount >= self.updateCompCountMax:
paddle2.auto_move(ball)
self.updateCompCount = 0
#ball hits paddle 1
if ball.dir_x == -1 and ball.x == 1:
ball.dir_x = ball.dir_x * -1
# if the ball bounces off the corner of the paddle, it goes back in the same direction it came from
if (ball.dir_y == 1 and ball.y == paddle1.position) or (ball.dir_y == -1 and ball.y == paddle1.position + 2):
ball.dir_y = ball.dir_y * -1
# if the paddle is in motion, give the ball some translation
if paddle1.direction == self.Paddle.DIRECTION.DOWN:
if ball.y < 7:
ball.y = ball.y + 1
if ball.y >= 7:
ball.y = 7
ball.dir_y = -1
elif paddle1.direction == self.Paddle.DIRECTION.UP:
if ball.y > 0:
ball.y = ball.y - 1
if ball.y <= 0:
ball.y = 0
ball.dir_y = 1
elif ball.dir_x == -1 and ball.x == 0:
if paddle1.position <= ball.y and (paddle1.position + paddle1.size) > ball.y:
#1 point for hitting the ball
score += 1
print("Hit!")
else:
#game over if left wall is hit
print("Miss!")
keep_going = False
elif ball.dir_x == 1 and ball.x == 6:
ball.dir_x = ball.dir_x * -1
if paddle2.position <= ball.y and (paddle2.position + paddle2.size) > ball.y:
#ball hits paddle2
pass
elif ball.dir_x == 1 and ball.x == 7:
#5 points for beating paddle2
score += 5
print("Score!")
#update display
self.matrix.set_matrix(matrix.Colors.OFF.value)
#draw game background
for x in range(0, 8):
self.matrix.set_pixel(x, 0, matrix.Colors.WHITE_LOW.value)
self.matrix.set_pixel(x, 7, matrix.Colors.WHITE_LOW.value)
for y in range(0, 8):
self.matrix.set_pixel(0, y, matrix.Colors.WHITE_LOW.value)
self.matrix.set_pixel(3, y, matrix.Colors.WHITE_LOW.value)
self.matrix.set_pixel(4, y, matrix.Colors.WHITE_LOW.value)
self.matrix.set_pixel(7, y, matrix.Colors.WHITE_LOW.value)
paddle1.draw(self.matrix)
paddle2.draw(self.matrix)
ball.draw(self.matrix)
self.matrix.update()
led_iteration_count = (led_iteration_count + 1) % self.matrix.NUM_LEDS
frame_count = frame_count + 1
time.sleep(0.01)
#display score before exiting
self.matrix.set_matrix(matrix.Colors.OFF.value)
if score > 750:
for i in range(0, 64):
self.matrix.set_pixel(i % self.matrix.WIDTH, math.floor(i / self.matrix.HEIGHT), matrix.Colors.YELLOW.value)
else:
multiples = math.floor(score / 50)
score = score % 50
for i in range(0, multiples):
self.matrix.set_pixel(i % self.matrix.WIDTH, math.floor(i / self.matrix.HEIGHT), matrix.Colors.YELLOW.value)
for i in range(multiples, score):
self.matrix.set_pixel(i % self.matrix.WIDTH, math.floor(i / self.matrix.HEIGHT), matrix.Colors.WHITE.value)
self.matrix.update()
time.sleep(2)
|