经典方块游戏-基础
1、游戏中使用的 “点” 的定义
1 # -*- coding: utf-8 -*- 2 3 class Point: 4 5 def __init__(self, x=0, y=0): 6 self.x, self.y = x, y 7 8 def __eq__(self, another): 9 return isinstance(another, Point) and self.x == another.x and self.y == another.y 10 11 def __str__(self): 12 return 'Point(%d, %d)'%(self.x, self.y) 13 14 if __name__ == '__main__': 15 print('Point(1,2) == Point(2,1): {0}'.format(Point(1, 2) == Point(2, 1))) 16 print('Point(1,2) == Point(1,2): {0}'.format(Point(1, 2) == Point(1, 2)))
2、游戏中使用的一些枚举定义
1 # -*- coding: utf-8 -*- 2 3 from enum import Enum 4 5 Block = Enum('Block', ('Empty', 'Fill')) 6 7 Direct = Enum('Direct', ('Left', 'Down', 'Right', 'Up')) 8 9 Status = Enum('Status', ('Running', 'Pause', 'Over', 'Passed')) 10 11 Button = Enum('Button', ('Left', 'Down', 'Right', 'Up', 'Trans', 'Reset', 'Sound', 'Pause_start', 'Options'))
3、游戏基类
1 # -*- coding: utf-8 -*- 2 3 from point import Point 4 from utils import * 5 6 class Base: 7 __WIDTH = 10 8 __HEIGHT = 20 9 10 @classmethod 11 def width(cls): 12 return cls.__WIDTH 13 14 @classmethod 15 def height(cls): 16 return cls.__HEIGHT 17 18 def __init__(self): 19 self._score = 0 20 self._status = Status.Running 21 self._area = [ [ Block.Empty for y in range(self.__HEIGHT) ] for x in range(self.__WIDTH) ] 22 pass 23 24 def score(self): 25 return self._score 26 27 def area(self, x, y): 28 return self._area[x][y] 29 30 def _clear_area(self): 31 for col in self._area: 32 for i in range(len(col)): 33 col[i] = Block.Empty 34 35 @staticmethod 36 def forward_from(point, dir): 37 if dir == Direct.Left: 38 return Point(point.x - 1, point.y) 39 elif dir == Direct.Down: 40 return Point(point.x, point.y - 1) 41 elif dir == Direct.Right: 42 return Point(point.x + 1, point.y) 43 elif dir == Direct.Up: 44 return Point(point.x, point.y + 1) 45 else: 46 raise Exception('Invalid direct: {0}'.format(dir)) 47 48 @classmethod 49 def check_point_in_area(cls, point): 50 return point.x >= 0 and point.x < cls.__WIDTH and point.y >= 0 and point.y < cls.__HEIGHT 51 52 if __name__ == '__main__': 53 print('Base: width {0}, height {1}'.format(Base.width(), Base.height())) 54 print('Base(): score {0}'.format(Base().score()))
4、窗口绘制参数
1 # -*- coding: utf_8 -*- 2 3 from base import Base 4 5 # 游戏区域和显示窗口间的间隔 6 _area_view_gap = 5 7 # 小方块们和游戏区域间的间隔 8 _blocks_area_gap = 2 9 # 小方块和小方块之间的间隔 10 _block_block_gap = 1 11 12 # 小方块的边长 13 block_side = 10 14 15 # 小方块与小方块之间的偏移 16 _block_shift = block_side + _block_block_gap 17 18 # 显示窗口的左上角坐标 19 view_point = (0, 0) 20 # 游戏区域的左上角坐标 21 area_point = (view_point[0] + _area_view_gap, view_point[1] + _area_view_gap) 22 23 # 小方块们的左上角坐标 24 _blocks_point = (area_point[0] + _blocks_area_gap, area_point[1] + _blocks_area_gap) 25 # 小方块各自的左上角坐标 26 block_points = [ [ (_blocks_point[0] + x * _block_shift, _blocks_point[1] + y * _block_shift) for y in range(Base.height()) ] for x in range(Base.width()) ] 27 28 # 游戏区域的矩形大小 29 area_size = (block_points[Base.width() - 1][Base.height() - 1][0] + block_side + _blocks_area_gap - area_point[0], 30 block_points[Base.width() - 1][Base.height() - 1][1] + block_side + _blocks_area_gap - area_point[1] ) 31 32 # 信息区域的左上角坐标 33 info_area_point = (area_point[0] + area_size[0] + _area_view_gap, area_point[1]) 34 # 信息区域的矩形大小 35 info_area_size = (int(area_size[0]*2/3), area_size[1]) 36 37 # 显示区域的矩形大小 38 view_size = (info_area_point[0] + info_area_size[0] + _area_view_gap - view_point[0], info_area_point[1] + info_area_size[1] + _area_view_gap - view_point[1]) 39 40 # 窗口大小 41 screen_size = view_size 42 43 if __name__ == '__main__': 44 print('view_point {0}, view_size {1}'.format(view_point, view_size)) 45 print('area_point {0}, area_size {1}'.format(area_point, area_size)) 46 print('block_points {0}'.format(block_points))