iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >python写一个简单的俄罗斯方块
  • 851
分享到

python写一个简单的俄罗斯方块

俄罗斯方块简单python 2023-01-31 03:01:51 851人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

# teris.py# A module for game teris.# By programmer FYJfrom tkinter import *from time import sleepfrom random import *f

# teris.py
# A module for game teris.
# By programmer FYJ

from tkinter import *
from time import sleep
from random import *
from tkinter import messagebox


class Teris:
def __init__(self):
#方块颜色列表
self.color = ['red','orange','yellow','purple','blue','green','pink']
# Set a core squre and any shape can be drawn by the relative location.
#字典 存储形状对应7种形状 元组存储坐标
self.shapeDict = {1:[(0,0),(0,-1),(0,-2),(0,1)], # shape I
2:[(0,0),(0,-1),(1,-1),(1,0)], # shape O
3:[(0,0),(-1,0),(0,-1),(1,0)], # shape T T型
4:[(0,0),(0,-1),(1,0),(2,0)], # shape J 右长倒L盖子
5:[(0,0),(0,-1),(-1,0),(-2,0)], # shape L
6:[(0,0),(0,-1),(-1,-1),(1,0)], # shape Z
7:[(0,0),(-1,0),(0,-1),(1,-1)]} # shape S
#旋转坐标控制
self.rotateDict = {(0,0):(0,0),(0,1):(-1,0),(0,2):(-2,0),(0,-1):(1,0),
(0,-2):(2,0),(1,0):(0,1),(2,0):(0,2),(-1,0):(0,-1),
(-2,0):(0,-2),(1,1):(-1,1),(-1,1):(-1,-1),
(-1,-1):(1,-1),(1,-1):(1,1)}
# 初始高度,宽度 核心块位置
self.coreLocation = [4,-2]
self.height,self.width = 20,10
self.size = 32
# Map can record the location of every square.i宽 j高
self.map = {}
#全部置0
for i in range(self.width):
for j in range(-4,self.height):
self.map[(i,j)] = 0
#添加边界
for i in range(-4,self.width+4):
self.map[(i,self.height)] = 1
for j in range(-4,self.height+4):
for i in range(-4,0):
self.map[(i,j)] = 1
for j in range(-4,self.height+4):
for i in range(self.width,self.width+4):
self.map[(i,j)] = 1

# 初始化分数0 默认不加快 按下时加快
self.score = 0
self.isFaster = False
# 创建GUI界面
self.root = Tk()
self.root.title("Teris")
self.root.geometry("500x645")
self.area = canvas(self.root,width=320,height=640,bg='white')
self.area.grid(row=2)
self.pauseBut = Button(self.root,text="Pause",height=2,width=13,font=(18),command=self.isPause)
self.pauseBut.place(x=340,y=100)
self.startBut = Button(self.root,text="Start",height=2,width=13,font=(18),command=self.play)
self.startBut.place(x=340,y=20)
self.restartBut = Button(self.root,text="Restart",height=2,width=13,font=(18),command=self.isRestart)
self.restartBut.place(x=340,y=180)
self.quitBut = Button(self.root,text="Quit",height=2,width=13,font=(18),command=self.isQuit)
self.quitBut.place(x=340,y=260)
self.scoreLabel1 = Label(self.root,text="Score:",font=(24))
self.scoreLabel1.place(x=340,y=600)
self.scoreLabel2 = Label(self.root,text="0",fg='red',font=(24))
self.scoreLabel2.place(x=410,y=600)
#按键交互
self.area.bind("<Up>",self.rotate)
self.area.bind("<Left>",self.moveLeft)
self.area.bind("<Right>",self.moveRight)
self.area.bind("<Down>",self.moveFaster)
self.area.bind("<Key-w>",self.rotate)
self.area.bind("<Key-a>",self.moveLeft)
self.area.bind("<Key-d>",self.moveRight)
self.area.bind("<Key-s>",self.moveFaster)
self.area.focus_set()
#菜单
self.menu = Menu(self.root)
self.root.config(menu=self.menu)
self.startMenu = Menu(self.menu)
self.menu.add_cascade(label='Start',menu=self.startMenu)
self.startMenu.add_command(label='New Game',command=self.isRestart)
self.startMenu.add_separator()
self.startMenu.add_command(label='Continue',command=self.play)
self.exitMenu = Menu(self.menu)
self.menu.add_cascade(label='Exit',command=self.isQuit)
self.helpMenu = Menu(self.root)
self.menu.add_cascade(label='Help',menu=self.helpMenu)
self.helpMenu.add_command(label='How to play',command=self.rule)
self.helpMenu.add_separator()
self.helpMenu.add_command(label='About...',command=self.about)

