python写贪吃蛇

 源码:

import random
import sys
import pygame
'''
玩法:
w s a d 上下左右移动
'''
# 初始化
pygame.init()
# 全局变量
clock = pygame.time.Clock()
is_running = True
bg_color = (0, 130, 0)
head_color = (0, 0, 0)
speed_x = 0
speed_y = -1
food_color = (250, 0, 0)
body_color = (0, 0, 255)
dead = False
# 窗口宽高
W, H = 600, 600
# 行数列数
ROW, COL = 30, 30
# 创建窗口
size = (W, H)
window = pygame.display.set_mode(size)
# 窗口名字
pygame.display.set_caption('贪吃蛇')
# 设置背景颜色
window.fill(bg_color)


# 格子行列,(x, y) 表示格子在x行y列
class Point:
    x = 0
    y = 0

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def copy(self):
        return Point(x=self.x, y=self.y)


# 画格子的方法
def cell(point, color):
    # 格子宽 高
    cell_w = W/COL
    cell_h = H/ROW
    # 格子坐标
    cell_x = point.y * cell_w
    cell_y = point.x * cell_h
    pygame.draw.rect(window, color, (cell_x, cell_y, cell_w, cell_h))


# 蛇头
head = Point(15, 15)
# 食物
food = Point(random.randint(0, 29), random.randint(0, 29))
# 蛇身
bodies = [
    Point(head.x+1, head.y),
    Point(head.x+2, head.y),
    Point(head.x+3, head.y)
]
# 循环
while is_running:
    # 事件检测
    for event in pygame.event.get():
        # 退出
        if event.type == pygame.QUIT:
            pygame.quit()
            is_running = False
            sys.exit()
        # 键盘按下事件
        if event.type == pygame.KEYDOWN:
            # 向上w
            if event.key == 119 and speed_y != 1:
                speed_x = 0
                speed_y = -1
            # 向下s
            if event.key == 115 and speed_y != -1:
                speed_x = 0
                speed_y = 1
            # 向左a
            if event.key == 97 and speed_x != 1:
                speed_y = 0
                speed_x = -1
            # 向右d
            if event.key == 100 and speed_x != -1:
                speed_y = 0
                speed_x = 1
    # 蛇头移动前,处理蛇身
    bodies.insert(0, head.copy())
    bodies.pop()
    # 蛇头移动,蛇头的行数x变就是y方向的速度增加
    head.x += speed_y
    head.y += speed_x
    # 清空痕迹
    window.fill(bg_color)
    # 碰撞检测
    if head.x < 0 or head.y < 0 or head.x > 29 or head.y > 29:   # 撞墙
        dead = True
    for body in bodies:
        if head.x == body.x and head.y == body.y:   # 撞自己
            dead = True
        # 食物与蛇不能重合
        if body.x == food.x and body.y == food.y:
            food = Point(random.randint(0, 29), random.randint(0, 29))
    # 吃
    if head.x == food.x and head.y == food.y:
        bodies.append(food)
        # 重置食物位置
        food = Point(random.randint(0, 29), random.randint(0, 29))
    # 游戏结束
    if dead:
        pygame.quit()
        sys.exit()
    # 画蛇头
    cell(head, head_color)
    # 画食物
    cell(food, food_color)
    # 画蛇身
    for body in bodies:
        cell(body, body_color)
    # 刷新
    pygame.display.update()
    clock.tick(15)


if __name__=='__main__':
    pass

效果图:

 这应该是较简单的写法了。

偏面向过程。

核心思路:

1.将窗口划分为若干个格子,用坐标表示每个格子的位置,定义画格子的方法

2.蛇身跟随蛇头移动:蛇头移动前把蛇头当前坐标添加到蛇身第一个位置,并移除蛇身的最后一个元素

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值