zoukankan      html  css  js  c++  java
  • 经典方块游戏-俄罗斯方块

    1、俄罗斯方块类定义

      1 # -*- coding: utf-8 -*-
      2 
      3 from base import Base
      4 from point import Point
      5 from utils import *
      6 import random
      7 
      8 class Tetris(Base):
      9     __SHAPE_NUM = 7
     10     __FORM_NUM_PER_SHAPE = 4
     11     __SHAPE_S, __SHAPE_Z, __SHAPE_L, __SHAPE_J, __SHAPE_I, __SHAPE_O, __SHAPE_T = 0,1,2,3,4,5,6
     12     __FORM_A, __FORM_B, __FORM_C, __FORM_D = 0,1,2,3
     13     __SHIFT_TAB = (
     14         (
     15             ( (-1, -1), (0, -1), (0, 0), (1, 0) ), 
     16             ( (-1, 1), (-1, 0), (0, 0), (0, -1) ), 
     17             ( (-1, -1), (0, -1), (0, 0), (1, 0) ), 
     18             ( (-1, 1), (-1, 0), (0, 0), (0, -1) )
     19         ), # S
     20         (
     21             ( (1, -1), (0, -1), (0, 0), (-1, 0) ), 
     22             ( (1, 1), (1, 0), (0, 0), (0, -1) ), 
     23             ( (1, -1), (0, -1), (0, 0), (-1, 0) ), 
     24             ( (1, 1), (1, 0), (0, 0), (0, -1) )
     25         ), # Z
     26         (
     27             ( (1, -1), (0, -1), (0, 0), (0, 1) ), 
     28             ( (-1, -1), (-1, 0), (0, 0), (1, 0) ), 
     29             ( (-1, 1), (0, 1), (0, 0), (0, -1) ), 
     30             ( (1, 1), (1, 0), (0, 0), (-1, 0) )
     31         ), # L
     32         (
     33             ( (-1, -1), (0, -1), (0, 0), (0, 1) ), 
     34             ( (-1, 1), (-1, 0), (0, 0), (1, 0) ), 
     35             ( (1, 1), (0, 1), (0, 0), (0, -1) ), 
     36             ( (1, -1), (1, 0), (0, 0), (-1, 0) )
     37         ), # J
     38         (
     39             ( (0, 2), (0, 1), (0, 0), (0, -1) ), 
     40             ( (2, 0), (1, 0), (0, 0), (-1, 0) ), 
     41             ( (0, 2), (0, 1), (0, 0), (0, -1) ), 
     42             ( (2, 0), (1, 0), (0, 0), (-1, 0) )
     43         ), # I
     44         (
     45             ( (1, -1), (0, -1), (0, 0), (1, 0) ), 
     46             ( (1, -1), (0, -1), (0, 0), (1, 0) ), 
     47             ( (1, -1), (0, -1), (0, 0), (1, 0) ), 
     48             ( (1, -1), (0, -1), (0, 0), (1, 0) )
     49         ), # O
     50         (
     51             ( (0, -1), (-1, 0), (0, 0), (1, 0) ), 
     52             ( (0, -1), (-1, 0), (0, 0), (0, 1) ), 
     53             ( (0, 1), (-1, 0), (0, 0), (1, 0) ), 
     54             ( (0, -1), (1, 0), (0, 0), (0, 1) )
     55         ) # T
     56     )
     57     
     58     def __init__(self):
     59         Base.__init__(self)
     60         self._point = self._get_start_point()
     61         self._shape = self._get_shape()
     62         self._form = self._get_form()
     63         self._next_shape = self._get_shape()
     64         self._form = self._get_form()
     65     
     66     def _get_shape(self):
     67         return random.randint(0, self.__SHAPE_NUM - 1)
     68     
     69     def _get_form(self):
     70         return random.randint(0, self.__FORM_NUM_PER_SHAPE - 1)
     71         
     72     def _get_start_point(self):
     73         base_x, base_y = int(Base.width() / 2), Base.height()
     74         
     75         if (self._shape == self.__SHAPE_L and self.__form == self.__FORM_D) or 
     76            (self._shape == self.__SHAPE_J and self.__form == self.__FORM_B) or 
     77            (self._shape == self.__SHAPE_T and self.__form == self.__FORM_C) or 
     78            (self._shape == self.__SHAPE_I and (self.__form == self.__FORM_B or self.__form == self.__FORM_B)):
     79             return Point(base_x, base_y)
     80         return Point(base_x, base_y + 1)
     81     
     82     def _get_shape_points(self):
     83         return ( Point(self._point.x + shift_x, self._point.y + shift_y) for shift_x,shift_y in self.__SHIFT_TAB[self._shape][self._form] )
     84         
     85     def _fall(self):
     86         if self._status != Status.Running:
     87             return
     88             
     89         self._clear_shape_in_area()
     90         self._point.y -= 1
     91         if self._check_shape():
     92             
     93             return
     94         else:
     95             
     96         
     97     def _check_shape(self):
     98         shape_points = self._get_shape_points()
     99         for point in shape_points:
    100             if not Base.check_point_in_area(point):
    101                 return False
    102             if self._area[point.x][point.y] != Block.Empty:
    103                 return False
    104         return True
    105     
    106     def _clear_shape_in_area(self):
    107         shape_points = self._get_shape_points()
    108         for point in shape_points:
    109             if Base.check_point_in_area(point):
    110                 self._area[point.x][point.y] = Block.Empty
    111     
    112     def _set_shape_in_area(self):
    113         shape_points = self._get_shape_points()
    114         for point in shape_points:
    115             if Base.check_point_in_area(point):
    116                 self._area[point.x][point.y] = Block.Fill
    117     
    118     def _clear_rows(self):
    119         
    120     
    121 if __name__ == '__main__':
    122     tetris = Tetris()
    123     print(tetris)
    tetris.py
  • 相关阅读:
    ubuntu国内镜像源
    windows安装Pygraphviz
    python dict与collections.defaultdict的区别
    python生成 requirements.txt文件
    python list 和 dict前加星号
    Ubuntu安装Docker
    Zookeeper核心概念及读写流程
    docker安装mysql5和mysql8
    ubuntu docker更改默认镜像和容器存储位置
    训练篇-胸
  • 原文地址:https://www.cnblogs.com/rmthy/p/8206094.html
Copyright © 2011-2022 走看看