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 = [
Point(row=head.row, clo=head.clo + 1),
Point(row=head.row, clo=head.clo + 2),
Point(row=head.row, clo=head.clo + 3)
]
# 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
if head.row == position.row and head.clo == position.clo:
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
head_color = (0, 158, 128)
# 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
eat = (head.row == snakeFood.row and head.clo == snakeFood.clo)
# 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))
snake.insert(0, head.copy())
if not eat:
snake.pop()
# Move the snake
if direct == ‘left’:
head.clo -= 1
if direct == ‘right’:
head.clo += 1
if direct == ‘top’:
head.row -= 1
if direct == ‘bottom’:
head.row += 1
dead = False
if head.clo < 0 or head.row < 0 or head.clo >= CLO or head.row >= ROW:
dead = True
for body in snake:
if head.clo == body.clo and head.row == body.row:
dead = True
break
if dead:
print(‘Game Over’)
pygame.quit()
exit()
quit = False
# Draw the background
pygame.draw.rect(window, (20, 10, 10), (0, 0, width, hight))
# Snake head
rect(head, head_color)
# 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()