python实现俄罗斯方块游戏(改进版)

本文为大家分享了python实现俄罗斯方块游戏,继上一篇的改进版,供大家参考,具体内容如下

1.加了方块预览部分

2.加了开始按钮

在公司实习抽空写的,呵呵。觉得Python还不错,以前觉得像个玩具语言。希望能够用它做更多大事吧!!!加油。

截图如下:

代码如下:

#coding=utf-8
from Tkinter import *;
from random import *;
import thread;
from tkMessageBox import showinfo;
import threading;
from time import sleep;
class BrickGame(object):

 #是否开始
 start = True;
 #是否到达底部
 isDown = True;

 #窗体
 window = None;
 #frame
 frame1 = None;
 frame2 = None;

 #按钮
 btnStart = None;

 #绘图类
 canvas = None;
 canvas1 = None;

 #标题
 title = "BrickGame";
 #宽和高
 width = 450;
 height = 670;

 #行和列
 rows = 20;
 cols = 10;

 #下降方块的线程
 downThread = None;

 #几种方块
 brick = [

  [
     [
       [1,1,1],
       [0,0,1],
       [0,0,0]
     ],
     [
       [0,0,1],
       [0,0,1],
       [0,1,1]
     ],
     [
       [0,0,0],
       [1,0,0],
       [1,1,1]
     ],
     [
       [1,1,0],
       [1,0,0],
       [1,0,0]
     ]
  ],
  [
     [
        [0,0,0],
        [0,1,1],
        [0,1,1]
     ],
     [
        [0,0,0],
        [0,1,1],
        [0,1,1]
     ],
     [
        [0,0,0],
        [0,1,1],
        [0,1,1]
     ],
     [
        [0,0,0],
        [0,1,1],
        [0,1,1]
     ]
  ],
  [
     [
        [1,1,1],
        [0,1,0],
        [0,1,0]
     ],
     [
        [0,0,1],
        [1,1,1],
        [0,0,1]
     ],
     [
        [0,1,0],
        [0,1,0],
        [1,1,1]
     ],
     [
        [1,0,0],
        [1,1,1],
        [1,0,0]
     ]
  ],
  [
     [
        [0,1,0],
        [0,1,0],
        [0,1,0]
     ],
     [
        [0,0,0],
        [1,1,1],
        [0,0,0]
     ],
     [
        [0,1,0],
        [0,1,0],
        [0,1,0]
     ],
     [
        [0,0,0],
        [1,1,1],
        [0,0,0]
     ]
  ]
 ];

 #当前的方块
 curBrick = None;
 #当前方块数组
 arr = None;
 arr1 = None;
 #当前方块形状
 shape = -1;
 #当前方块的行和列(最左上角)
 curRow = -10;
 curCol = -10;

 #背景
 back = list();
 #格子
 gridBack = list();
 preBack = list();

 #初始化
 def init(self):

  for i in range(0,self.rows):

   self.back.insert(i,list());
   self.gridBack.insert(i,list());

  for i in range(0,self.rows):

   for j in range(0,self.cols):

    self.back[i].insert(j,0);
    self.gridBack[i].insert(j,self.canvas.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));

  for i in range(0,3):

   self.preBack.insert(i,list());

  for i in range(0,3):

   for j in range(0,3):

    self.preBack[i].insert(j,self.canvas1.create_rectangle(30*j,30*i,30*(j+1),30*(i+1),fill="black"));

 #绘制游戏的格子
 def drawRect(self):

  for i in range(0,self.rows):

     for j in range(0,self.cols):

      if self.back[i][j]==1:

       self.canvas.itemconfig(self.gridBack[i][j],fill="blue",outline="white");

      elif self.back[i][j]==0:

       self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white");

  #绘制预览方块
  for i in range(0,len(self.arr1)):

   for j in range(0,len(self.arr1[i])):

    if self.arr1[i][j]==0:

     self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white");

    elif self.arr1[i][j]==1:

     self.canvas1.itemconfig(self.preBack[i][j],fill="orange",outline="white");

  #绘制当前正在运动的方块
  if self.curRow!=-10 and self.curCol!=-10:

   for i in range(0,len(self.arr)):

    for j in range(0,len(self.arr[i])):

     if self.arr[i][j]==1:     

      self.canvas.itemconfig(self.gridBack[self.curRow+i][self.curCol+j],fill="blue",outline="white");

  #判断方块是否已经运动到达底部
  if self.isDown:

   for i in range(0,3):

    for j in range(0,3):

     if self.arr[i][j]!=0:

      self.back[self.curRow+i][self.curCol+j] = self.arr[i][j];

   #判断整行消除
   self.removeRow();

   #判断是否死了
   self.isDead();

   #获得下一个方块
   self.getCurBrick();

 #判断是否有整行需要消除
 def removeRow(self):

  for i in range(0,self.rows):

   tag1 = True;
   for j in range(0,self.cols):

    if self.back[i][j]==0:

     tag1 = False;
     break;

   if tag1==True:

    #从上向下挪动
    for m in xrange(i-1,0,-1):

     for n in range(0,self.cols):

      self.back[m+1][n] = self.back[m][n];

 #获得当前的方块
 def getCurBrick(self):

  self.curBrick = randint(0,len(self.brick)-1);
  self.shape = 0;
  #当前方块数组
  self.arr = self.brick[self.curBrick][self.shape];
  self.arr1 = self.arr;

  self.curRow = 0;
  self.curCol = 1;

  #是否到底部为False
  self.isDown = False;

 #监听键盘输入
 def onKeyboardEvent(self,event):

  #未开始,不必监听键盘输入
  if self.start == False:

   return;

  #记录原来的值
  tempCurCol = self.curCol;
  tempCurRow = self.curRow;
  tempShape = self.shape;
  tempArr = self.arr;
  direction = -1;

  if event.keycode==37:

   #左移
   self.curCol-=1;
   direction = 1;
  elif event.keycode==38:
   #变化方块的形状
   self.shape+=1;
   direction = 2;

   if self.shape>=4:

    self.shape=0;
   self.arr = self.brick[self.curBrick][self.shape];
  elif event.keycode==39:

   direction = 3;
   #右移
   self.curCol+=1;
  elif event.keycode==40:

   direction = 4;
   #下移
   self.curRow+=1;

  if self.isEdge(direction)==False:

   self.curCol = tempCurCol;
   self.curRow = tempCurRow;
   self.shape = tempShape;
   self.arr = tempArr;

  self.drawRect();

  return True;

 #判断当前方块是否到达边界
 def isEdge(self,direction):

  tag = True;

  #向左,判断边界
  if direction==1:

   for i in range(0,3):

    for j in range(0,3):

     if self.arr[j][i]!=0 and (self.curCol+i<0 or self.back[self.curRow+j][self.curCol+i]!=0):

      tag = False;
      break;
  #向右,判断边界
  elif direction==3:

   for i in range(0,3):

    for j in range(0,3):

     if self.arr[j][i]!=0 and (self.curCol+i>=self.cols or self.back[self.curRow+j][self.curCol+i]!=0):

      tag = False;
      break;
  #向下,判断底部
  elif direction==4:

   for i in range(0,3):

    for j in range(0,3):

     if self.arr[i][j]!=0 and (self.curRow+i>=self.rows or self.back[self.curRow+i][self.curCol+j]!=0):

      tag = False;
      self.isDown = True;
      break;
  #进行变形,判断边界
  elif direction==2:

   if self.curCol<0:

    self.curCol=0;

   if self.curCol+2>=self.cols:

    self.curCol = self.cols-3;

   if self.curRow+2>=self.rows:

    self.curRow = self.curRow-3;

  return tag;

 #方块向下移动
 def brickDown(self):

  while True:

   if self.start==False:

    print("exit thread");
    break;

   tempRow = self.curRow;
   self.curRow+=1;

   if self.isEdge(4)==False:

    self.curRow = tempRow;

   self.drawRect();

   #每一秒下降一格
   sleep(1); 

 #点击开始
 def clickStart(self):

  self.start = True;

  for i in range(0,self.rows):

   for j in range(0,self.cols):

    self.back[i][j] = 0;
    self.canvas.itemconfig(self.gridBack[i][j],fill="black",outline="white");

  for i in range(0,len(self.arr)):

   for j in range(0,len(self.arr[i])):

    self.canvas1.itemconfig(self.preBack[i][j],fill="black",outline="white");

  self.getCurBrick();
  self.drawRect();

  self.downThread = threading.Thread(target=self.brickDown,args=());
  self.downThread.start();  

 #判断是否死了
 def isDead(self):

  for j in range(0,len(self.back[0])):

   if self.back[0][j]!=0:

    showinfo("提示","你挂了,再来一盘吧!");
    self.start = False;
    break;

 #运行
 def __init__(self):

  self.window = Tk();
  self.window.title(self.title);
  self.window.minsize(self.width,self.height);
  self.window.maxsize(self.width,self.height);    

  self.frame1 = Frame(self.window,width=300,height=600,bg="black");
  self.frame1.place(x=20,y=30);

  self.frame2 = Frame(self.window,width=90,height=90,bg="black");
  self.frame2.place(x=340,y=60);

  self.canvas = Canvas(self.frame1,width=300,height=600,bg="black");
  self.canvas1 = Canvas(self.frame2,width=90,height=90,bg="black");

  self.btnStart = Button(self.window,text="开始",command=self.clickStart);
  self.btnStart.place(x=340,y=400,width=80,height=25);

  self.init();

  #获得当前的方块
  self.getCurBrick();

  #按照数组,绘制格子
  self.drawRect();  

  self.canvas.pack();
  self.canvas1.pack();

  #监听键盘事件
  self.window.bind("<KeyPress>",self.onKeyboardEvent); 

  #启动方块下落线程
  self.downThread = threading.Thread(target=self.brickDown,args=());
  self.downThread.start();  

  self.window.mainloop(); 

  self.start=False;

 pass;

