Skip to content

Commit

Permalink
Comprehensive commit celebrating the achievement of the first milesto…
Browse files Browse the repository at this point in the history
…ne on LEGO Ideas (https://ideas.lego.com/projects/53e8d771-166c-4a35-85d3-25e3512eb799) and release of version 1.1 of PortaBrick Arcade software.

pixel_pics.py:
- renamed to pixel_library.py
- changed structure (Pixel pics now organized in dictionary)
game_control.py:
- adjustments to the changes of pixel_library.py
main.py:
- fundamental overhaul
- Introduction of new menu structure
- preparation for integration of new games
- adjustments to the changes of pixel_library.py
brick_pong.py:
- minor adjustments
brick_snake.py:
- minor adjustments
PixelArtPainter.htm:
minor adjustments
  • Loading branch information
lc-jrx committed Aug 23, 2023
1 parent cbe2c59 commit e427512
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 70 deletions.
2 changes: 1 addition & 1 deletion PixelArtGen.html → PixelArtPainter.htm
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ <h3>Coordinates</h3>
<h4>About</h4>
<p>This tool is designed to draw icons and pixel art displayed on matrixes built from Spike Prime ColorLightMatrix modules. To transfer a drawn picture to Pixel Pics Library copy the coordinates, edit pixel_pics.py, insert a valid new variable, and insert the coordinates.</p>
<p>Large parts of the code were created with <a href="https://chat.openai.com">ChatGPT</a> and manually customized and improved.</p>
<p>The Pixel Art Painter is part of the PortaBrick Arcade project. Learn more at <a href="https://www.lc-jrx.com">LC-jrx.com</a>. Source Code is available at <a href="https://github.com/lc-jrx/PortaBrick_Arcade">GitHub</a>. The PortaBrick Arcade, the PortaBrick Arcade software and the PortaBrick Arcade games are licensed under the MIT License.</p>
<p>The Pixel Art Painter is part of the PortaBrick Arcade project. Learn more at <a href="https://www.lc-jrx.com/en/2023/08/19/creations/machines/moc-portabrick-arcade-636.html">LC-jrx.com</a> and <a href="https://ideas.lego.com/projects/53e8d771-166c-4a35-85d3-25e3512eb799">LEGO Ideas</a>. Source Code is available at <a href="https://github.com/lc-jrx/PortaBrick_Arcade">GitHub</a>. The PortaBrick Arcade, the PortaBrick Arcade software and the PortaBrick Arcade games are licensed under the MIT License.</p>
<h4>Disclaimer</h4>
<p>LEGO® is a trademark of the LEGO Group of companies which does not sponsor, authorize or endorse this project.</p>
</div>
Expand Down
6 changes: 4 additions & 2 deletions brick_pong.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from pybricks.tools import wait as blocking_wait, StopWatch
from matrix_helper import MatrixHelper
from game_control import GameControl
from pixel_pics import PixelPics
from urandom import randint


Expand All @@ -34,8 +33,12 @@ class BrickPong:
code of the game was generated by chat.openai.com and then adapted."""

def __init__(self, display_res_x, display_res_y):
# Basic variables
self.__screen_width = display_res_x
self.__screen_height = display_res_y
self.app_icon = "pong"
self.app_name = "Pong"
self.app_color = Color.BLUE

# Set up game variables
self.__ball_x, self.__ball_y = None, None
Expand All @@ -54,7 +57,6 @@ def __init__(self, display_res_x, display_res_y):
# Initialize software and hardware
self.__gamecontrol = GameControl(self.__screen_width, self.__screen_height, self.__game_speed)
self.__matrix = MatrixHelper(self.__screen_width, self.__screen_height) # initialize driver for matrix
self.__pixel_pics = PixelPics() # initialize pixel drawings library
self.__hub = PrimeHub() # initialize LEGO Spike Prime Hub
self.__button_L = ForceSensor(Port.E) # initialize LEGO Spike Prime Force Sensor as left button
self.__button_R = ForceSensor(Port.F) # initialize LEGO Spike Prime Force Sensor as right button
Expand Down
7 changes: 4 additions & 3 deletions brick_snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from pybricks.tools import wait as blocking_wait, StopWatch
from matrix_helper import MatrixHelper
from game_control import GameControl
from pixel_pics import PixelPics
from urandom import randint


Expand All @@ -37,6 +36,9 @@ def __init__(self, display_res_x, display_res_y):
# Basic variables
self.__resolution = (display_res_x, display_res_y)
self.__game_speed = 300 # work speed itself
self.app_icon = "snake"
self.app_name = "Snake"
self.app_color = Color.ORANGE

# Variables for game SNAKE
self.__hardgame = False # If True hitting the wall ends the game
Expand All @@ -48,7 +50,7 @@ def __init__(self, display_res_x, display_res_y):
self.__render_off = []
self.__snake_had_lunch = None

self.loop = None # Prevents a 180 degree turn on same.
self.loop = None # Prevents a 180 degree turn on same.
self.__game_counter = None
self.__gameover = None
self.__quit = False
Expand All @@ -57,7 +59,6 @@ def __init__(self, display_res_x, display_res_y):
# Initialize software and hardware
self.__gamecontrol = GameControl(self.__resolution[0], self.__resolution[1], self.__game_speed)
self.__matrix = MatrixHelper(self.__resolution[0], self.__resolution[1]) # initialize driver for matrix
self.__pixel_pics = PixelPics() # initialize pixel drawings library
self.__hub = PrimeHub() # initialize LEGO Spike Prime Hub
self.__button_L = ForceSensor(Port.E) # initialize LEGO Spike Prime Force Sensor as left button
self.__button_R = ForceSensor(Port.F) # initialize LEGO Spike Prime Force Sensor as right button
Expand Down
15 changes: 7 additions & 8 deletions game_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from pybricks.parameters import Button, Color
from pybricks.tools import wait
from matrix_helper import MatrixHelper
from pixel_pics import PixelPics
from pixel_library import PixelLibrary


class GameControl:
Expand All @@ -41,13 +41,10 @@ def __init__(self, display_res_x, display_res_y, default_gamespeed):

# Initialize software and hardware
self.__matrix = MatrixHelper(self.__resolution[0], self.__resolution[1]) # initialize driver for matrix
self.__pixel_pics = PixelPics() # initialize pixel drawings library
self.__pixel_lib = PixelLibrary() # initialize pixel drawings library
self.__hub = PrimeHub() # initialize LEGO Spike Prime Hub
self.__hub_buttons = [] # initialize variable that holds info about pressed button

self.__hub.system.set_stop_button(None) # Disable Center button to be used as return button
self.__hub.system.set_stop_button(Button.BLUETOOTH) # Set Bluetooth button as stop button for system

def set_game_settings(self):
""" Sets the game variables for game speed and for the difficulty level.
:return:
Expand Down Expand Up @@ -99,7 +96,7 @@ def reset_game(self):
game_quit = None
game_reset = None
self.__matrix.matrix_off()
self.__matrix.draw_pixel_graphic(self.__pixel_pics.smiley, Color.GREEN)
self.__matrix.draw_pixel_graphic(self.__pixel_lib.pixelpics('smiley'), Color.GREEN)
self.__hub.display.text("Play again?", 200, 50)
while not action:
pressed = self.__hub.buttons.pressed()
Expand All @@ -118,6 +115,8 @@ def reset_game(self):

def gameover(self):
self.__matrix.matrix_off()
self.__hub.speaker.play_notes(["B3/2", "B2/2"], 160) # make a sad sound
self.__matrix.draw_pixel_graphic(self.__pixel_pics.smiley_sad, Color.RED) # show a grafik for game over
# make a sad sound
self.__hub.speaker.play_notes(["B3/2", "B2/2"], 160)
# show a grafik for game over
self.__matrix.draw_pixel_graphic(self.__pixel_lib.pixelpics('smiley_sad'), Color.RED)
self.__hub.display.text("Game Over", 200, 50)
103 changes: 57 additions & 46 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,96 +17,107 @@
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

from pybricks.hubs import PrimeHub
from pybricks.parameters import Icon, Button
from pybricks.parameters import Icon, Button, Color
from pybricks.tools import wait

# Import helper files (ColorMatrixDisplay driver, pixel pic library)
# Import helper files (ColorMatrixDisplay driver, pixel library)
from matrix_helper import MatrixHelper
from pixel_pics import PixelPics
from pixel_library import PixelLibrary

# Import games
# Import games (Add additional games here and initialize them in "__init__" section)
from brick_pong import BrickPong
from brick_snake import BrickSnake


class PortaBrickArcade:
""" Main Class of the PortaBrick Arcade. Controls the main menu and leads into the two games.
""" Main Class of the PortaBrick Arcade. Controls the main menu and leads into the games.
"""

def __init__(self):
# Basic variables
self.display_resolution = (6, 6)
self.pressed = []

# Load available games (Must be initialized AND added to "available_games" dictionary!!!)
self.snake_game = BrickSnake(self.display_resolution[0], self.display_resolution[1])
self.pong_game = BrickPong(self.display_resolution[0], self.display_resolution[1])
self.available_games = (self.snake_game, self.pong_game)

# Initialize classes and Hardware
self.hub = PrimeHub()
self.matrix = MatrixHelper(self.display_resolution[0], self.display_resolution[1])
self.pixel_pics = PixelPics()
self.pressed = []
# self.state = True
self.pixel_lib = PixelLibrary()

# Change functions of hub's buttons
self.hub.system.set_stop_button(None) # Disable Center button to be used as return button
self.hub.system.set_stop_button(Button.BLUETOOTH) # Set Bluetooth button as stop button for system

def start_up(self):
self.matrix.draw_pixel_graphic(self.pixel_lib.pixelpics("heart"), Color.RED)
self.hub.display.text("PortaBrick Arcade", 200, 50)
self.hub.display.icon(Icon.HEART)
self.matrix.matrix_off()
wait(1000)

def game_snake(self):
snake_game = BrickSnake(self.display_resolution[0], self.display_resolution[1])
snake_game.gameplay()

def game_pong(self):
pong_game = BrickPong(self.display_resolution[0], self.display_resolution[1])
pong_game.gameplay()

def end_session(self):
self.matrix.matrix_off()
self.hub.display.off()

def dialog(self):
self.hub.display.text("SNAKE", 200, 50)
counter = 0 # Counter for menu entries
action = False # True closes game session and restarts dialog
sys_quit = False # True closes program

def show_menu_entry(i):
self.matrix.matrix_off()
self.matrix.draw_pixel_graphic(self.pixel_lib.pixelpics(self.available_games[i].app_icon),
self.available_games[i].app_color)
self.hub.display.text(self.available_games[i].app_name, 200, 50)

# Show little Howto how to choose the entries
self.hub.display.text("Choose game", 200, 100)
wait(500)
self.hub.display.icon(Icon.ARROW_LEFT_DOWN)
wait(1000)
self.hub.display.text("PONG", 200, 50)
self.hub.display.text("Next", 200, 100)
wait(500)
self.hub.display.icon(Icon.ARROW_RIGHT_DOWN)
wait(1000)
self.hub.display.text("Quit", 200, 50)
self.hub.display.text("Previous", 200, 100)
wait(500)
self.hub.display.icon(Icon.ARROW_RIGHT_UP)
self.hub.display.icon(Icon.ARROW_LEFT_DOWN)
wait(1000)
self.hub.display.char("?")
self.hub.display.text("Quit", 200, 100)
wait(500)
self.hub.display.icon(Icon.ARROW_RIGHT_UP)
wait(1000)

def dialog_input(self):
action = False # set input status
sys_quit = False # True closes game session

while not sys_quit:
self.dialog()
# Show the first menu entry
show_menu_entry(counter)

while not action:
pressed = self.hub.buttons.pressed()
if Button.LEFT in pressed:
self.hub.display.char("S")
self.game_snake()
action = True
if counter > 0:
counter -= 1
show_menu_entry(counter)
elif Button.RIGHT in pressed:
self.hub.display.char("P")
self.game_pong()
if counter < len(self.available_games) - 1:
counter += 1
show_menu_entry(counter)
elif Button.CENTER in pressed:
self.available_games[counter].gameplay()
action = True
elif Button.BLUETOOTH in pressed:
self.hub.display.char("Q")
# self.state = False
sys_quit = True
action = True
self.end_session()
action = False
self.end_session()


if __name__ == "__main__":

game_session = PortaBrickArcade()
game_session.start_up()
# game_session.dialog()
game_session.dialog_input()
game_session.end_session()
try:
game_session = PortaBrickArcade()
game_session.start_up()
game_session.dialog()
except SystemExit:
game_session.end_session()

# Leave the next line empty to fullfil PEP 8
32 changes: 22 additions & 10 deletions pixel_pics.py → pixel_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@
"""


class PixelPics:
class PixelLibrary:
def __init__(self):
self.characters_dict = None
self.smiley = ((0, 0), (1, 0), (0, 1), (1, 1), (4, 0), (5, 0), (4, 1),
(5, 1), (0, 3), (1, 4), (2, 4), (3, 4), (4, 4), (5, 3))
self.smiley_sad = ((0, 0), (1, 0), (0, 1), (1, 1), (4, 0), (5, 0), (4, 1),
(5, 1), (0, 5), (1, 4), (2, 4), (3, 4), (4, 4), (5, 5))

self.__characters_dict = None
self.__pixelpics = None

def pixelpics(self, icon):
self.__pixelpics = {
'smiley': [(0, 0), (1, 0), (0, 1), (1, 1), (4, 0), (5, 0), (4, 1), (5, 1), (0, 3), (1, 4), (2, 4), (3, 4),
(4, 4), (5, 3)],
'smiley_sad': [(0, 0), (1, 0), (0, 1), (1, 1), (4, 0), (5, 0), (4, 1), (5, 1), (0, 5), (1, 4), (2, 4),
(3, 4), (4, 4), (5, 5)],
'heart': [(1, 0), (0, 1), (0, 2), (1, 3), (2, 4), (3, 4), (4, 3), (5, 2), (5, 1), (4, 0), (3, 1), (2, 1),
(1, 1), (1, 2), (2, 2), (3, 2), (4, 2), (4, 1), (3, 3), (2, 3), (0, 3), (1, 4), (2, 5), (3, 5),
(4, 4), (5, 3)],
'pong': [(0, 1), (0, 2), (0, 3), (5, 4), (5, 3), (5, 2), (2, 2)],
'snake': [(1, 4), (2, 4), (3, 4), (4, 4), (4, 3), (4, 1)]
}

return self.__pixelpics[icon]

def characters(self, char_input):
self.characters_dict = {
self.__characters_dict = {
'A': [(1, 1), (2, 0), (3, 0), (4, 1), (4, 2), (4, 3), (4, 4), (4, 5), (1, 5), (1, 4), (1, 3), (1, 2),
(2, 3), (3, 3)],
'B': [(1, 0), (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 5), (3, 5), (4, 4), (4, 3), (3, 2), (2, 2),
Expand Down Expand Up @@ -90,5 +102,5 @@ def characters(self, char_input):
'!': [(2, 0), (3, 0), (2, 3), (2, 5), (3, 2), (3, 5), (3, 3), (2, 2), (2, 1), (3, 1)],
' ': []
}
return self.characters_dict[char_input.upper()]

return self.__characters_dict[char_input.upper()]

0 comments on commit e427512

Please sign in to comment.