Greedy Snake (Live Mode, Using CyberPi as a Controller)

Greedy Snake (Live Mode, Using CyberPi as a Controller)

In this project, you can use the joystick and keys of CyberPi to control the snake.

import pygame

from sys import exit

import random

import time

import cyberpi

class Point():

def __init__(self, row, clo):

self.row = row

self.clo = clo

def copy(self):

return Point(row=self.row, clo=self.clo)

# Initialize the settings

pygame.init()

width = 800

hight = 400

ROW = 30

CLO = 50

direct = ‘left’

window = pygame.display.set_mode((width, hight))

pygame.display.set_caption(‘Greedy Snake’)

# Set the head of the snake in the middle

head = Point(row=int(ROW / 2), clo=int(CLO / 2))

# Initialize the number of cells in the body of the snake

snake = [

]

# Generate food outside of the body of the snake

def gen_food():

while 1:

position = Point(row=random.randint(0, ROW – 1), clo=random.randint(0, CLO – 1))

is_coll = False

is_coll = True

for body in snake:

if body.row == position.row and body.clo == position.clo:

is_coll = True

break

if not is_coll:

break

return position

# Define coordinates

# Define the color of the snake head

# Coordinates of food

snakeFood = gen_food()

# Color of food

snakeFood_color = (255, 255, 0)

snake_color = (200, 0, 18)

# Define a function to execute multiple drawing steps

def rect(point, color):

# Set the left and top positions for drawing

left = point.clo * width / CLO

top = point.row * hight / ROW

# Color the squares

pygame.draw.rect(window, color, (left, top, width / CLO, hight / ROW))

quit = True

# Set the frame rate

clock = pygame.time.Clock()

while quit:

# Lock the frame rate

clock.tick(30)

# Control through the joystick of CyberPi

if cyberpi.controller.is_press(“up”):

if direct == ‘left’ or direct == ‘right’:

direct = ‘top’

if cyberpi.controller.is_press(“down”):

if direct == ‘left’ or direct == ‘right’:

direct = ‘bottom’

if cyberpi.controller.is_press(“left”):

if direct == ‘top’ or direct == ‘bottom’:

direct = ‘left’

if cyberpi.controller.is_press(“right”):

if direct == ‘top’ or direct == ‘bottom’:

direct = ‘right’

# Control through the keys

for event in pygame.event.get():

if event.type == pygame.QUIT:

quit = False

elif event.type == pygame.KEYDOWN:

if event.key == 273 or event.key == 119:

if direct == ‘left’ or direct == ‘right’:

direct = ‘top’

if event.key == 274 or event.key == 115:

if direct == ‘left’ or direct == ‘right’:

direct = ‘bottom’

if event.key == 276 or event.key == 97:

if direct == ‘top’ or direct == ‘bottom’:

direct = ‘left’

if event.key == 275 or event.key == 100:

if direct == ‘top’ or direct == ‘bottom’:

direct = ‘right’

# Eat food

# Process the body of the snake

# 1.Insert the original snake head into the head of the snake

# 2.Delete the last snake

if eat:

snakeFood = Point(row=random.randint(0, ROW – 1), clo=random.randint(0, CLO – 1))

if not eat:

snake.pop()

# Move the snake

if direct == ‘left’:

if direct == ‘right’:

if direct == ‘top’:

if direct == ‘bottom’:

for body in snake:

break

print(‘Game Over’)

pygame.quit()

exit()

quit = False

# Draw the background

pygame.draw.rect(window, (20, 10, 10), (0, 0, width, hight))

# Draw food

rect(snakeFood, snakeFood_color)

# Draw the body of the snake

for body in snake:

rect(body, snake_color)

# Stop controlling through CyberPi

pygame.display.flip()

time.sleep(0.05)

pygame.quit()

exit()