俄罗斯方块Python
# -*- coding: utf-8 -*-from tkinter import *
import random
#尚需改进,有待提高#各种方块的表示,与参考方块的相对位置
shapedic={1:((0,0),(1,0),(0,-1),(1,-1)),#正方形2:((0,0),(0,-1),(0,-2),(0,1)),#长条3:((0,0),(0,-1),(1,0),(1,1)),#之字型4:((0,0),(0,-1),(-1,0),(-1,1)),#反之字型5:((0,0),(1,0),(-1,0),(-1,-1)),#L型6:((0,0),(1,0),(-1,0),(1,-1)),#反L型7:((0,0),(1,0),(-1,0),(0,-1))#T型}#旋转函数,顺时针旋转90度,相对于参考方块
change_dic={(0,0):(0,0),(0,1):(-1,0),(-1,0):(0,-1),(0,-1):(1,0),(1,0):(0,1),(1,-1):(1,1),(1,1):(-1,1),(-1,1):(-1,-1),(-1,-1):(1,-1),(2,0):(0,2),(0,2):(-2,0),(-2,0):(0,-2),(0,-2):(2,0)}#随机颜色
colorDict = {0 :'#CCC0B4',1 :'#EEE4DA',2 : '#EDE0C8',3 : '#F2B179',4 : '#EC8D54',5 : '#F67C5F', 6 : '#EA5937',7 : '#804000', 8 : '#F1D04B',9 : '#E4C02A',10 : '#EE7600',11 : '#D5A500',12 : '#E4C02A',13 : '#804000',14 : '#EA5937',15 : '#EE7600',16 : '#776E65',17 : '#776E65',18 : '#FFFFFF',19 : 'yellow',20 : 'blue',21 : 'lightblue',22 : 'red'}#俄罗斯方块
class Game_Russia:def __init__(self):#每个方块的大小self.width=20#方块数目,长和宽self.row=28self.column=19# #初始化
# self.scores=0
# self.all_square={}#坐标系网格中个位置方块的存在性
# self.head_square=[]#参考方块绝对位置
# self.new_square=[]#移动方块相对位置
# self.direction=-1#方块初始方向
# #规定界限
# #i表示第i列,0在左边
# #j表示第j行,零在上面
# for j in range(-4,self.row):
# for i in range(self.column):
# self.all_square[i,j]=0
# #划界,开口向上
# for j in range(self.row+1):
# self.all_square[19,j]=1
# self.all_square[-1,j]=1
# for i in range(-1,self.column+1):
# self.all_square[i,28]=1"""用来debugfor j in range(self.row+1):for i in range(-1,self.column+1):print self.all_square[i,j],print"""self.window = Tk()self.window.geometry()self.window.maxsize(400,610)self.window.minsize(400,610)self.window.title(u"俄罗斯方块")self.frame1=Frame(self.window,bg="white",relief=GROOVE,borderwidth=5)self.frame2=Frame(self.window,bg="white",relief=RAISED,borderwidth=2,height=40,width=570)self.canvas=Canvas(self.frame1,bg='purple',width=400,height=570)self.score_label=Label(self.frame2,text="Score: 0")self.frame1.pack()self.frame2.pack(fill=BOTH)self.score_label.pack(side=LEFT)self.canvas.pack(fill=BOTH)self.draw_wall()self.initial()self.get_new_square()self.draw_new_square()self.play()self.window.mainloop()"=== View Part ==="#边界def draw_wall(self):self.canvas.create_line(5,5,385,5,fill='blue',width=1)self.canvas.create_line(385,5,385,565,fill='blue',width=1)self.canvas.create_line(5,5,5,565,fill='blue',width=1)self.canvas.create_line(5,565,385,565,fill='blue',width=1)#得分def draw_score(self):self.get_score()self.score_label.config(self.score_label,text="Score: "+str(self.scores))#画下面所有不动的方块def draw_square(self):color=colorDict[random.randint(0,len(colorDict)-1)]for j in range(self.row):self.canvas.delete("line"+str(j))for i in range(self.column):if self.all_square[i,j]:self.canvas.create_rectangle(5+i*self.width,5+j*self.width,5+(i+1)*self.width,5+(j+1)*self.width,fill=color,tags="line"+str(j))#画移动的方块def draw_new_square(self):self.canvas.delete("new")self.head_square[1]+=1color=colorDict[random.randint(0,len(colorDict)-1)]for i in range(4):self.canvas.create_rectangle(5+(self.head_square[0]+self.new_square[i][0])*self.width,5+(self.head_square[1]+self.new_square[i][1])*self.width,5+(self.head_square[0]+self.new_square[i][0]+1)*self.width,5+(self.head_square[1]+1+self.new_square[i][1])*self.width,fill=color,tags="new")"=== Model Part ==="def initial(self):#初始化self.scores=0self.all_square={}#坐标系网格中个位置方块的存在性self.head_square=[]#参考方块绝对位置self.new_square=[]#移动方块相对位置self.direction=-1#方块初始方向#规定界限#i表示第i列,0在左边#j表示第j行,零在上面for j in range(-4,self.row):for i in range(self.column):self.all_square[i,j]=0#划界,开口向上for j in range(self.row+1):self.all_square[19,j]=1self.all_square[-1,j]=1for i in range(-1,self.column+1):self.all_square[i,28]=1def is_dead(self):#判断死亡与否,最上方中间四个方块for i in {8,9,10,11}:if self.all_square[i,0]:return Trueelse:return Falsedef get_new_square(self):#获得新的方块,初始位置均为(9,-2)self.new=random.randrange(1,8)#随机方块#主方块(参考方块)的位置self.direction=random.randrange(4)self.head_square=[9,-2]self.new_square=list(shapedic[self.new])for i in range(self.direction):self.change()def delete_one_line(self,j):#得分后删除整行for t in range(j,2,-1):for i in range(self.column):self.all_square[i,t]=self.all_square[i,t-1]for i in range(self.column):self.all_square[i,0]=0def get_score(self):for j in range(self.row):for i in range(self.column):#判断某行是否全满if not self.all_square[i,j]:breakelse:self.scores+=10self.delete_one_line(j)#移动方块停止def get_seated(self):self.all_square[tuple(self.head_square)]=1for i in range(4):self.all_square[self.head_square[0]+self.new_square[i][0],self.head_square[1]+self.new_square[i][1]]=1#方块是否到了最底端 def is_seated(self):for i in range(4):if self.all_square[self.head_square[0]+self.new_square[i][0],self.head_square[1]+self.new_square[i][1]+1]:return Truereturn False"=== Control Part ==="#改变方块朝向#通过旋转改变,主方块不动def change(self):if self.new>1:for i in range(4):if self.all_square[self.head_square[0]+change_dic[self.new_square[i]][0],self.head_square[1]+change_dic[self.new_square[i]][1]]:returnelse:for i in range(4):self.new_square[i]=change_dic[self.new_square[i]]else:return#右移def right_move(self):#先判断是否可以移动for i in range(4):if self.all_square[self.head_square[0]+self.new_square[i][0]-1,self.head_square[1]+self.new_square[i][1]]:return Trueself.head_square[0]-=1#左移def left_move(self):for i in range(4):if self.all_square[self.head_square[0]+self.new_square[i][0]+1,self.head_square[1]+self.new_square[i][1]]:return Trueself.head_square[0]+=1#向下加速def down_quicker(self):while (not self.is_seated()):self.draw_new_square()self.canvas.after(50)self.canvas.update()#方向键控制def move(self,event):if event.keycode == 39:self.left_move() elif event.keycode == 38:self.change()elif event.keycode == 37:self.right_move()elif event.keycode == 40:self.down_quicker()else:pass#开始游戏def play(self):self.canvas.bind('<Key>',self.move)self.canvas.focus_set()while True:if self.is_dead():self.gameover()breakif self.is_seated():self.get_seated()self.get_new_square()self.draw_score()self.draw_square()self.draw_new_square()else:self.draw_new_square()self.canvas.after(500)self.canvas.update()#游戏结束def gameover(self):if askquestion("LOSE",u"你输了!\n重新开始吗?") == 'yes':return self.restart()else:return self.window.destroy()#重新开始 def restart(self):self.initial()self.draw_square()self.get_new_square()self.draw_new_square()self.play()#主程序
if __name__ == "__main__":Game_Russia()
俄罗斯方块Python相关推荐
- 俄罗斯方块python源码
先将俄罗斯方块源码贴出来,待以后有时间在详细写设计与思路. #_*_ coding:utf-8 _*_ ''' Created on 2017年7月30日上午11:16:44@author: xiao ...
- 学习python一开始枯燥_编程零基础应当怎样开始学python?他说,看这三个经典方法...
很多时候,想就是不如做. 与其想着怎样开始学python,不如赶紧找份python教程先看起来! 当然,这只是告诉你,想不如开始做! 下面,我针对python初学介绍一下可以借鉴的方法! 1.看书 这 ...
- python翻转棋_奥赛罗棋reverse
作者,持牌照消费金融模型经理,发明国家算法专利,国内大型医药数据中心担任过数据库负责人.和中科院,清华大学,百度,腾讯,爱奇艺等平台保持长期项目合作.擅长python 机器学习,应用于游戏,医疗,金融 ...
- Python 游戏:贪吃蛇
系列文章地址 Python 游戏:贪吃蛇 Python 游戏:扫雷 Python 游戏:300行代码实现俄罗斯方块 Python 游戏:五子棋之人机对战 文章目录 系列文章地址 一.游戏介绍 二.游戏 ...
- c++小项目_编程初学者的练手小项目(Pythonamp;C/C++)
老是有一些刚接触编程的小伙伴问,怎么样才能快速提高编程能力?"一切不写代码的学编程,都是瞎搞!",想要提高编程能力,当然是写代码.写代码.写代码.在不断做项目的过程中,加强基础语法 ...
- 关于本号,你想看的都在这里
2019.08.05 「Python 技术」公众号发出了第一篇文章,时至今日,已经快一年了,期间我们累积发文 260+ 篇,52 周除去周末我们基本做到了日更,这一路走来磕磕碰碰有太多的不容易,会有为 ...
- python编的俄罗斯方块游戏下载_python写的俄罗斯方块游戏
python写的俄罗斯方块游戏 功能包括:记录所花费时间;消去的总行数;总分;排行榜,最高记录查看等. 排行榜中包含一系列的统计功能,如单位时间消去的行数,单位时间得分等. from Tkinter ...
- AI玩俄罗斯方块(Python实现)
目录 1.环境 2.实现机制(Pierre Dellacherie算法) 3.代码实现 人工智能大火的今天,如果还是自己玩俄罗斯方块未免显得太LOW,为什么不对游戏升级,让机器自己去玩俄罗斯方块呢?有 ...
- python俄罗斯方块算法详解_用 Python 写一个俄罗斯方块游戏 (
@@ -2,34 +2,34 @@ > * 原文作者:[Dr Pommes](https://medium.com/@pommes) > * 译文出自:[掘金翻译计划](https://g ...
- python编程小游戏代码-Python小游戏之300行代码实现俄罗斯方块
前言 本文代码基于 python3.6 和 pygame1.9.4. 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块.但是想到旋转,停靠,消除等操作,感觉好像很 ...
最新文章
- 急我所需!机器学习、深度学习绘图模板.ppt
- matplotlib生成指定大小的空白的白色图(故意保存)实战:当然也可以保存正常的有内容的图像
- Python 绘图库 Matplotlib
- caxa画图怎么倒角_16个底部含圆弧倒角的宽槽编程案例
- java volatile关键字的作用_java volatile关键字作用及使用场景详解
- 关于前端开发,你真的了解吗?
- MySQL05:DCL语言、视图
- centos中创建快捷键pycharm
- 阿里财报揭秘:阿里巴巴最忙的人这一年都干了啥
- ADS2015安装包和教程
- 使用HttpClient和OkHttp实现模拟登录方正教务系统
- 静态链表(简单介绍)
- Liunx最全最常用的命令-初学者专属
- 两个cgi的莫名其妙的core dump问题的解决
- 【论文笔记】Combining EfficientNet and Vision Transformers for Video Deepfake Detection
- (Modern Family S01E01) Part 4 PhilClaire Luke射Alex / Haley邀请Dylan
- BUUCTF_Crypto_[MRCTF2020]天干地支+甲子
- 如何用Python操控手机APP攻略!建议收藏!很全面
- 九个值得一试的跨平台移动应用开发工具
- 5年内禁用支付宝和微信支付!多地公安出手:这些人摊上大事了
热门文章
- 安装opencv_contrib-3.4.9, fatal error: opencv2/xfeatures2d.hpp: 没有那个文件或目录. 解决方法
- 如何写好项目会议纪要?
- linux设置自动关机命令,Linux怎么用命令设置自动关机
- matplotlib 使用简明教程(三)-一些专业图表简介
- python snap7 plc_基于Snap7实现与西门子PLC通信(示例代码)
- SystemConfiguration 简介
- 自制题库答题考试软件小程序开发,把题库导入小程序里,javascript小程序
- 怎么将PDF文件在线转换成JPG图片
- 同时处理知网、万方、维普数据库——CiteSpace、Ucinet、Vosviewer等
- 如何Python写一个安卓APP