-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from MotherTeresaHS/master
Read the docs template
- Loading branch information
Showing
64 changed files
with
533 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Created by: ???? | ||
# Created on: ???? 2019 | ||
# This file is the "????" game | ||
# for CircuitPython | ||
|
||
import ugame | ||
import stage | ||
import board | ||
import neopixel | ||
import time | ||
import random | ||
|
||
import constants | ||
|
||
|
||
def blank_white_reset_scene(): | ||
# this function is the splash scene game loop | ||
|
||
# do house keeping to ensure everythng is setup | ||
|
||
# set up the NeoPixels | ||
pixels = neopixel.NeoPixel(board.NEOPIXEL, 5, auto_write=False) | ||
pixels.deinit() # and turn them all off | ||
|
||
# reset sound to be off | ||
sound = ugame.audio | ||
sound.stop() | ||
sound.mute(False) | ||
|
||
# an image bank for CircuitPython | ||
image_bank_1 = stage.Bank.from_bmp16("mt_game_studio.bmp") | ||
|
||
# sets the background to image 0 in the bank | ||
background = stage.Grid(image_bank_1, 160, 120) | ||
|
||
# create a stage for the background to show up on | ||
# and set the frame rate to 60fps | ||
game = stage.Stage(ugame.display, 60) | ||
# set the layers, items show up in order | ||
game.layers = [background] | ||
# render the background and inital location of sprite list | ||
# most likely you will only render background once per scene | ||
game.render_block() | ||
|
||
# repeat forever, game loop | ||
while True: | ||
# get user input | ||
|
||
# update game logic | ||
|
||
# Wait for 1/2 seconds | ||
time.sleep(0.5) | ||
mt_splash_scene() | ||
|
||
# redraw sprite list | ||
|
||
def mt_splash_scene(): | ||
# this function is the MT splash scene | ||
|
||
# an image bank for CircuitPython | ||
image_bank_2 = stage.Bank.from_bmp16("mt_game_studio.bmp") | ||
|
||
# sets the background to image 0 in the bank | ||
background = stage.Grid(image_bank_2, constants.SCREEN_GRID_X, constants.SCREEN_GRID_Y) | ||
|
||
# used this program to split the iamge into tile: https://ezgif.com/sprite-cutter/ezgif-5-818cdbcc3f66.png | ||
background.tile(2, 2, 0) # blank white | ||
background.tile(3, 2, 1) | ||
background.tile(4, 2, 2) | ||
background.tile(5, 2, 3) | ||
background.tile(6, 2, 4) | ||
background.tile(7, 2, 0) # blank white | ||
|
||
background.tile(2, 3, 0) # blank white | ||
background.tile(3, 3, 5) | ||
background.tile(4, 3, 6) | ||
background.tile(5, 3, 7) | ||
background.tile(6, 3, 8) | ||
background.tile(7, 3, 0) # blank white | ||
|
||
background.tile(2, 4, 0) # blank white | ||
background.tile(3, 4, 9) | ||
background.tile(4, 4, 10) | ||
background.tile(5, 4, 11) | ||
background.tile(6, 4, 12) | ||
background.tile(7, 4, 0) # blank white | ||
|
||
background.tile(2, 5, 0) # blank white | ||
background.tile(3, 5, 0) | ||
background.tile(4, 5, 13) | ||
background.tile(5, 5, 14) | ||
background.tile(6, 5, 0) | ||
background.tile(7, 5, 0) # blank white | ||
|
||
text = [] | ||
|
||
text1 = stage.Text(width=29, height=14, font=None, palette=constants.MT_GAME_STUDIO_PALETTE, buffer=None) | ||
text1.move(20, 10) | ||
text1.text("MT Game Studios") | ||
text.append(text1) | ||
|
||
text2 = stage.Text(width=29, height=14, font=None, palette=constants.MT_GAME_STUDIO_PALETTE, buffer=None) | ||
text2.move(35, 110) | ||
text2.text("PRESS START") | ||
text.append(text2) | ||
|
||
# get sound ready | ||
# follow this guide to convert your other sounds to something that will work | ||
# https://learn.adafruit.com/microcontroller-compatible-audio-file-conversion | ||
coin_sound = open("coin.wav", 'rb') | ||
sound = ugame.audio | ||
sound.stop() | ||
sound.mute(False) | ||
sound.play(coin_sound) | ||
|
||
# create a stage for the background to show up on | ||
# and set the frame rate to 60fps | ||
game = stage.Stage(ugame.display, 60) | ||
# set the layers, items show up in order | ||
game.layers = text + [background] | ||
# render the background and inital location of sprite list | ||
# most likely you will only render background once per scene | ||
game.render_block() | ||
|
||
# repeat forever, game loop | ||
while True: | ||
# get user input | ||
|
||
# update game logic | ||
|
||
# Wait for 1 seconds | ||
time.sleep(1.0) | ||
game_splash_scene() | ||
|
||
# redraw sprite list | ||
|
||
def game_splash_scene(): | ||
# this function is the game scene | ||
|
||
# repeat forever, game loop | ||
while True: | ||
# get user input | ||
|
||
# update game logic | ||
|
||
# redraw sprite list | ||
pass # just a placeholder until you write the code | ||
|
||
|
||
def main_menu_scene(): | ||
# this function is the game scene | ||
|
||
# repeat forever, game loop | ||
while True: | ||
# get user input | ||
|
||
# update game logic | ||
|
||
# redraw sprite list | ||
pass # just a placeholder until you write the code | ||
|
||
|
||
def game_scene(): | ||
# this function is the game scene | ||
|
||
# repeat forever, game loop | ||
while True: | ||
# get user input | ||
|
||
# update game logic | ||
|
||
# redraw sprite list | ||
pass # just a placeholder until you write the code | ||
|
||
|
||
def game_over_scene(final_score): | ||
# this function is the game over scene | ||
|
||
# repeat forever, game loop | ||
while True: | ||
# get user input | ||
|
||
# update game logic | ||
|
||
# redraw sprite list | ||
pass # just a placeholder until you write the code | ||
|
||
|
||
if __name__ == "__main__": | ||
blank_white_reset_scene() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Created by: Mr. Coxall | ||
# Created on: October 2019 | ||
# This constants file is CircuitPython Stage game | ||
|
||
# CircuitPython screen size is 160x128 and sprites are 16x16 | ||
SCREEN_X = 160 | ||
SCREEN_Y = 128 | ||
SCREEN_GRID_X = 16 | ||
SCREEN_GRID_Y = 8 | ||
SPRITE_SIZE = 16 | ||
OFF_TOP_SCREEN = -1 * SPRITE_SIZE | ||
OFF_BOTTOM_SCREEN = SCREEN_Y + SPRITE_SIZE | ||
|
||
MT_GAME_STUDIO_PALETTE = (b'\xf8\x1f\x00\x00\xcey\x00\xff\xf8\x1f\xff\x19\xfc\xe0\xfd\xe0' | ||
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff') | ||
|
||
SCORE_PALETTE = (b'\xf8\x1f\x00\x00\xcey\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff' | ||
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff') | ||
|
||
# Using for button state | ||
button_state = { | ||
"button_up": "up", | ||
"button_just_pressed": "just pressed", | ||
"button_still_pressed": "still pressed", | ||
"button_released": "released", | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.. _background: | ||
|
||
Background | ||
========== | ||
|
||
X |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.. _game: | ||
|
||
**** | ||
Game | ||
**** | ||
|
||
X | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
:glob: | ||
|
||
Background <background> | ||
Space Ship <space_ship> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.. _space_ship: | ||
|
||
Space Ship | ||
========== | ||
|
||
X |
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
Your IDE | ||
======== | ||
|
||
One of the great things about CircuitPython hardware is that it just automatically shows up as a USB drive when you attach it to your computer. This means that you can access and save your code using any text editor. This is particularly helpful in schools, where computers are likely to be locked down so students can not load anything. Also students might be using Chromebooks, where only "authorized" Chrome extensions can be loaded. | ||
|
||
If you are working on a Chromebook, the easiest way to start coding is to just use the built in `Text app <https://chrome.google.com/webstore/detail/text/mmfbcljfglbokpmkimbfghdkjmjhdgbg?hl=en>`_. As soon as you open or save a file with a :file:`*.py` extension, it will know it is Python code and automatically start syntax highlighting. | ||
|
||
.. figure:: ./chrome_text_ide.png | ||
:width: 480 px | ||
:align: center | ||
:alt: Chromebook Text Editor | ||
|
||
Chromebook Text app | ||
|
||
If you are using a non-Chromebook computer, your best beat for an IDE is `Mu <https://codewith.mu>`_. You can get it for Windows, Mac, Raspberry Pi and Linux. It works seamlessly with CircuitPython and the serial console will give you much needed debugging information. You can download Mu `here <https://codewith.mu/en/download>`_. | ||
|
||
.. figure:: ./circuitpython_mu-front-page.png | ||
:width: 480 px | ||
:alt: Mu Editor | ||
:align: center | ||
|
||
Mu IDE | ||
|
||
Since with CircuitPython devices you are just writing Python files to a USB drive, you are more than welcome to use any IDE that you are familiar using. | ||
|
||
Hello, World! | ||
------------- | ||
|
||
Yes, you know that first program you should always run when starting a new coding adventure, just to ensure everything is running correctly! Once you have access to your IDE and you have CircuitPython loaded, you should make sure everything is working before you move on. To do this we will do the traditional "Hello, World!" program. By default CircuitPython looks for a file called :file:`code.py` in the root directory of the PyBadge to start up. You will place the following code in the :file:`code.py` file: | ||
|
||
.. code-block:: python | ||
:linenos: | ||
print("Hello, World!") | ||
As soon as you save the file onto the PyBadge, the screen should flash and you should see something like: | ||
|
||
.. figure:: ./hello_world.png | ||
:width: 480 px | ||
:alt: Hello, World! | ||
:align: center | ||
|
||
Hello, World! program on PyBadge | ||
|
||
Although this code does work just as is, it is always nice to ensure we are following proper coding conventions, including style and comments. Here is a better version of Hello, World! You will notice that I have a call to a :py:func:`main()` function. This is common in Python code but not normally seen in CircuitPython. I am including it because by breaking the code into different functions to match different scenes, eventually will be really helpful. | ||
|
||
|
||
.. literalinclude:: ./example.py | ||
:language: py | ||
:lines: 10-20 | ||
|
||
.. code-block:: python | ||
:linenos: | ||
#!/usr/bin/env python3 | ||
# Created by : Mr. Coxall | ||
# Created on : January 2020 | ||
# This program prints out Hello, World! onto the PyBadge | ||
def main(): | ||
# this function prints out Hello, World! onto the PyBadge | ||
print("Hello, World!") | ||
if __name__ == "__main__": | ||
main() | ||
Congratulations, we are ready to start. |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Oops, something went wrong.