python-贪吃蛇
0.工具-PyCharm
1.这里面含有文件和数据库简单的操作,感兴趣的可以作为练手小游戏。
import turtle
import copy
import random
from turtle import *
from time import sleep
from random import randrange
from tkinter.messagebox import *
from tkinter import *
import os
import sqlite3
con = sqlite3.connect(r"E:\pythonSource\ment.db")
# con.execute("create table if not exists men(name primary key,score1)")
# con.execute("create table men(name primary key,scores)")
snake = [[0, 0], [0, 10], [0, 20]]
# 定义蛇的身体大小,这里定义为三个格子
aim = [0, 10]
# 定义移动的方向,默认往正方向移动
food = [-10, 0]
# 设置食物坐标
scores = 0
number = 0
num = [0]
maxcores = 0
def nan():
global maxcores
global scores
# score1 = random.randrange(100, 200)
# name = random.choice('abcdefghijklmnopqrstuvwxyz0123456789')
name = random.randrange(1, 10000000000)
con = sqlite3.connect(r"E:\pythonSource\ment.db")
con.execute("insert into men(name, scores) values(?,?)", (name, scores))
cur = con.execute("select name, scores from men")
for row in cur:
num.append(row[1])
maxcores = max(num)
con.commit()
cur.close()
con.close()
def change_direction(x, y):
# 定义改变方向
aim[0] = x
aim[1] = y
def square(x, y, size, color):
# 定义单个方块
turtle.fillcolor('green')
turtle.penup()
# 抬起画笔
turtle.goto(x, y)
# 初始坐标
turtle.begin_fill()
# 渲染
turtle.pendown()
# 落下画笔
for i in range(4):
turtle.forward(size)
# 画笔每一次画的长度
turtle.left(90)
# 每一次都旋转90度
turtle.end_fill()
def inside(head):
# 定义边界大小
return -250 < head[0] < 250 and -250 < head[1] < 250
def snake_move():
# 定义蛇的移动
global scores
head = copy.deepcopy(snake[-1])
# 头部深拷贝
head = [head[0] + aim[0], head[1] + aim[1]]
# 头部向前移动
if head in snake or not inside(head):
# 失败的条件,吃到自己,或者超出边界值
nan()
r2 = askokcancel(title='提示', message='游戏失败,再来一局?')
if not r2:
sys.exit()
else:
restart_program()
square(head[0], head[1], 10, "red")
return
if head == food:
# 产生食物坐标
food[0] = randrange(-15, 15)*10
food[1] = randrange(-15, 15)*10
scores += 500
core()
else:
snake.pop(0)
# mostcore()
# 尾部去除一个方块
snake.append(head)
# 头部添加一个方块
turtle.clear()
# 删除之前的动画
if head == food:
mostcore()
square(food[0], food[1], 10, "green")
# 设置食物的方块
for body in snake:
# 重新加载蛇的身体
square(body[0], body[1], 10, "black")
turtle.update()
# 更新,不会太突然
turtle.ontimer(snake_move, 75)
# 循环,让蛇可以一直移动,每三百毫秒执行一次这个函数,可以设置难度等级
def ui_view():
r1 = askquestion(title='启动游戏', message='是否开始游戏?')
if r1 == 'no':
sys.exit()
def restart_program():
python = sys.executable
os.execl(python, python, * sys.argv)
showTalkPen = turtle.Turtle()
def core():
showTalkPen.penup()
showTalkPen.goto(-200, -200)
showTalkPen.pendown()
showTalkPen.fillcolor('green')
showTalkPen.hideturtle()
# 去掉箭头
showTalkPen.clear()
showTalkPen.write("Scores: " + str(scores), align='left', font=('Arial', 10, 'bold'))
showTalkPens = turtle.Turtle()
def mostcore():
global maxcores
nan()
showTalkPens.penup()
showTalkPens.goto(-200, -180)
showTalkPens.pendown()
showTalkPens.fillcolor('green')
showTalkPens.hideturtle()
# 去掉箭头
showTalkPens.clear()
showTalkPens.write("MaxScore: " + str(maxcores), align='left', font=('Arial', 10, 'bold'))
turtle.setup(500, 500)
# 屏幕宽度,定义边界值
turtle.bgpic("1.gif")
turtle.tracer(False)
# 去除动画效果
turtle.hideturtle()
# 去掉箭头
turtle.listen()
# 监听,设置前后左右按键
turtle.onkey(lambda: change_direction(0, 10), "Up")
turtle.onkey(lambda: change_direction(0, -10), "Down")
turtle.onkey(lambda: change_direction(-10, 0), "Left")
turtle.onkey(lambda: change_direction(10, 0), "Right")
ui_view()
core()
mostcore()
snake_move()
# 调用方法
turtle.done()
# 不让屏幕立马关闭
3.实验结果
(1)启动界面,选择是择开始游戏,选择取消择退出游戏
(2)点击是进入游戏界面,随机生成方块,可以看见有分数和最高分,每次吃到一个方块加500分,并且能够记录每次的最高分,最高分也会相应的显示在上面。
(3)蛇后退设置成吃到自己,当超出边界,也就是撞墙,游戏将会结束,当分数超过当前的最高分,那么在下次游戏开始最高分将会更新。
(4)这里我们点击确定,将会新的开始。
(5)当我们超过最高分再次启动游戏就能看见更新数据