#先将核心块的所在位置在map中的元素设为1,通过self.shapeDict获取其余方块位置,将map中对应元素设为1。
def getLocation(self):
map[(core[0],core[1])] = 1
for i in range(4):
map[((core[0]+getNew[i][0]),
(core[1]+getNew[i][1]))]=1

#判断方块下移一格后对应位置map中的元素是否为一,是,则不可移动,返回False;否,可以移动,返回True。
def canMove(self):
for i in range(4):
if map[(core[0]+getNew[i][0]),(core[1]+1+getNew[i][1])] == 1:
return False
return True

# 先用randRange获取1~7中的随机整数,随机到某一整数,那么访问self.shapeDict,获取这种形状方块的核心块及其他方块的相对位置。
# 访问颜色字典,获取此方块的颜色。建立循环,当方块可移动时(while self. canMove():),且暂停键未被摁下(if isPause:),
# 核心块纵坐标加一,根据核心块及其他方块对于核心块的相对位置,画出四个方块。用self.getLocation()函数获取方块的位置。
def drawNew(self):
global next
global getNew
global core
next = randrange(1,8)
#形状
self.getNew = self.shapeDict[next]
getNew = self.getNew
core = [4,-2]
time = 0.2
while self.canMove():
if isPause:
core[1] += 1
self.drawSquare()
if self.isFaster:
sleep(time-0.15)
else:
sleep(time+0.22)
self.isFaster = False
else:
self.drawSquare()
sleep(time)
self.getLocation()

# 绘制当前方块
def drawSquare(self):
self.area.delete("new")
for i in range(4):
self.area.create_rectangle((core[0]+self.getNew[i][0])*self.size,
(core[1]+self.getNew[i][1])*self.size,
(core[0]+self.getNew[i][0]+1)*self.size,
(core[1]+self.getNew[i][1]+1)*self.size,
fill=self.color[next-1],tags="new")
self.area.update()

# 给底部每行中方块都加上标签:bottom + str(j), j代表该块所在行数,每次遍历map,建立对于range(self. height)的for循环,删去每一行,
# 若map什么地方的元素为1,画出这一位置的方块,不断更新。这样可以画出底部方块。
def drawBottom(self):
for j in range(self.height):
self.area.delete('bottom'+str(j))
for i in range(self.width):
if map[(i,j)] == 1:
self.area.create_rectangle(self.size*i,self.size*j,self.size*(i+1),
self.size*(j+1),fill='grey',tags='bottom'+str(j))
self.area.update()
# 判断填满遍历map每一行的各个元素,若所有元素为1,则标签中score值+10,将
# 此行所有元素改为0,行数map(i,j)=map(i-1,j)(即所有之上的行下移)
# ,那么后续画底部方块时,可实现消行。
def isFill(self):
for j in range(self.height):
t = 0
for i in range(self.width):
if map[(i,j)] == 1:
t = t + 1
if t == self.width:
self.getScore()
self.deleteLine(j)

# 加分
def getScore(self):
scoreValue = eval(self.scoreLabel2['text'])
scoreValue += 10
self.scoreLabel2.config(text=str(scoreValue))

# 消行
def deleteLine(self,j):
for t in range(j,2,-1):
for i in range(self.width):
map[(i,t)] = map[(i,t-1)]
for i in range(self.width):
map[(i,0)] = 0
self.drawBottom()

# 遍历每一行,若从顶部到底部map每一行都有某一个元素或更多元素为1,
# 那么说明方块以顶到最上端,游戏结束。此处不可以简单判定最上一行是否有元素为1就判定结束,
# 若这样会产生顶部有新的方块产生,然后导致顶部有元素为1,误判为游戏结束。
def isOver(self):
t = 0
for j in range(self.height):
for i in range(self.width):
if self.map[(i,j)] == 1:
t += 1
break
if t >= self.height:
return False
else:
return True

