Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Last active November 25, 2019 08:02
Show Gist options
  • Save PandaWhoCodes/8f470127baef2957df0f4f347a4d839c to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/8f470127baef2957df0f4f347a4d839c to your computer and use it in GitHub Desktop.
Series of examples to get you started with PyGame Zero
"""
Code to open a pgz window
"""
import pgzrun
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
pgzrun.go()
"""
Code to display a letter in the middle of a pgz window
"""
import pgzrun
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
screen.draw.text("A", (WIDTH/2, HEIGHT/2), fontsize=50, color=WHITE)
pgzrun.go()
"""
Code to display a random letter in the center of the screen
"""
import pgzrun
from random import choice
import string
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
screen.draw.text(choice(string.ascii_letters), (WIDTH/2, HEIGHT/2), fontsize=50, color=WHITE)
pgzrun.go()
"""
Code to take an input from the user and display it on screen
"""
import pgzrun
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LETTER = {"letter": "Give an input"}
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
screen.draw.text(LETTER["letter"], (WIDTH/2, HEIGHT/2), fontsize=50, color=WHITE)
def on_key_down(key, mod, unicode):
if unicode:
LETTER["letter"] = unicode
pgzrun.go()
"""
Code to display a take input from the user and display it on screen at random locations
"""
import pgzrun
from random import randint
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LETTER = {"letter": "Give an input", "x": WIDTH / 2, "y": HEIGHT / 2}
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
screen.draw.text(LETTER["letter"], (LETTER["x"], LETTER["y"]), fontsize=50, color=WHITE)
def on_key_down(key, mod, unicode):
if unicode:
LETTER["letter"] = unicode
LETTER["x"] = randint(1, WIDTH)
LETTER["y"] = randint(1, HEIGHT)
pgzrun.go()
"""
Code to display 1 random letter and change it when user enters the letter
"""
import pgzrun
from random import randint, choice
import string
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LETTER = {"letter": "", "x": 0, "y": 0}
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
screen.draw.text(LETTER["letter"], (LETTER["x"], LETTER["y"]), fontsize=50, color=WHITE)
def on_key_down(key, mod, unicode):
if unicode:
if LETTER["letter"] == unicode:
update_letter()
def update_letter():
LETTER["letter"] = choice(string.ascii_letters).lower()
LETTER["x"] = randint(10, WIDTH-10)
LETTER["y"] = randint(10, HEIGHT-10)
update_letter()
pgzrun.go()
"""
Code to display 1 random letter falling from the top of the screen
and change it when user enters the letter
"""
import pgzrun
from random import randint, choice
import string
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LETTER = {"letter": "", "x": 0, "y": 0}
VELOCITY = 1
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
screen.draw.text(LETTER["letter"], (LETTER["x"], LETTER["y"]), fontsize=50, color=WHITE)
def update():
LETTER["y"] += VELOCITY
def on_key_down(key, mod, unicode):
if unicode:
if LETTER["letter"] == unicode:
update_letter()
def update_letter():
LETTER["letter"] = choice(string.ascii_letters).lower()
LETTER["x"] = randint(10, WIDTH - 10)
LETTER["y"] = 1
update_letter()
pgzrun.go()
"""
Code to invoke update_letter if letter falls below the screen
"""
import pgzrun
from random import randint, choice
import string
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LETTER = {"letter": "", "x": 0, "y": 0}
VELOCITY = 1
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
screen.draw.text(LETTER["letter"], (LETTER["x"], LETTER["y"]), fontsize=50, color=WHITE)
def update():
LETTER["y"] += VELOCITY
if LETTER["y"] == HEIGHT - 5:
update_letter()
def on_key_down(key, mod, unicode):
if unicode:
if LETTER["letter"] == unicode:
update_letter()
def update_letter():
LETTER["letter"] = choice(string.ascii_letters).lower()
LETTER["x"] = randint(10, WIDTH - 10)
LETTER["y"] = 1
update_letter()
pgzrun.go()
"""
Code to keep a track of Right letters Wrong letters entered
"""
import pgzrun
from random import randint, choice
import string
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LETTER = {"letter": "", "x": 0, "y": 0}
VELOCITY = 1
SCORE = {"RIGHT": 0, "WRONG": 0}
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
screen.draw.text(LETTER["letter"], (LETTER["x"], LETTER["y"]), fontsize=50, color=WHITE)
screen.draw.text("RIGHT: " + str(SCORE["RIGHT"]), (WIDTH - 130, 10), fontsize=30, color=WHITE)
screen.draw.text("WRONG: " + str(SCORE["WRONG"]), (WIDTH - 130, 40), fontsize=30, color=WHITE)
def update():
LETTER["y"] += VELOCITY
if LETTER["y"] == HEIGHT - 5:
SCORE["WRONG"] += 1
update_letter()
def on_key_down(key, mod, unicode):
if unicode:
if LETTER["letter"] == unicode:
SCORE["RIGHT"] += 1
update_letter()
else:
SCORE["WRONG"] += 1
def update_letter():
LETTER["letter"] = choice(string.ascii_letters).lower()
LETTER["x"] = randint(10, WIDTH - 20)
LETTER["y"] = 1
update_letter()
pgzrun.go()
"""
Code to have atleast 4 letters falling down at a given time
"""
import pgzrun
from random import randint, choice
import string
WIDTH = 800
HEIGHT = 500
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# LETTER = {"letter": "", "x": 0, "y": 0}
ON_SCREEN_LETTERS = []
VELOCITY = 1
SCORE = {"RIGHT": 0, "WRONG": 0}
def draw(): # Pygame Zero draw function
screen.clear()
screen.fill(BLACK)
for LETTER in ON_SCREEN_LETTERS:
screen.draw.text(LETTER["letter"], (LETTER["x"], LETTER["y"]), fontsize=50, color=WHITE)
screen.draw.text("RIGHT: " + str(SCORE["RIGHT"]), (WIDTH - 130, 10), fontsize=30, color=WHITE)
screen.draw.text("WRONG: " + str(SCORE["WRONG"]), (WIDTH - 130, 40), fontsize=30, color=WHITE)
def update():
for i, LETTER in enumerate(ON_SCREEN_LETTERS):
LETTER["y"] += VELOCITY
if LETTER["y"] == HEIGHT - 5:
SCORE["WRONG"] += 1
delete_letter(i)
while len(ON_SCREEN_LETTERS) < 4:
add_letter()
def on_key_down(key, mod, unicode):
if unicode:
for i, LETTER in enumerate(ON_SCREEN_LETTERS):
if LETTER["letter"] == unicode:
SCORE["RIGHT"] += 1
delete_letter(i)
return
else:
SCORE["WRONG"] += 1
def add_letter():
letter = choice(string.ascii_letters).lower()
x = randint(10, WIDTH - 20)
y = 1
ON_SCREEN_LETTERS.append({"letter": letter, "x": x, "y": y})
def delete_letter(i):
del ON_SCREEN_LETTERS[i]
add_letter()
pgzrun.go()
@doraithodla
Copy link

Nice. Browsed through the code. Will test them all. I will take it from here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment