Skip to content

Commit

Permalink
Merge pull request #1 from MotherTeresaHS/master
Browse files Browse the repository at this point in the history
Read the docs template
  • Loading branch information
cameron-teed authored Dec 11, 2019
2 parents d062e26 + e11170c commit af7efce
Show file tree
Hide file tree
Showing 64 changed files with 533 additions and 184 deletions.
192 changes: 192 additions & 0 deletions code.py
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()
Binary file added coin.wav
Binary file not shown.
28 changes: 28 additions & 0 deletions constants.py
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 added docs/.DS_Store
Binary file not shown.
18 changes: 9 additions & 9 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
master_doc = 'index'

# General information about the project.
project = u'Computer Based Problem Solving'
copyright = u'2020, Patrick Coxall'
project = u'Space Aliens - CircuitPython Game'
copyright = u'2020, Mr. Coxall'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
Expand Down Expand Up @@ -131,7 +131,7 @@

# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
html_logo = "images/cs-logo.png"
html_logo = "images/space_aliens.png"

# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
Expand Down Expand Up @@ -216,8 +216,8 @@ def setup(app):
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'toga.tex', u'Computer Based Problem Solving',
u'Patrick Coxall', 'manual'),
('index', 'toga.tex', u'Space Aliens - CircuitPython Game',
u'Mr. Coxall', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -246,8 +246,8 @@ def setup(app):
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
('index', 'CBPS', u'Computer Based Problem Solving',
[u'Patrick Coxall'], 1)
('index', 'CBPS', u'Space Aliens - CircuitPython Game',
[u'Mr. Coxall'], 1)
]

# If true, show URL addresses after external links.
Expand All @@ -260,8 +260,8 @@ def setup(app):
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
('index', 'CBPS', u'Computer Based Problem Solving',
u'Patrick Coxall', 'CBPS', 'Computer Based Problem Solving Textbook.',
('index', 'CBPS', u'Space Aliens - CircuitPython Game',
u'Mr. Coxall', 'Space Aliens', 'Space Aliens - CircuitPython Game',
'Miscellaneous'),
]

Expand Down
Binary file added docs/game/.DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions docs/game/background.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _background:

Background
==========

X
Binary file not shown.
14 changes: 14 additions & 0 deletions docs/game/index.rst
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>
6 changes: 6 additions & 0 deletions docs/game/space_ship.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. _space_ship:

Space Ship
==========

X
Binary file not shown.
Binary file added docs/ide/chrome_text_ide.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ide/circuitpython_mu-front-page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/ide/hello_world.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
72 changes: 72 additions & 0 deletions docs/ide/index.rst
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 added docs/image_bank/.DS_Store
Binary file not shown.
Binary file added docs/image_bank/ball.bmp
Binary file not shown.
Binary file added docs/image_bank/boom.wav
Binary file not shown.
Loading

0 comments on commit af7efce

Please sign in to comment.