当前位置: 首页>>代码示例>>Python>>正文


Python Shape.move方法代码示例

本文整理汇总了Python中shape.Shape.move方法的典型用法代码示例。如果您正苦于以下问题:Python Shape.move方法的具体用法?Python Shape.move怎么用?Python Shape.move使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在shape.Shape的用法示例。


在下文中一共展示了Shape.move方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Tetris

# 需要导入模块: from shape import Shape [as 别名]
# 或者: from shape.Shape import move [as 别名]
class Tetris(object):
    W = 12          # the width of play board
    H = 20          # the height of play board
    TILEW = 20      # the pixel width/height of a tile
    START = (100, 40) # the playboard lefttop on background
    SPACE = 1000    # shape will fall after xxx ms(level 1)
    def __init__(self, screen):
        self.stat = "game"
        self.WIDTH = self.TILEW * self.W
        self.HEIGHT = self.TILEW * self.H
        self.screen = screen
        self.pause = False
        # the array save current situation
        # same as screen cood
        self.board = []
        for i in xrange(self.H):
            line = [ None ] * self.W
            self.board.append(line)
        # will display
        self.level = 1
        self.killed = 0
        self.score = 0
        # after this time, shape falls
        self.time = self.SPACE * 0.9 ** (self.level - 1)
        # save the elapsed time after last fail
        self.elapsed = 0
        # used for judge pressed firstly or for a  long time
        self.pressing = 0
        # the moving shape
        self.shape = Shape(self.START,
                (self.WIDTH, self.HEIGHT), (self.W, self.H))
        self.shape.set_board(self.board)
        self.board_image = pygame.Surface((self.WIDTH, self.HEIGHT))
        # draw the background once
        self.screen.blit(pygame.image.load(
            util.file_path("background.jpg")).convert(), (0, 0))
        self.display_info()

    def update(self, elapse):
        for e in pygame.event.get():
            if e.type == KEYDOWN:
                self.pressing = 1
                self.move(e.key == K_UP, e.key == K_DOWN,
                        e.key == K_LEFT, e.key == K_RIGHT)
                if e.key == K_ESCAPE:
                    self.stat = 'menu'
                elif e.key == K_SPACE:
                    self.pause = not self.pause
            elif e.type == KEYUP and self.pressing:
                self.pressing = 0
            elif e.type == QUIT:
                self.stat = 'quit'
        if self.pause:
            self.draw()
            return self.stat
        if self.pressing:
            pressed = pygame.key.get_pressed()
            self.move(pressed[K_UP], pressed[K_DOWN],
                    pressed[K_LEFT], pressed[K_RIGHT])
        self.elapsed += elapse
        if self.elapsed >= self.time:
            self.next()
            self.elapsed = self.elapsed - self.time
            self.draw()
        return self.stat

    def move(self, u, d, l, r):
        if not (u or d or l or r):
            return
        # pressed for the first time
        # pressed for a long time
        if self.pressing == 1 or self.pressing > 10:
            if u:   self.shape.rotate()
            elif d: self.next()     #self.shape.move(0, 1) is bad
            elif l: self.shape.move(-1, 0)
            elif r: self.shape.move(1, 0)
            self.draw()
        self.pressing += 1

    def check_line(self):
        self.add_to_board()
        filled = []
        for i in xrange(self.H-1, -1, -1):
            line = self.board[i]
            sum = 0
            for t in line:
                sum += 1 if t else 0
            if sum == self.W:
                filled.append(i)
            elif sum == 0:
                break
        if i == 0 and sum !=0:
            self.game_over()
        self.create_board_image() # used for killing animation
        self.kill_line(filled)
        self.create_board_image() # used for update

    def kill_line(self, filled=[]):
        if len(filled) == 0:
            return
#.........这里部分代码省略.........
开发者ID:sherlockkenan,项目名称:Tetris,代码行数:103,代码来源:tetris.py


注:本文中的shape.Shape.move方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。