当前位置: 首页 > news >正文

python贪吃蛇毕业设计_如何用Python写一个贪吃蛇AI

2013-10-26 回答

以前在远标写过:from tkinter import *

import tkmessagebox,sys

from random import randint

class grid(object):

def __init__(self,master=none,window_width=800,window_height=600,grid_width=50,offset=10):

self.height = window_height

self.width = window_width

self.grid_width = grid_width

self.offset = offset

self.grid_x = self.width/self.grid_width

self.grid_y = self.height/self.grid_width

self.bg = "#ebebeb"

self.canvas = canvas(master, width=self.width+2*self.offset, height=self.height+2*self.offset, bg=self.bg)

self.canvas.pack()

self.grid_list()

def draw(self, pos, color,):

x = pos[0]*self.grid_width + self.offset

y = pos[1]*self.grid_width + self.offset

self.canvas.create_rectangle(x, y, x+self.grid_width, y+self.grid_width,fill=color,outline=self.bg)

def grid_list(self):

grid_list = []

for y in range(0,self.grid_y):

for x in range(0,self.grid_x):

grid_list.append((x,y))

self.grid_list = grid_list

class food(object):

def __init__(self, grid):

self.grid = grid

self.color = "#23d978"

self.set_pos()

def set_pos(self):

x = randint(0,self.grid.grid_x - 1)

y = randint(0,self.grid.grid_y - 1)

self.pos = (x, y)

def display(self):

self.grid.draw(self.pos,self.color)

class snake(object):

def __init__(self, grid):

self.grid = grid

self.body = [(10,6),(10,7),(10,8)]

self.direction = "up"

self.status = ['run','stop']

self.speed = 300

self.color = "#5fa8d9"

self.food = food(self.grid)

self.display_food()

self.gameover = false

self.score = 0

def available_grid(self):

return [i for i in self.grid.grid_list if i not in self.body[2:]]

def change_direction(self, direction):

self.direction = direction

def display(self):

for (x,y) in self.body:

self.grid.draw((x,y),self.color)

def display_food(self):

while(self.food.pos in self.body):

self.food.set_pos()

self.food.display()

def move(self):

head = self.body[0]

if self.direction == 'up':

new = (head[0], head[1]-1)

elif self.direction == 'down':

new = (head[0], head[1]+1)

elif self.direction == 'left':

new = (head[0]-1,head[1])

else:

new = (head[0]+1,head[1])

if not self.food.pos == head:

pop = self.body.pop()

self.grid.draw(pop,self.grid.bg)

else:

self.display_food()

self.score += 1

self.body.insert(0,new)

if not new in self.available_grid():

self.status.reverse()

self.gameover = true

else:

self.grid.draw(new,color=self.color)

class snakegame(frame):

def __init__(self,master=none, *args, **kwargs):

frame.__init__(self, master)

self.master = master

self.grid = grid(master=master,*args, **kwargs)

self.snake = snake(self.grid)

self.bind_all("", self.key_release)

self.snake.display()

def run(self):

if not self.snake.status[0] == 'stop':

self.snake.move()

if self.snake.gameover == true:

message = tkmessagebox.showinfo("game over", "your score: %d" % self.snake.score)

if message == 'ok':

sys.exit()

self.after(self.snake.speed,self.run)

def key_release(self, event):

key = event.keysym

key_dict = {"up":"down","down":"up","left":"right","right":"left"}

if key_dict.has_key(key) and not key == key_dict[self.snake.direction]:

self.snake.change_direction(key)

self.snake.move()

elif key == 'p':

self.snake.status.reverse()

if __name__ == '__main__':

root = tk()

snakegame = snakegame(root)

snakegame.run()

snakegame.mainloop()

相关文章:

  • active mq topic消费后删除_面试官杠上消息队列?高可用、重复消费、丢失、顺序消息你懂吗?...
  • 天气预报c是什么意思_昨天“大雪”天气,对明年气候有什么影响?
  • 当退出python时是否释放全部内存_Python跑循环时内存泄露的解决方法
  • 为什么parsefloat加出来还是字符串_为什么股票资金流出了1000万,却还是封住了涨停板?知道套路的我眼泪都掉出来了...
  • java web项目github_3月份Github上“最热门”的十大开源项目,竟被Java承包了!
  • python协程实现一万并发_求你别再花大价钱学 Python 之协程高并发爬虫
  • 什么是python编程例子_什么是Python编程的逻辑判断?
  • python读取odb_python - 从.odb文件中提取von mises应力值 - 堆栈内存溢出
  • sqlserver union执行后变慢_Zabbix如何监控SQL Server服务状态
  • 事件总线第一次点击_干货Spring Cloud Bus 消息总线介绍
  • cgi web 调用多次启动_漏洞预警|Web系统管理工具Webmin远程命令执行高危漏洞分析(CVE201915107)...
  • flashplayer离线安装包 64位_离线安装NET Framework 3.5的一般方法
  • node 获取表单数据 为空_Python数据结构(二)单向循环链表
  • javascript案例大全_JavaScript 类型 — 重学 JavaScript
  • ajax如何提交多表单的值_Ajax完整详细教程(一)
  • 【Leetcode】101. 对称二叉树
  • 【399天】跃迁之路——程序员高效学习方法论探索系列(实验阶段156-2018.03.11)...
  • Apache Zeppelin在Apache Trafodion上的可视化
  • magento2项目上线注意事项
  • Redux 中间件分析
  • session共享问题解决方案
  • vue和cordova项目整合打包,并实现vue调用android的相机的demo
  • 闭包--闭包之tab栏切换(四)
  • 力扣(LeetCode)21
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 智能合约Solidity教程-事件和日志(一)
  • FaaS 的简单实践
  • 从如何停掉 Promise 链说起
  • 国内开源镜像站点
  • 直播平台建设千万不要忘记流媒体服务器的存在 ...
  • ​Java并发新构件之Exchanger
  • (07)Hive——窗口函数详解
  • (C#)获取字符编码的类
  • (办公)springboot配置aop处理请求.
  • (博弈 sg入门)kiki's game -- hdu -- 2147
  • (动手学习深度学习)第13章 计算机视觉---图像增广与微调
  • (附源码)ssm学生管理系统 毕业设计 141543
  • (剑指Offer)面试题34:丑数
  • (力扣)循环队列的实现与详解(C语言)
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • .equals()到底是什么意思?
  • .gitignore文件设置了忽略但不生效
  • .net core 客户端缓存、服务器端响应缓存、服务器内存缓存
  • .NET中使用Redis (二)
  • .so文件(linux系统)
  • /etc/fstab和/etc/mtab的区别
  • /var/log/cvslog 太大
  • @基于大模型的旅游路线推荐方案
  • [ 蓝桥杯Web真题 ]-布局切换
  • [2544]最短路 (两种算法)(HDU)
  • [AAuto]给百宝箱增加娱乐功能
  • [bzoj1912]异象石(set)
  • [bzoj2957]楼房重建
  • [C/C++]_[初级]_[关于编译时出现有符号-无符号不匹配的警告-sizeof使用注意事项]