From 6948cd7d529ee628fa7d4fd731df2722722fdda4 Mon Sep 17 00:00:00 2001 From: Leon Date: Mon, 4 Feb 2019 23:49:58 +0100 Subject: [PATCH 1/4] init --- PongMulti.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PongMulti.py b/PongMulti.py index c69b69f..ef9a4a7 100644 --- a/PongMulti.py +++ b/PongMulti.py @@ -10,7 +10,7 @@ class PongMulti(Game.CubeGame, threading.Thread): _name = 'PongMultiplayer' - _version = 'v0.1' + _version = 'v0.2' _menu_frame = [0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, From abc53a9f196a634f400bf7c7c2aaed2696c987ed Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 8 Feb 2019 14:24:01 +0100 Subject: [PATCH 2/4] dingens --- Cube.py | 4 ++-- Pong.py | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Cube.py b/Cube.py index 0755aaf..1cb86d4 100644 --- a/Cube.py +++ b/Cube.py @@ -1,7 +1,7 @@ import threading import time import Direction -import api +import vCubeAPI as api import Game as Game import FrameCollection2D as Frames import Snake @@ -53,7 +53,7 @@ def open_menu(self): self.show_menu_state() - if api.pressed_enter: + if api.cube.pressed_enter: self.cube_games[self.current_item].close_animation() self.current_game = self.cube_games[self.current_item] updated = True diff --git a/Pong.py b/Pong.py index a8133ce..caeb19d 100644 --- a/Pong.py +++ b/Pong.py @@ -137,6 +137,11 @@ def run(self): api.cuboid_on(self.p_loc, self.p_size, 1, self.p_size) Direction.direction = None + if self.b_loc[1] - self.b_radius < 1 / self.cube_size: + if ((self.p_loc[0] + self.p_radius > self.b_loc[0] > self.p_loc[0] - self.p_radius) and ( + self.p_loc[2] + self.p_radius > self.b_loc[2] > self.p_loc[2] - self.p_radius)): + self.ball_vel_y *= -1 + if self.b_loc[1] - self.b_radius < 0: if not ((self.p_loc[0] + self.p_radius > self.b_loc[0] > self.p_loc[0] - self.p_radius) and ( self.p_loc[2] + self.p_radius > self.b_loc[2] > self.p_loc[2] - self.p_radius)): @@ -151,4 +156,4 @@ def run(self): api.display() - time.sleep(0.02) + time.sleep(0.04) From 8e13022abfac23985dc9ee4f4a64a3092fc360e9 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 8 Feb 2019 15:04:57 +0100 Subject: [PATCH 3/4] aliasing --- Pong.py | 60 +++++++++++++++++++++++++++++----------------------- PongMulti.py | 24 +++++++++++++-------- vCubeAPI.py | 56 ++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 98 insertions(+), 42 deletions(-) diff --git a/Pong.py b/Pong.py index caeb19d..9dbd727 100644 --- a/Pong.py +++ b/Pong.py @@ -25,12 +25,12 @@ def __init__(self, cube_size, frame_size): self.an = None - self.b_loc = [0.5, 0.5, 0.5] + self.b_loc = [0.250, 0.5, 0.250] self.b_size = 1 self.b_radius = (self.b_size / self.cube_size) / 2 - self.ball_vel_x = 0.01 - self.ball_vel_y = 0.01 - self.ball_vel_z = 0.01 + self.ball_vel_x = 0.125 + self.ball_vel_y = 0.125 + self.ball_vel_z = 0.125 self.failed = False self.player_action = None @@ -69,6 +69,7 @@ def done(self): time.sleep(1) def run(self): + counter = 0 while not self.failed: # Turn off last position # api.led_off(self.ball_loc) @@ -80,20 +81,33 @@ def run(self): self.ball_loc[2] += self.ball_vel_z """ - api.cuboid_off(self.b_loc, self.b_size, self.b_size, self.b_size) + api.ball_off(self.b_loc) - self.b_loc[0] += self.ball_vel_x - self.b_loc[1] += self.ball_vel_y - self.b_loc[2] += self.ball_vel_z + if counter == 7: + self.b_loc[0] += self.ball_vel_x + self.b_loc[1] += self.ball_vel_y + self.b_loc[2] += self.ball_vel_z - if self.b_loc[0] - self.b_radius < 0 or self.b_loc[0] + self.b_radius > 1: - self.ball_vel_x *= -1 + if self.b_loc[0] - self.b_radius < 0 or self.b_loc[0] + self.b_radius > 1: + self.ball_vel_x *= -1 - if self.b_loc[1] - self.b_radius < 0 or self.b_loc[1] + self.b_radius > 1: - self.ball_vel_y *= -1 + if self.b_loc[1] - self.b_radius < 0 or self.b_loc[1] + self.b_radius > 1: + self.ball_vel_y *= -1 + + if self.b_loc[2] - self.b_radius < 0 or self.b_loc[2] + self.b_radius > 1: + self.ball_vel_z *= -1 + + if self.b_loc[1] - self.b_radius < 1 / self.cube_size: + if ((self.p_loc[0] + self.p_radius > self.b_loc[0] > self.p_loc[0] - self.p_radius) and ( + self.p_loc[2] + self.p_radius > self.b_loc[2] > self.p_loc[2] - self.p_radius)): + self.ball_vel_y *= -1 + + if self.b_loc[1] - self.b_radius < 0: + if not ((self.p_loc[0] + self.p_radius > self.b_loc[0] > self.p_loc[0] - self.p_radius) and ( + self.p_loc[2] + self.p_radius > self.b_loc[2] > self.p_loc[2] - self.p_radius)): + self.failed = True - if self.b_loc[2] - self.b_radius < 0 or self.b_loc[2] + self.b_radius > 1: - self.ball_vel_z *= -1 + self.done() # Player moving self.player_action = Direction.direction @@ -137,23 +151,17 @@ def run(self): api.cuboid_on(self.p_loc, self.p_size, 1, self.p_size) Direction.direction = None - if self.b_loc[1] - self.b_radius < 1 / self.cube_size: - if ((self.p_loc[0] + self.p_radius > self.b_loc[0] > self.p_loc[0] - self.p_radius) and ( - self.p_loc[2] + self.p_radius > self.b_loc[2] > self.p_loc[2] - self.p_radius)): - self.ball_vel_y *= -1 - - if self.b_loc[1] - self.b_radius < 0: - if not ((self.p_loc[0] + self.p_radius > self.b_loc[0] > self.p_loc[0] - self.p_radius) and ( - self.p_loc[2] + self.p_radius > self.b_loc[2] > self.p_loc[2] - self.p_radius)): - self.failed = True - self.done() # Turn on new position # api.led_on(self.ball_loc) - api.cuboid_on(self.b_loc, self.b_size, self.b_size, self.b_size) + api.ball_on(self.b_loc) api.cuboid_on(self.p_loc, self.p_size, 1, self.p_size) api.display() - time.sleep(0.04) + counter = (counter + 1) % 8 + + print(counter) + + time.sleep(0.1) diff --git a/PongMulti.py b/PongMulti.py index ef9a4a7..8b10df5 100644 --- a/PongMulti.py +++ b/PongMulti.py @@ -126,7 +126,7 @@ def run(self): api.cuboid_on(self.player1_loc, self.player1_size, self.player1_size, 1) - if self.player1_action == Direction.Direction.BACK: + if self.player1_action == Direction.Direction.LEFT: api.cuboid_off(self.player1_loc, self.player1_size, self.player1_size, 1) if self.player1_loc[0] + self.mov_val <= 1: @@ -136,7 +136,7 @@ def run(self): api.cuboid_on(self.player1_loc, self.player1_size, self.player1_size, 1) - if self.player1_action == Direction.Direction.FORTH: + if self.player1_action == Direction.Direction.RIGHT: api.cuboid_off(self.player1_loc, self.player1_size, self.player1_size, 1) if self.player1_loc[0] - self.mov_val >= 0: @@ -164,13 +164,19 @@ def run(self): api.cuboid_on(self.ball_loc, self.ball_size, self.ball_size, self.ball_size) time.sleep(3) - # If Ball hits the paddle bounce off of it and do not go into the paddle P1 - if self.ball_loc[0] == 0 and any(loc in [self.ball_loc] for loc in self.player1_loc): - self.ball_loc[0] = 1 + if self.ball_loc[2] - self.ball_radius < 1 / self.cube_size: + if ((self.player1_loc[1] + self.player1_radius * 2 > self.ball_loc[1] > self.player1_loc[ + 1] - self.player1_radius * 2) and ( + self.player1_loc[0] + self.player1_radius * 2 > self.ball_loc[0] > self.player1_loc[ + 0] - self.player1_radius * 2)): + self.ball_vel_z *= -1 - # If Ball hits the paddle bounce off of it and do not go into the paddle P2 - if self.ball_loc[0] == 7 and any(loc in [self.ball_loc] for loc in self.player2_loc): - self.ball_loc[0] = 6 + if self.ball_loc[2] + self.ball_radius > 1 - (1 / self.cube_size): + if ((self.player2_loc[1] + self.player2_radius * 2 > self.ball_loc[1] > self.player2_loc[ + 1] - self.player2_radius * 2) and ( + self.player2_loc[0] + self.player2_radius * 2 > self.ball_loc[0] > self.player2_loc[ + 0] - self.player2_radius * 2)): + self.ball_vel_z *= -1 if self.player1_score == 8 or self.player2_score == 8: self.failed = True @@ -187,4 +193,4 @@ def run(self): api.display() - time.sleep(0.02) + time.sleep(0.1) diff --git a/vCubeAPI.py b/vCubeAPI.py index a292c0e..b27d742 100644 --- a/vCubeAPI.py +++ b/vCubeAPI.py @@ -152,6 +152,7 @@ def pad_led_off(*target_led_location): cube.buffer_cubes[ceil1 + 0 + (floor2 * 64)].setOff() cube.buffer_cubes[floor1 + 0 + ceil2 * 64].setOff() + def draw_sun(target_location, size_x, size_y, size_z): x = target_location[0] * (cubeSize - 1) y = target_location[1] * (cubeSize - 1) @@ -194,11 +195,23 @@ def draw_sun(target_location, size_x, size_y, size_z): for r_x in range(size_x): for r_y in range(size_y): for r_z in range(size_z): - if not ((r_x == 1 or r_x == (size_x - 2)) and (r_y == 1 or r_y == (size_y - 2)) and (r_z == 1 or r_z == (size_z - 2))) and not (((r_x == 0 or r_x == size_x - 1) and (r_y == 0 or r_y == 1 or r_y == size_y - 1 or r_y == size_y - 2)) or ((r_y == 0 or r_y == size_y - 1) and (r_x == 0 or r_x == 1 or r_x == size_x - 1 or r_x == size_x - 2)) or ((r_y == 0 or r_y == size_y - 1) and (r_z == 0 or r_z == 1 or r_z == size_z - 1 or r_z == size_z - 2)) or ((r_z == 0 or r_z == size_z - 1) and (r_y == 0 or r_y == 1 or r_y == size_y - 1 or r_y == size_y - 2)) or ((r_x == 0 or r_x == size_x - 1) and (r_z == 0 or r_z == 1 or r_z == size_z - 1 or r_z == size_z - 2)) or ((r_z == 0 or r_z == size_z - 1) and (r_x == 0 or r_x == 1 or r_x == size_x - 1 or r_x == size_x - 2))): + if not ((r_x == 1 or r_x == (size_x - 2)) and (r_y == 1 or r_y == (size_y - 2)) and ( + r_z == 1 or r_z == (size_z - 2))) and not (((r_x == 0 or r_x == size_x - 1) and ( + r_y == 0 or r_y == 1 or r_y == size_y - 1 or r_y == size_y - 2)) or ( + (r_y == 0 or r_y == size_y - 1) and ( + r_x == 0 or r_x == 1 or r_x == size_x - 1 or r_x == size_x - 2)) or ( + (r_y == 0 or r_y == size_y - 1) and ( + r_z == 0 or r_z == 1 or r_z == size_z - 1 or r_z == size_z - 2)) or ( + (r_z == 0 or r_z == size_z - 1) and ( + r_y == 0 or r_y == 1 or r_y == size_y - 1 or r_y == size_y - 2)) or ( + (r_x == 0 or r_x == size_x - 1) and ( + r_z == 0 or r_z == 1 or r_z == size_z - 1 or r_z == size_z - 2)) or ( + (r_z == 0 or r_z == size_z - 1) and ( + r_x == 0 or r_x == 1 or r_x == size_x - 1 or r_x == size_x - 2))): cube.buffer_cubes[((int(np.ceil(x_center - half_x + r_x)) % cubeSize) + ( - (int(np.ceil(y_center - half_y + r_y)) % cubeSize) * cubeSize) + ( - (int(np.ceil(z_center - half_z + r_z)) % cubeSize) * ( - cubeSize ** 2)))].setOn() + (int(np.ceil(y_center - half_y + r_y)) % cubeSize) * cubeSize) + ( + (int(np.ceil(z_center - half_z + r_z)) % cubeSize) * ( + cubeSize ** 2)))].setOn() def cuboid_on(target_location, size_x, size_y, size_z): @@ -243,10 +256,13 @@ def cuboid_on(target_location, size_x, size_y, size_z): for r_x in range(size_x): for r_y in range(size_y): for r_z in range(size_z): - cube.buffer_cubes[((int(np.ceil(x_center - half_x + r_x)) % cubeSize) + ((int(np.ceil(y_center - half_y + r_y)) % cubeSize) * cubeSize) + ((int(np.ceil(z_center - half_z + r_z)) % cubeSize) * (cubeSize**2)))].setOn() + cube.buffer_cubes[((int(np.ceil(x_center - half_x + r_x)) % cubeSize) + ( + (int(np.ceil(y_center - half_y + r_y)) % cubeSize) * cubeSize) + ( + (int(np.ceil(z_center - half_z + r_z)) % cubeSize) * ( + cubeSize ** 2)))].setOn() -def cuboid_off(target_location, size_x, size_y, size_z): +def cuboid_off(target_location, size_x, size_y, size_z): x = target_location[0] * (cubeSize - 1) y = target_location[1] * (cubeSize - 1) z = target_location[2] * (cubeSize - 1) @@ -288,7 +304,33 @@ def cuboid_off(target_location, size_x, size_y, size_z): for r_x in range(size_x): for r_y in range(size_y): for r_z in range(size_z): - cube.buffer_cubes[((int(np.ceil(x_center - half_x + r_x)) % cubeSize) + ((int(np.ceil(y_center - half_y + r_y)) % cubeSize) * cubeSize) + ((int(np.ceil(z_center - half_z + r_z)) % cubeSize) * (cubeSize**2)))].setOff() + cube.buffer_cubes[((int(np.ceil(x_center - half_x + r_x)) % cubeSize) + ( + (int(np.ceil(y_center - half_y + r_y)) % cubeSize) * cubeSize) + ( + (int(np.ceil(z_center - half_z + r_z)) % cubeSize) * ( + cubeSize ** 2)))].setOff() + + +def ball_on(target_location): + x = target_location[0] * (cubeSize - 1) + y = target_location[1] * (cubeSize - 1) + z = target_location[2] * (cubeSize - 1) + print(x, y, z, sep=" ") + x_center = round(x) + y_center = round(y) + z_center = round(z) + cube.buffer_cubes[((x_center % cubeSize) + ((y_center % cubeSize) * cubeSize) + ( + (z_center % cubeSize) * (cubeSize ** 2)))].setOn() + + +def ball_off(target_location): + x = target_location[0] * (cubeSize - 1) + y = target_location[1] * (cubeSize - 1) + z = target_location[2] * (cubeSize - 1) + x_center = round(x) + y_center = round(y) + z_center = round(z) + cube.buffer_cubes[((x_center % cubeSize) + ((y_center % cubeSize) * cubeSize) + ( + (z_center % cubeSize) * (cubeSize ** 2)))].setOff() def start(): From 0df98bbfa587b093487952052b83f5bfc4afa086 Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 8 Feb 2019 15:18:00 +0100 Subject: [PATCH 4/4] aliasing --- Pong.py | 13 +++++---- PongMulti.py | 81 ++++++++++++++++++++++++---------------------------- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/Pong.py b/Pong.py index 9dbd727..1cfa626 100644 --- a/Pong.py +++ b/Pong.py @@ -81,23 +81,28 @@ def run(self): self.ball_loc[2] += self.ball_vel_z """ - api.ball_off(self.b_loc) + api.cuboid_off(self.b_loc, 1, 1, 1) if counter == 7: self.b_loc[0] += self.ball_vel_x self.b_loc[1] += self.ball_vel_y self.b_loc[2] += self.ball_vel_z + changed = False + if self.b_loc[0] - self.b_radius < 0 or self.b_loc[0] + self.b_radius > 1: self.ball_vel_x *= -1 + changed = True if self.b_loc[1] - self.b_radius < 0 or self.b_loc[1] + self.b_radius > 1: self.ball_vel_y *= -1 + changed = True if self.b_loc[2] - self.b_radius < 0 or self.b_loc[2] + self.b_radius > 1: self.ball_vel_z *= -1 + changed = True - if self.b_loc[1] - self.b_radius < 1 / self.cube_size: + if self.b_loc[1] - self.b_radius < 1 / self.cube_size and not changed: if ((self.p_loc[0] + self.p_radius > self.b_loc[0] > self.p_loc[0] - self.p_radius) and ( self.p_loc[2] + self.p_radius > self.b_loc[2] > self.p_loc[2] - self.p_radius)): self.ball_vel_y *= -1 @@ -155,13 +160,11 @@ def run(self): # Turn on new position # api.led_on(self.ball_loc) - api.ball_on(self.b_loc) + api.cuboid_on(self.b_loc, 1, 1, 1) api.cuboid_on(self.p_loc, self.p_size, 1, self.p_size) api.display() counter = (counter + 1) % 8 - print(counter) - time.sleep(0.1) diff --git a/PongMulti.py b/PongMulti.py index 8b10df5..c6f2e35 100644 --- a/PongMulti.py +++ b/PongMulti.py @@ -30,11 +30,11 @@ def __init__(self, cube_size, frame_size): # Ball self.ball_size = 1 - self.ball_loc = [0.4, 0.6, 0.5] + self.ball_loc = [0.250, 0.5, 0.250] self.ball_radius = (self.ball_size / self.cube_size) / 2 - self.ball_vel_x = 0.01 - self.ball_vel_y = 0.01 - self.ball_vel_z = 0.01 + self.ball_vel_x = 0.125 + self.ball_vel_y = 0.125 + self.ball_vel_z = 0.125 # Player1 self.player1_loc = [0.5, 0.5, 0] # Left Side P1 @@ -83,24 +83,45 @@ def done(self): time.sleep(1) def run(self): + counter = 0 while not self.failed: api.cuboid_off(self.ball_loc, self.ball_size, self.ball_size, self.ball_size) - # Ball moving - self.ball_loc[0] += self.ball_vel_x - self.ball_loc[1] += self.ball_vel_y - self.ball_loc[2] += self.ball_vel_z + if counter == 7: + # Ball moving + self.ball_loc[0] += self.ball_vel_x + self.ball_loc[1] += self.ball_vel_y + self.ball_loc[2] += self.ball_vel_z - # Ball turning on wall impact - if self.ball_loc[0] - self.ball_radius < 0 or self.ball_loc[0] + self.ball_radius > 1: - self.ball_vel_x *= -1 + changed = False - if self.ball_loc[1] - self.ball_radius < 0 or self.ball_loc[1] + self.ball_radius > 1: - self.ball_vel_y *= -1 + # Ball turning on wall impact + if self.ball_loc[0] - self.ball_radius < 0 or self.ball_loc[0] + self.ball_radius > 1: + self.ball_vel_x *= -1 + changed = True - if self.ball_loc[2] - self.ball_radius < 0 or self.ball_loc[2] + self.ball_radius > 1: - self.ball_vel_z *= -1 + if self.ball_loc[1] - self.ball_radius < 0 or self.ball_loc[1] + self.ball_radius > 1: + self.ball_vel_y *= -1 + changed = True + + if self.ball_loc[2] - self.ball_radius < 0 or self.ball_loc[2] + self.ball_radius > 1: + self.ball_vel_z *= -1 + changed = True + + if self.ball_loc[2] - self.ball_radius < 1 / self.cube_size and not changed: + if ((self.player1_loc[1] + self.player1_radius * 2 > self.ball_loc[1] > self.player1_loc[ + 1] - self.player1_radius * 2) and ( + self.player1_loc[0] + self.player1_radius * 2 > self.ball_loc[0] > self.player1_loc[ + 0] - self.player1_radius * 2)): + self.ball_vel_z *= -1 + + if self.ball_loc[2] + self.ball_radius > 1 - (1 / self.cube_size) and not changed: + if ((self.player2_loc[1] + self.player2_radius * 2 > self.ball_loc[1] > self.player2_loc[ + 1] - self.player2_radius * 2) and ( + self.player2_loc[0] + self.player2_radius * 2 > self.ball_loc[0] > self.player2_loc[ + 0] - self.player2_radius * 2)): + self.ball_vel_z *= -1 # Player moving self.player1_action = Direction.direction @@ -148,35 +169,7 @@ def run(self): Direction.direction = None - # If Ball hits player1 wall - if self.ball_loc[0] == 0 and not any(loc in [self.ball_loc] for loc in self.player1_loc): - self.player2_score += 1 - api.cuboid_off(self.ball_loc, self.ball_size, self.ball_size, self.ball_size) - self.ball_loc = [0.4, 0.6, 0.5] - api.cuboid_on(self.ball_loc, self.ball_size, self.ball_size, self.ball_size) - time.sleep(3) - - # If Ball hits player2 wall - if self.ball_loc[0] == 1 and not any(loc in [self.ball_loc] for loc in self.player2_loc): - self.player1_score += 1 - api.cuboid_off(self.ball_loc, self.ball_size, self.ball_size, self.ball_size) - self.ball_loc = [0.4, 0.6, 0.5] - api.cuboid_on(self.ball_loc, self.ball_size, self.ball_size, self.ball_size) - time.sleep(3) - - if self.ball_loc[2] - self.ball_radius < 1 / self.cube_size: - if ((self.player1_loc[1] + self.player1_radius * 2 > self.ball_loc[1] > self.player1_loc[ - 1] - self.player1_radius * 2) and ( - self.player1_loc[0] + self.player1_radius * 2 > self.ball_loc[0] > self.player1_loc[ - 0] - self.player1_radius * 2)): - self.ball_vel_z *= -1 - if self.ball_loc[2] + self.ball_radius > 1 - (1 / self.cube_size): - if ((self.player2_loc[1] + self.player2_radius * 2 > self.ball_loc[1] > self.player2_loc[ - 1] - self.player2_radius * 2) and ( - self.player2_loc[0] + self.player2_radius * 2 > self.ball_loc[0] > self.player2_loc[ - 0] - self.player2_radius * 2)): - self.ball_vel_z *= -1 if self.player1_score == 8 or self.player2_score == 8: self.failed = True @@ -193,4 +186,6 @@ def run(self): api.display() + counter = (counter + 1) % 8 + time.sleep(0.1)