# 先判断方块是否可以旋转(针对其靠近边界时)。先将其现在所在位置对应map中的元素改为0,判断其旋
# 转后位置对应map中的元素是否有一,若有,说明其旋转后的位置已经被占,是不能旋转的,返回值为False
# 。否则为可旋转,返回值True。若已判定可以旋转,那么访问self.rotateDict,得出旋转以后所有小块的位置
# 变换,将变换以后的位置对应map的元素设为1,旋转便已完成。
def canRotate(self):
for i in range(4):
map[((core[0]+getNew[i][0]),
(core[1]+getNew[i][1]))] = 0
for i in range(4):
if map[((core[0]+self.rotateDict[getNew[i]][0]),
(core[1]+self.rotateDict[getNew[i]][1]))] == 1:
return False
return True

#旋转
def rotate(self,event):
if next != 2:
if self.canRotate():
for i in range(4):
getNew[i] = self.rotateDict[getNew[i]]
self.drawSquare()
if not self.canMove():
for i in range(4):
map[((core[0]+getNew[i][0]),(core[1]+getNew[i][1]))] = 1

# 先判断是否左移/右移,同样,将方块现在所处位置的map中元素设为0,看其移动后的位置上map的元素是否有1,
# 若有,说明这一位置已被占据或已到边界,不可移动,返回False。若可移动,返回True。按下左键,若可
# 以移动,核心块的横坐标减1,由于我们只讨论其他小块对于核心块的相对位置,所以其他小块的位置自动随
# 核心块的位置移动而移动。将移动过后的位置对应map中的元素设为1。
def canLeft(self):
coreNow = core
for i in range(4):
map[((coreNow[0]+getNew[i][0]),(coreNow[1]+getNew[i][1]))] = 0
for i in range(4):
if map[((coreNow[0]+getNew[i][0]-1),(coreNow[1]+getNew[i][1]))] == 1:
return False
return True

#左移
def moveLeft(self,event):
if self.canLeft():
core[0] -= 1
self.drawSquare()
self.drawBottom()
if not self.canMove():
for i in range(4):
map[((core[0]+getNew[i][0]),(core[1]+getNew[i][1]))] = 1

# 判断右移
def canRight(self):
for i in range(4):
map[((core[0]+getNew[i][0]),(core[1]+getNew[i][1]))] = 0
for i in range(4):
if map[((core[0]+getNew[i][0]+1),(core[1]+getNew[i][1]))] == 1:
return False
return True

# 右移
def moveRight(self,event):
if self.canRight():
core[0] += 1
self.drawSquare()
self.drawBottom()
if not self.canMove():
for i in range(4):
map[((core[0]+getNew[i][0]),(core[1]+getNew[i][1]))] = 1

# 初始化中有一self. isFaster 的变量被设为False,当其为False时,
# 程序中的sleep(time)中time的值为0.35,而按下下键,self. isFaster变为True,
# time变成0.05,通过调整sleep()中变量的大小可以调节方块运动的速度。
# 此功能通过if语句实现。
def moveFaster(self,event):
self.isFaster = True
if not self.canMove():
for i in range(4):
map[((core[0]+getNew[i][0]),(core[1]+getNew[i][1]))] = 1
# run the programe
def run(self):
self.isFill()
self.drawNew()
self.drawBottom()

# play the game
def play(self):
self.startBut.config(state=DISABLED)
global isPause
isPause = True
global map
map = self.map
while True:
if self.isOver():
self.run()
else:
break
self.over()

# 重新开始游戏
def restart(self):
self.core = [4,-2]
self.map = {}
for i in range(self.width):
for j in range(-4,self.height):
self.map[(i,j)] = 0
for i in range(-1,self.width):
self.map[(i,self.height)] = 1
for j in range(-4,self.height+1):
self.map[(-1,j)] = 1
self.map[(self.width,j)] = 1
self.score = 0
self.t = 0.07
for j in range(self.height):
self.area.delete('bottom'+str(j))
self.play()

