How to play a turtle graphics game using joystick on a raspberry pi?

96 Views Asked by At

I am trying to use a joystick as a controller for my snake game using a Raspberry Pi. The problem is that my analog signal reader is blocking the code for the game itself. When I load in the game the snake does not move, the analog reader for the movement seems to be not very accurate and glitchy at the same time.

import RPi.GPIO as GPIO
import time
from ADCDevice import *
from turtle import Screen
from snake import Snake
from food import Food
from scoreboard import Scoreboard
import time

Z_Pin = 12      # define Z_Pin
adc = ADCDevice() # Define an ADCDevice class object

def setup():
    global adc
    screen = Screen()
    screen.setup(width=600, height=600)
    screen.bgcolor("black")
    screen.title("My Snake Game")
    screen.tracer(0)

    snake = Snake()
    food = Food()
    scoreboard = Scoreboard()

    if(adc.detectI2C(0x48)): # Detect the pcf8591.
        adc = PCF8591()
    elif(adc.detectI2C(0x4b)): # Detect the ads7830
        adc = ADS7830()
    else:
        print("No correct I2C address found, \n"
        "Please use command 'i2cdetect -y 1' to check the I2C address! \n"
        "Program Exit. \n");
        exit(-1)
    GPIO.setmode(GPIO.BOARD)        
    GPIO.setup(Z_Pin,GPIO.IN,GPIO.PUD_UP)   # set Z_Pin to pull-up mode
def loop():
    game_is_on = True
    while game_is_on:   
        snake = Snake()  
        screen = Screen()
        food = Food()
        scoreboard = Scoreboard()
        val_Z = GPIO.input(Z_Pin)       # read digital value of axis Z
        val_Y = adc.analogRead(0)           # read analog value of axis X and Y
        val_X = adc.analogRead(1)
        
        if val_Y > 124 and val_X == 128 or 127:
            print("up")
            snake.up
        elif val_Y < 124 and val_X == 128 or 127:
            print("down")
            snake.down
        elif val_X > 128 or 127 and val_Y == 124 or 125:
            print("right")
            snake.right
        elif val_X < 128 or 127 and val_Y == 124 or 125:
            print("left")
            snake.left
        
        time.sleep(0.01)
        screen.update()
        time.sleep(0.1)
        snake.move()
        # Detect collision with food.
        if snake.head.distance(food) < 15:
            food.refresh()
            snake.extend()
            scoreboard.increase()

        # Detect collision with wall.
        if snake.head.xcor() > 280 or snake.head.xcor() < -280 or snake.head.ycor() > 280 or snake.head.ycor() < -280:
            game_is_on = False
            scoreboard.game_over()

        # Detect collision with tail.
        for segment in snake.segments:
            if segment == snake.head:
                pass
            elif snake.head.distance(segment) < 10:
                game_is_on = False
                scoreboard.game_over()
    screen.exitonclick()

def destroy():
    adc.close()
    GPIO.cleanup()
    
if __name__ == '__main__':
    print ('Program is starting ... ') # Program entrance
    setup()
    try:
        loop()
    except KeyboardInterrupt: # Press ctrl-c to end the program.
        destroy()

0

There are 0 best solutions below