發表文章

目前顯示的是 6月, 2024的文章

陳暐丞Bro Code貪吃蛇python snake game

圖片
from tkinter import * import random GAME_WIDTH = 800 GAME_HEIGHT = 800 SPEED = 500 SPACE_SIZE = 50 BODY_PARTS = 3 SNAKE_COLOR = "yellow" FOOD_COLOR = "purple" BACKGROUND_COLOR = "white" class Snake: def __init__(self): self.body_size = BODY_PARTS self.coordinates = [] self.squares = [] for i in range(0, BODY_PARTS): self.coordinates.append([0, 0]) for x, y in self.coordinates: square = canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR, tag="snake") self.squares.append(square) class Food: def __init__(self): x = random.randint(0, (GAME_WIDTH / SPACE_SIZE)-1) * SPACE_SIZE y = random.randint(0, (GAME_HEIGHT / SPACE_SIZE) - 1) * SPACE_SIZE self.coordinates = [x, y] canvas.create_oval(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOR, tag="food") def next_turn(snake, food): x, y = ...