其他IDE應該不會
不是很懂
以下是一個簡單的貪食蛇遊戲的 Python 代碼:
pythonimport pygame
import random
# 初始化 Pygame
pygame.init()
# 定義常數
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
SNAKE_SIZE = 10
SNAKE_SPEED = 15
SNAKE_COLOR = (0, 255, 0)
FOOD_COLOR = (255, 0, 0)
# 創建視窗
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 設置標題
pygame.display.set_caption('貪食蛇')
# 創建蛇
snake = [[200, 200], [210, 200], [220, 200]]
snake_direction = 'RIGHT'
# 創建食物
food_position = [random.randrange(1, (SCREEN_WIDTH//10)) * 10,
random.randrange(1, (SCREEN_HEIGHT//10)) * 10]
# 定義函數
def draw_snake(snake):
for pos in snake:
pygame.draw.rect(screen, SNAKE_COLOR, pygame.Rect(pos[0], pos[1], SNAKE_SIZE, SNAKE_SIZE))
def move_snake(snake, snake_direction):
if snake_direction == 'RIGHT':
new_pos = [snake[0][0] + SNAKE_SIZE, snake[0][1]]
elif snake_direction == 'LEFT':
new_pos = [snake[0][0] - SNAKE_SIZE, snake[0][1]]
elif snake_direction == 'UP':
new_pos = [snake[0][0], snake[0][1] - SNAKE_SIZE]
elif snake_direction == 'DOWN':
new_pos = [snake[0][0], snake[0][1] + SNAKE_SIZE]
snake.insert(0, new_pos)
snake.pop()
def draw_food(food_position):
pygame.draw.rect(screen, FOOD_COLOR, pygame.Rect(food_position[0],
food_position[1],
SNAKE_SIZE,
SNAKE_SIZE))
def check_collision(snake, food_position):
if snake[0] == food_position:
return True
return False
def check_game_over(snake):
if snake[0][0] < 0 or snake[0][0] > SCREEN_WIDTH-SNAKE_SIZE:
return True
elif snake[0][1] < 0 or snake[0][1] > SCREEN_HEIGHT-SNAKE_SIZE:
return True
for block in snake[1:]:
if snake[0] == block:
return True
return False
# 遊戲迴圈
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT and snake_direction != 'LEFT':
snake_direction = 'RIGHT'
elif event.key == pygame.K_LEFT and snake_direction != 'RIGHT':
snake_direction = 'LEFT'
elif event.key == pygame.K_UP and snake_direction != 'DOWN':
snake_direction = 'UP'
elif event.key == pygame.K_DOWN and snake_direction != 'UP':
snake_direction = 'DOWN'
screen.fill((0, 0, 0))
# 移動蛇
move_snake(snake, snake_direction)
import pygameimport random
# 初始化 Pygamepygame.init()
# 定義常數SCREEN_WIDTH = 600SCREEN_HEIGHT = 600SNAKE_SIZE = 10SNAKE_SPEED = 15SNAKE_COLOR = (0, 255, 0)FOOD_COLOR = (255, 0, 0)
# 創建視窗screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
# 設置標題pygame.display.set_caption('貪食蛇')
# 創建蛇snake = [[200, 200], [210, 200], [220, 200]]snake_direction = 'RIGHT'
# 創建食物food_position = [random.randrange(1, (SCREEN_WIDTH//10)) * 10, random.randrange(1, (SCREEN_HEIGHT//10)) * 10]
# 定義函數def draw_snake(snake): for pos in snake: pygame.draw.rect(screen, SNAKE_COLOR, pygame.Rect(pos[0], pos[1], SNAKE_SIZE, SNAKE_SIZE))
def move_snake(snake, snake_direction): if snake_direction == 'RIGHT': new_pos = [snake[0][0] + SNAKE_SIZE, snake[0][1]] elif snake_direction == 'LEFT': new_pos = [snake[0][0] - SNAKE_SIZE, snake[0][1]] elif snake_direction == 'UP': new_pos = [snake[0][0], snake[0][1] - SNAKE_SIZE] elif snake_direction == 'DOWN': new_pos = [snake[0][0], snake[0][1] + SNAKE_SIZE] snake.insert(0, new_pos) snake.pop()
def draw_food(food_position): pygame.draw.rect(screen, FOOD_COLOR, pygame.Rect(food_position[0], food_position[1], SNAKE_SIZE, SNAKE_SIZE))
def check_collision(snake, food_position): if snake[0] == food_position: return True return False
def check_game_over(snake): if snake[0][0] < 0 or snake[0][0] > SCREEN_WIDTH-SNAKE_SIZE: return True elif snake[0][1] < 0 or snake[0][1] > SCREEN_HEIGHT-SNAKE_SIZE: return True for block in snake[1:]: if snake[0] == block: return True return False
# 遊戲迴圈while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_RIGHT and snake_direction != 'LEFT': snake_direction = 'RIGHT' elif event.key == pygame.K_LEFT and snake_direction != 'RIGHT': snake_direction = 'LEFT' elif event.key == pygame.K_UP and snake_direction != 'DOWN': snake_direction = 'UP' elif event.key == pygame.K_DOWN and snake_direction != 'UP': snake_direction = 'DOWN'
screen.fill((0, 0, 0))
# 移動蛇 move_snake(snake, snake_direction)