Skip to main content
Image

r/circuitpython



Explore latest offers. First come, first thrilled. Take home the award-winning Lucid Gravity with exclusive limited-time offers today.
  • Image
    Explore latest offers. First come, first thrilled. Take home the award-winning Lucid Gravity with exclusive limited-time offers today.
  • Image
    Explore latest offers. First come, first thrilled. Take home the award-winning Lucid Gravity with exclusive limited-time offers today.
  • Image
    Explore latest offers. First come, first thrilled. Take home the award-winning Lucid Gravity with exclusive limited-time offers today.


Cool MicroPython Game! Cool MicroPython Game!

I spent a long time making this code and now I am going to share it with y'all. enjoy.

from microbit import *

import random

# Initialize spaceship position and score

ship_x = 2 # Middle of bottom row

score = 0

# Function to draw spaceship

def draw_ship(x):

display.set_pixel(x, 4, 9) # Bottom row, brightness max

# Function to generate new asteroid

def new_asteroid():

return [random.randint(0, 4), 0] # x position, y=0 top row

# List of falling asteroids

asteroids = []

# Main game loop

while True:

display.clear()

# Move spaceship based on tilt

if accelerometer.get_x() < -200:

ship_x = max(0, ship_x - 1) # Move left

elif accelerometer.get_x() > 200:

ship_x = min(4, ship_x + 1) # Move right

draw_ship(ship_x)

# Add new asteroid occasionally

if random.randint(0, 4) == 0:

asteroids.append(new_asteroid())

# Move asteroids down

for asteroid in asteroids:

asteroid[1] += 1

if asteroid[1] < 5:

display.set_pixel(asteroid[0], asteroid[1], 5) # Asteroid brightness

# Check for collisions

for asteroid in asteroids:

if asteroid[1] == 4 and asteroid[0] == ship_x:

display.show(Image.SKULL)

sleep(1000)

display.scroll("Score: " + str(score))

# Reset game

ship_x = 2

score = 0

asteroids = []

break

# Remove asteroids that have left the screen

asteroids = [a for a in asteroids if a[1] < 5]

score += 1 # Increase score over time

sleep(300) # Control game speed

has all the comments, just copy and paste. then you wanna plug it in to the irl one. shake it left or right. ur objective is to not hit the falling asteroids. if u do, you lose your points and it gives a skull emoji. you keep getting points.