# 结束后告诉用户失败
def over(self):
feedback =messagebox.askquestion("You Lose!","You Lose!\nDo you want to restart?")
if feedback == 'yes':
self.restart()
else:
self.root.destroy()

# 退出
def isQuit(self):
askQuit =messagebox.askquestion("Quit","Are you sure to quit?")
if askQuit == 'yes':
self.root.destroy()
exit()

#判断是否按下restart
def isRestart(self):
askRestart =messagebox.askquestion("Restart","Are you sure to restart?")
if askRestart == 'yes':
self.restart()
else:
return

# 每次一按下暂停键,isPause = not isPause,当isPause = True时,由于之前提到过的if isPause:语句,
# 方块可以移动,游戏运行。当按下暂停键以后,isPause值为False,方块将不可移动。同时,isPause值为False时
# ,暂停键变为开始键,即标签由Pause 改为 Resume,当isPause值为True时,Resume改为Pause。这一功能由if语句实现。
def isPause(self):
global isPause
isPause=not isPause
if not isPause:
self.pauseBut["text"]="Resume"
else:
self.pauseBut["text"]="Pause"
#帮助
def rule(self):
ruleTop = Toplevel()
ruleTop.title('Help')
ruleTop.geometry('800x400')
rule ="Start: Press the start button or choose the option 'Continue' to start the game.\n%-s%-s%-s%-s%-s%-s%-s%-s%-s%-s%-s%-s%-s%-s"%("Restart: Press the restart button or choose the option 'New Game' to resatrt the game.\n", "Enjoy the Teris game! Have fun!")
ruleLabel = Label(ruleTop,text=rule,fg='blue',font=(18))
ruleLabel.place(x=50,y=50)

# 显示有关信息
def about(self):
aboutTop = Toplevel()
aboutTop.title('About')
aboutTop.geometry('300x150')
about = "Teris.py\n\
By Programmer Lee\n\
All Rights Reserved."
aboutLabel = Label(aboutTop,font=('Curier',20),fg='darkblue',text=about)
aboutLabel.pack()

# Get into mainloop
def mainloop(self):
self.root.mainloop()


# TerisPlay.py
# Game Teris
# By programmer FYJ

from teris import *

def main():
teris = Teris()
teris.mainloop()
main()



运行结构如图所示:


--结束END--

本文标题: python写一个简单的俄罗斯方块