if __name__=='__main__':

 brickGame = BrickGame();

更多俄罗斯方块精彩文章请点击专题:俄罗斯方块游戏集合 进行学习。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 用Python编写一个简单的俄罗斯方块游戏的教程

    俄罗斯方块游戏,使用Python实现,总共有350+行代码,实现了俄罗斯方块游戏的基本功能,同时会记录所花费时间,消去的总行数,所得的总分,还包括一个排行榜,可以查看最高记录. 排行榜中包含一系列的统计功能,如单位时间消去的行数,单位时间得分等. 附源码: from Tkinter import * from tkMessageBox import * import random import time #俄罗斯方块界面的高度 HEIGHT = 18 #俄罗斯方块界面的宽度 WIDTH = 10

  • python实现俄罗斯方块游戏

    在公司实习.公司推崇Python和Django框架,所以也得跟着学点. 简单瞅了下Tkinter,和Canvas配合在一起,还算是简洁的界面开发API.threading.Thread创建新的线程,其多线程机制也算是方便. 只是canvas.create_rectangle居然不是绘制矩形,而是新建了矩形控件这点让人大跌眼镜.先开始,在线程里每次都重绘多个矩形(随数组变化),其实是每次都新建了N个矩形,结果内存暴增.原来,对矩形进行变更时,只需用canvas.itemconfig即可. 下面就是

  • Python小游戏之300行代码实现俄罗斯方块

    前言 本文代码基于 python3.6 和 pygame1.9.4. 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块.但是想到旋转,停靠,消除等操作,感觉好像很难啊,等真正写完了发现,一共也就 300 行代码,并没有什么难的. 先来看一个游戏截图,有点丑,好吧,我没啥美术细胞,但是主体功能都实现了,可以玩起来. 现在来看一下实现的过程. 外形 俄罗斯方块整个界面分为两部分,一部分是左边的游戏区域,另一部分是右边的显示区域,显示得分.速度.下一个方块样式等.

  • python实现俄罗斯方块

    网上搜到一个Pygame写的俄罗斯方块(tetris),大部分看懂的前提下增加了注释,Fedora19下运行OK的 主程序: #coding:utf8 #! /usr/bin/env python # 注释说明:shape表示一个俄罗斯方块形状 cell表示一个小方块 import sys from random import choice import pygame from pygame.locals import * from block import O, I, S, Z, L, J,

  • python和pygame实现简单俄罗斯方块游戏

    本文为大家分享了python实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 Github:Tetris 代码: # -*- coding:utf-8 -*- import pygame, sys, random, copy from pygame.locals import * pygame.init() CubeWidth = 40 CubeHeight = 40 Column = 10 Row = 20 ScreenWidth = CubeWidth * (Column + 5) S

  • python实现简单俄罗斯方块

    本文实例为大家分享了python实现俄罗斯方块的具体代码,供大家参考,具体内容如下 # 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 =

  • python编写俄罗斯方块

    本文实例为大家分享了python实现俄罗斯方块的具体代码,供大家参考,具体内容如下 #coding=utf-8 from tkinter import * from random import * import threading from tkinter.messagebox import showinfo from tkinter.messagebox import askquestion import threading from time import sleep class Brick

  • Python使用pygame模块编写俄罗斯方块游戏的代码实例

    文章先介绍了关于俄罗斯方块游戏的几个术语. 边框--由10*20个空格组成,方块就落在这里面. 盒子--组成方块的其中小方块,是组成方块的基本单元. 方块--从边框顶掉下的东西,游戏者可以翻转和改变位置.每个方块由4个盒子组成. 形状--不同类型的方块.这里形状的名字被叫做T, S, Z ,J, L, I , O.如下图所示: 模版--用一个列表存放形状被翻转后的所有可能样式.全部存放在变量里,变量名字如S_SHAPE_TEMPLATE or J_SHAPE_TEMPLATE 着陆--当一个方块

  • python实现俄罗斯方块游戏(改进版)

    本文为大家分享了python实现俄罗斯方块游戏,继上一篇的改进版,供大家参考,具体内容如下 1.加了方块预览部分 2.加了开始按钮 在公司实习抽空写的,呵呵.觉得Python还不错,以前觉得像个玩具语言.希望能够用它做更多大事吧!!!加油. 截图如下: 代码如下: #coding=utf-8 from Tkinter import *; from random import *; import thread; from tkMessageBox import showinfo; import t

  • Python实现简单的俄罗斯方块游戏

    本文实例为大家分享了Python实现俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 玩法:童年经典,普通模式没啥意思,小时候我们都是玩加速的. 源码分享: import os import sys import random from modules import * from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * '''定义俄罗斯方块游戏类''' class TetrisG

  • python实现俄罗斯方块小游戏

    回顾我们的python制作小游戏之路,几篇非常精彩的文章 我们用python实现了坦克大战 python制作坦克大战 我们用python实现了飞船大战 python制作飞船大战 我们用python实现了两种不同的贪吃蛇游戏 200行python代码实现贪吃蛇游戏 150行代码实现贪吃蛇游戏 我们用python实现了扫雷游戏 python实现扫雷游戏 我们用python实现了五子棋游戏 python实现五子棋游戏 今天我们用python来实现小时候玩过的俄罗斯方块游戏吧 具体代码与文件可以访问我的

  • python实现简单俄罗斯方块游戏

    本文实例为大家分享了python实现简单俄罗斯方块游戏的具体代码,供大家参考,具体内容如下 import pygame,sys,random,time all_block = [[[0,0],[0,-1],[0,1],[0,2]],         [[0,0],[0,1],[1,1],[1,0]],         [[0,0],[0,-1],[-1,0],[-1,1]],          [[0,0],[0,1],[-1,-1],[-1,0]],           [[0,0],[0,1

  • Python完整实现俄罗斯方块游戏全解

    目录 1俄罗斯方块游戏 2Python代码实现 2.1展现 2.2Python代码 1 俄罗斯方块游戏 <俄罗斯方块>原本是前苏联科学家阿列克谢·帕基特诺夫所开发的教育用软件,之后开始提供授权给各个游戏公司,造成各平台上软件大量发行的现象. Game Boy版的俄罗斯方块在日本卖出424万套,是Game Boy史上卖最好的游戏.海湾战争时,也是前线美军最常拿消磨时间的游戏之一. 由于俄罗斯方块具有的数学性.动态性与知名度,也经常拿来作为游戏程序设计的练习题材. 2 Python代码实现 2.1

  • Python实现AI自动玩俄罗斯方块游戏

    目录 导语 正文 1)Tetris.py 2)Tetris_model.py​​ 3)Tetris_ai.py​​ 效果展示 导语 提到<俄罗斯方块>(Tetris),那真是几乎无人不知无人不晓.​ 其历史之悠久,可玩性之持久,能手轻轻一挥,吊打一大波游戏. 对于绝大多数小友而言,<俄罗斯方块>的规则根本无需多言——将形状不一的方块填满一行消除即可. 这款火了30几年的<俄罗斯方块>游戏之前就已经写过的哈,往期的Pygame合集里面可以找找看! 但今天木木子介绍的是&l

随机推荐