In this project, you can use the joystick and keys of CyberPi to control the snake.
import pygamefrom sys import exitimport randomimport timeimport cyberpiclass Point():def __init__(self, row, clo):self.row = rowself.clo = clodef copy(self):return Point(row=self.row, clo=self.clo)# Initialize the settingspygame.init()width = 800hight = 400ROW = 30CLO = 50direct = 'left'window = pygame.display.set_mode((width, hight))pygame.display.set_caption('Greedy Snake')# Set the head of the snake in the middlehead = Point(row=int(ROW / 2), clo=int(CLO / 2))# Initialize the number of cells in the body of the snakesnake = [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 snakedef gen_food():while 1:position = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))is_coll = Falseif head.row == position.row and head.clo == position.clo:is_coll = Truefor body in snake:if body.row == position.row and body.clo == position.clo:is_coll = Truebreakif not is_coll:breakreturn position# Define coordinates# Define the color of the snake headhead_color = (0, 158, 128)# Coordinates of foodsnakeFood = gen_food()# Color of foodsnakeFood_color = (255, 255, 0)snake_color = (200, 0, 18)# Define a function to execute multiple drawing stepsdef rect(point, color):# Set the left and top positions for drawingleft = point.clo * width / CLOtop = point.row * hight / ROW# Color the squarespygame.draw.rect(window, color, (left, top, width / CLO, hight / ROW))quit = True# Set the frame rateclock = pygame.time.Clock()while quit:# Lock the frame rateclock.tick(30)# Control through the joystick of CyberPiif 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 keysfor event in pygame.event.get():if event.type == pygame.QUIT:quit = Falseelif 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 foodeat = (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 snakeif 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 snakeif direct == 'left':head.clo -= 1if direct == 'right':head.clo += 1if direct == 'top':head.row -= 1if direct == 'bottom':head.row += 1dead = Falseif head.clo < 0 or head.row < 0 or head.clo >= CLO or head.row >= ROW:dead = Truefor body in snake:if head.clo == body.clo and head.row == body.row:dead = Truebreakif dead:print('Game Over')pygame.quit()exit()quit = False# Draw the backgroundpygame.draw.rect(window, (20, 10, 10), (0, 0, width, hight))# Snake headrect(head, head_color)# Draw foodrect(snakeFood, snakeFood_color)# Draw the body of the snakefor body in snake:rect(body, snake_color)# Stop controlling through CyberPipygame.display.flip()time.sleep(0.05)pygame.quit()exit()
