Recommended Posts

I present my latest MOC - it contains one part :innocent:

Try it yourself by pasting this code into the Mindstorms app (see if you can get 100 points):

Spoiler

from mindstorms import MSHub, App
from mindstorms.control import wait_for_seconds, wait_until, Timer
from mindstorms.operator import greater_than, greater_than_or_equal_to, less_than, less_than_or_equal_to, equal_to, not_equal_to
from random import randint
import math


hub = MSHub()


def reset():
    global snake
    global direction
    global fruit_x
    global fruit_y
    global step_time
    global score
    global points

    # Reset variables
    snake = [23,24] # X/Y positions of snake segments, from head to tail (i.e. head is at 2,3)
    direction = "up"
    fruit_x = 2
    fruit_y = 0
    step_time = 0.3
    score = 0
    points = 1

    # Countdown

    hub.light_matrix.show_image("SNAKE")
    hub.speaker.beep(70, 0.01)
    wait_for_seconds(0.5)

    hub.light_matrix.show_image("SNAKE", 80)
    hub.speaker.beep(70, 0.01)
    wait_for_seconds(0.5)

    hub.light_matrix.show_image("SNAKE", 60)
    hub.speaker.beep(70, 0.05)
    wait_for_seconds(0.5)

    for i in range(5):
        for j in range(5):
            hub.light_matrix.set_pixel(i, j, 50)

    # Reset snake and fruit
    hub.light_matrix.set_pixel(2, 3, 100)
    hub.light_matrix.set_pixel(2, 4, 100)
    hub.light_matrix.set_pixel(2, 0, 80)

    # Nullify any button presses made before game
    if hub.left_button.was_pressed():
        direction == "up"
    if hub.right_button.was_pressed():
        direction == "up"


def game_over():
    hub.speaker.beep(60, 0.5)
    wait_for_seconds(1)
    hub.light_matrix.write(score)
    wait_for_seconds(2)

    # Bonus for 100+ score
    if score > 99:
        hub.light_matrix.show_image('HAPPY')
        hub.speaker.beep(68, 0.01)
        wait_for_seconds(0.1)
        hub.speaker.beep(76, 0.01)
        wait_for_seconds(0.1)
        hub.speaker.beep(92, 0.01)
        wait_for_seconds(4)

    reset()


def step():
    global direction
    global points

    # Pause
    wait_for_seconds(step_time/2)

    # Reduce the amount of points that fruit is worth
    if points > 1:
        points -= 1

    # Turn left
    if hub.left_button.was_pressed():
        if direction == "up":
            direction = "left"
        elif direction == "left":
            direction = "down"
        elif direction == "down":
            direction = "right"
        elif direction == "right":
            direction = "up"
    
    # Turn right
    if hub.right_button.was_pressed():
        if direction == "up":
            direction = "right"
        elif direction == "left":
            direction = "up"
        elif direction == "down":
            direction = "left"
        elif direction == "right":
            direction = "down"
    
    # Logic for moving
    if direction == "up":
        move = -1
    if direction == "left":
        move = -10
    if direction == "down":
        move = 1
    if direction == "right":
        move = 10

    # Pause here for better button timing
    wait_for_seconds(step_time/2)

    # Insert new head
    snake.insert(0, snake[0] + move)
        
    # Check if head is out of bounds or hitting body
    if snake[0] < 0 or snake[0] > 44 or (snake[0] % 10) > 4 or (snake[0] in snake[1:-1]):
        game_over()
        return
    
    # Snake on fruit
    if snake[0] == fruit_x * 10 + fruit_y:
        eat()
    else:
        hub.light_matrix.set_pixel(int(snake[len(snake) - 1] / 10), snake[len(snake) - 1] % 10, 50)
        snake.pop()
    
    # Show new head
    hub.light_matrix.set_pixel(int(snake[0]/10), snake[0] % 10, 100)


def eat():
    global fruit_x
    global fruit_y
    global step_time
    global score
    global points

    hub.speaker.beep(60, 0.05)
    step_time -= 0.01
    score = score + points
    points = 10

    if len(snake) == 25:
        game_over()

    # Put a new fruit somewhere that's not in the snake
    while fruit_x * 10 + fruit_y in snake:
        fruit_x = randint(0, 4)
        fruit_y = randint(0, 4)
    hub.light_matrix.set_pixel(fruit_x, fruit_y, 80)



# Program
reset()
while True:
    step()

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.