本文链接: https://www.lsjlt.com/news/187194.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python写一个简单的俄罗斯方块
    # teris.py# A module for game teris.# By programmer FYJfrom tkinter import *from time import sleepfrom random import *f...
    99+
    2023-01-31
    俄罗斯方块 简单 python
  • Python写的俄罗斯方块
    在公司实习。公司推崇Python和Django框架,所以也得跟着学点。 简单瞅了下Tkinter,和Canvas配合在一起,还算是简洁的界面开发API。threading.Thread创建新的线程,其多线程机制也算是方便。 只是canva...
    99+
    2023-01-31
    俄罗斯方块 Python
  • python实现简单的俄罗斯方块
    本文实例为大家分享了python实现简单的俄罗斯方块的具体代码,供大家参考,具体内容如下 1. 案例介绍 俄罗斯方块是由 4 个小方块组成不同形状的板块,随机从屏幕上方落下,按方向键...
    99+
    2024-04-02
  • python实现简单俄罗斯方块游戏
    本文实例为大家分享了python实现简单俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 import pygame,sys,random,time all_block = [[...
    99+
    2024-04-02
  • java实现简单的俄罗斯方块
    本文实例为大家分享了java实现简单俄罗斯方块的具体代码,供大家参考,具体内容如下 结合网上的资料刚做完课程设计,具体代码如下: public class TetrisPanel e...
    99+
    2024-04-02
  • Python实现简单的俄罗斯方块游戏
    本文实例为大家分享了Python实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 玩法:童年经典,普通模式没啥意思,小时候我们都是玩加速的。 源码分享: import o...
    99+
    2024-04-02
  • 如何使用Html5写一个简单的俄罗斯方块小游戏
    小编给大家分享一下如何使用Html5写一个简单的俄罗斯方块小游戏,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!游戏效果:制作思路因为书里的俄罗斯方块比较普通,太常...
    99+
    2023-06-09
  • python是怎么实现简单的俄罗斯方块
    本篇文章为大家展示了python是怎么实现简单的俄罗斯方块,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Python主要用来做什么Python主要应用于:1、Web开发;2、数据科学研究;3、网络爬...
    99+
    2023-06-26
  • python制作俄罗斯方块
    python制作俄罗斯方块 简介 俄罗斯方块》(Tetris, 俄文:Тетрис)是一款由俄罗斯人阿列克谢·帕基特诺夫于1984年6月发明的休闲游戏。 该游戏曾经被多家公司代理过。经过多轮诉讼后,该...
    99+
    2023-09-17
    python 开发语言
  • Java实现俄罗斯方块游戏简单版
    本文实例为大家分享了Java实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 游戏页面效果如下: 俄罗斯方块游戏本身的逻辑: 俄罗斯方块游戏的逻辑是比较简单的。它就类似于堆砌...
    99+
    2024-04-02
  • python是怎么实现简单俄罗斯方块游戏
    本篇文章为大家展示了python是怎么实现简单俄罗斯方块游戏,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。Python的优点有哪些1、简单易用,与C/C++、Java、C# 等传统语言相比,Pyth...
    99+
    2023-06-26
  • 利用Java编写一个俄罗斯方块小游戏
    这期内容当中小编将会给大家带来有关利用Java编写一个俄罗斯方块小游戏,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。Java游戏俄罗斯方块的实现实例    &nbs...
    99+
    2023-05-31
    java ava
  • Java怎样实现俄罗斯方块游戏简单版
    这篇文章给大家介绍Java怎样实现俄罗斯方块游戏简单版,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。游戏页面效果如下:俄罗斯方块游戏本身的逻辑:俄罗斯方块游戏的逻辑是比较简单的。它就类似于堆砌房子一样,各种各样的方地形...
    99+
    2023-06-26
  • 如何利用vue3实现一个俄罗斯方块
    目录前言游戏相关设置游戏界面设置存储还在移动的俄罗斯方块信息存储已经不能移动的俄罗斯方块信息使用之前在贪吃蛇中使用的颜色渲染工具让方块移动起来(不考虑切换方块的形态切换)检测移动的俄...
    99+
    2024-04-02
  • 使用canvas怎么实现一个俄罗斯方块
    本篇文章给大家分享的是有关使用canvas怎么实现一个俄罗斯方块,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。界面的实现整个面板就是以左上角(0,0)为原点的坐标系,右上角(1...
    99+
    2023-06-09
  • 使用Python怎么控制台输出一个俄罗斯方块
    这篇文章给大家介绍使用Python怎么控制台输出一个俄罗斯方块,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。俄罗斯方块-打印功能:输入字母,打印俄罗斯方块的*图形# 尽可能吧俄罗斯方块放在中间Tetris&n...
    99+
    2023-06-14
  • 使用python怎么制作一个俄罗斯方块小游戏
    这期内容当中小编将会给大家带来有关使用python怎么制作一个俄罗斯方块小游戏,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。python可以做什么Python是一种编程语言,内置了许多有效的工具,Pyth...
    99+
    2023-06-14
  • Java实现俄罗斯方块的代码怎么写
    本文小编为大家详细介绍“Java实现俄罗斯方块的代码怎么写”,内容详细,步骤清晰,细节处理妥当,希望这篇“Java实现俄罗斯方块的代码怎么写”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。具体实现代码: ...
    99+
    2023-06-30
  • python练手--自制俄罗斯方块(文末
    小白python学习中,刚把面向对象弄了个大概,打算找个项目练练手,于是决定做一个俄罗斯方块吧!然后到现在一个月就过去了。。。。。 期间接触了一下pygame,参考了目光博客的Pygame教程,当时感觉看懂了,等到用的时候,哈哈哈,感觉把...
    99+
    2023-01-31
    俄罗斯方块 python
  • python基于pygame实现俄罗斯方块的方法
    小编给大家分享一下python基于pygame实现俄罗斯方块的方法,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!一、简单说明90后的小伙伴都玩过“俄罗斯方块”,那种“叱咤风云”场景 偶尔闪现在脑海 真的是太爽了;如果没有来...
    99+
    2023-06-06
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作