python版贪吃蛇,复制可直接运行

"""贪吃蛇"""

import random
import sys
import time
import pygame
from pygame.locals import *
from collections import deque

SCREEN_WIDTH = 800  # 屏幕宽度
SCREEN_HEIGHT = 600  # 屏幕高度
SIZE = 20  # 小方格大小
LINE_WIDTH = 1  # 网格线宽度

# 游戏区域的坐标范围
SCOPE_X = (0, SCREEN_WIDTH // SIZE - 1)
SCOPE_Y = (2, SCREEN_HEIGHT // SIZE - 1)

# 食物的分值及颜色
FOOD_STYLE_LIST = [(10, (255, 100, 100)), (20, (100, 255, 100)), (30, (100, 100, 255))]

LIGHT = (100, 100, 100)
DARK = (200, 200, 200)  # 蛇的颜色
BLACK = (0, 0, 0)  # 网格线颜色
RED = (200, 30, 30)  # 红色,GAME OVER 的字体颜色
BGCOLOR = (40, 40, 60)  # 背景色


def print_text(screen, font, x, y, text, fcolor=(255, 0, 0)):
    imgText = font.render(text, True, fcolor)
    screen.blit(imgText, (x, y))


# 初始化蛇
def init_snake():
    snake = deque()
    snake.append((2, SCOPE_Y[0]))
    snake.append((1, SCOPE_Y[0]))
    snake.append((0, SCOPE_Y[0]))
    return snake


def create_food(snake):
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的Python贪吃蛇游戏代码,希望对你有所帮助: ```python import pygame import random # 初始化Pygame pygame.init() # 定义颜色 black = pygame.Color(0, 0, 0) white = pygame.Color(255, 255, 255) red = pygame.Color(255, 0, 0) green = pygame.Color(0, 255, 0) blue = pygame.Color(0, 0, 255) # 设置游戏窗口大小和标题 screen_width = 640 screen_height = 480 pygame.display.set_caption('贪吃蛇') screen = pygame.display.set_mode((screen_width, screen_height)) # 定义游戏帧率和时钟对象 fps = pygame.time.Clock() # 定义贪吃蛇的初始位置和大小 snake_position = [100, 50] snake_body = [[100, 50], [90, 50], [80, 50]] # 定义食物的初始位置 food_position = [random.randrange(1, screen_width // 10) * 10, random.randrange(1, screen_height // 10) * 10] # 定义食物的大小 food_size = [10, 10] # 定义初始方向 direction = 'RIGHT' change_direction = direction # 定义字体对象 font = pygame.font.SysFont('simhei', 18) def show_score(score): """显示得分""" score_text = font.render('得分: ' + str(score), True, white) screen.blit(score_text, [0, 0]) def game_over(): """游戏结束""" gameover_text = font.render('游戏结束!', True, white) gameover_rect = gameover_text.get_rect() gameover_rect.midtop = (screen_width / 2, screen_height / 4) screen.blit(gameover_text, gameover_rect) pygame.display.flip() pygame.time.delay(2000) pygame.quit() quit() # 开始游戏循环 score = 0 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: change_direction = 'RIGHT' elif event.key == pygame.K_LEFT: change_direction = 'LEFT' elif event.key == pygame.K_UP: change_direction = 'UP' elif event.key == pygame.K_DOWN: change_direction = 'DOWN' elif event.key == pygame.K_ESCAPE: pygame.quit() quit() # 判断是否改变方向 if change_direction == 'RIGHT' and not direction == 'LEFT': direction = 'RIGHT' elif change_direction == 'LEFT' and not direction == 'RIGHT': direction = 'LEFT' elif change_direction == 'UP' and not direction == 'DOWN': direction = 'UP' elif change_direction == 'DOWN' and not direction == 'UP': direction = 'DOWN' # 移动贪吃蛇 if direction == 'RIGHT': snake_position[0] += 10 elif direction == 'LEFT': snake_position[0] -= 10 elif direction == 'UP': snake_position[1] -= 10 elif direction == 'DOWN': snake_position[1] += 10 # 更新贪吃蛇的身体 snake_body.insert(0, list(snake_position)) if snake_position == food_position: food_position = [random.randrange(1, screen_width // 10) * 10, random.randrange(1, screen_height // 10) * 10] score += 10 else: snake_body.pop() # 绘制游戏界面 screen.fill(black) for position in snake_body: pygame.draw.rect(screen, green, pygame.Rect( position[0], position[1], 10, 10)) pygame.draw.rect(screen, red, pygame.Rect( food_position[0], food_position[1], food_size[0], food_size[1])) show_score(score) pygame.display.update() # 判断游戏是否结束 if snake_position[0] < 0 or snake_position[0] > screen_width - 10: game_over() elif snake_position[1] < 0 or snake_position[1] > screen_height - 10: game_over() for block in snake_body[1:]: if snake_position == block: game_over() # 控制游戏帧率 fps.tick(20) ``` 该代码使用Pygame库实现了一个简单的贪吃蛇游戏,主要包括游戏界面的绘制、键盘事件的处理、贪吃蛇的移动、得分的显示、游戏结束等功能。你可以直接代码复制Python环境中运行,体验游戏的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值