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


Python UI.draw_rect方法代码示例

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


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

示例1: Playing

# 需要导入模块: from ui import UI [as 别名]
# 或者: from ui.UI import draw_rect [as 别名]

#.........这里部分代码省略.........

    def rand_pos(self, side):
        xpos = random.randrange(side, W - side)
        ypos = random.randrange(2.5 * H/10 + side, H - side)
        pos = (xpos, ypos)
        rect = pygame.Rect(pos, (side, side))
        return pos, rect

    def is_unique(self, rect):
        if len(self.squares) == 0:
            return True
        for square in self.squares:
            if rect.colliderect(square[3]):
                return False
        return True

    def rand_color(self):
        return random.choice([RED, GREEN, BLUE, YELLOW, LTBLUE, PURPLE, LIME,
                              VIOLET, PINK, NAVY])

    def rand_vel(self):
        xvel = random.choice([-5,-4,-3,3,4,5])
        yvel = random.choice([-5,-4,-3,3,4,5])
        vel = (xvel, yvel)
        return vel

    def destroy(self, square):
        self.squares.remove(square)
        self.spawn()

    def spawn(self):
        side = 50
        pos, rect = self.rand_pos(side)
        while not self.is_unique(rect):
            pos, rect = self.rand_pos(side)
        color = self.rand_color()
        vel = self.rand_vel()
        self.squares.append([pos, (side, side), color, rect, vel])

    def get_time(self):
        return ((self.countdown - \
                (pygame.time.get_ticks() - self.start_time)) // 1000) + 1

    def update(self, screen):
        # draw stats
        time = "Next Change: " + str(self.get_time())
        lives = "Lives: " + str(self.lives)
        penalties = "Penalties: " + str(self.penalties) + " of " \
                    + str(self.penalties_per_life)
        score = "Score: " + str(self.score)
        self.ui.draw_text(screen, lives, location=(W/10, H/10), align=-1)
        self.ui.draw_text(screen, penalties, location=(W/10, 2*H/10), align=-1)
        self.ui.draw_text(screen, score, location=(9 * W/10, H/10), align=1)
        self.ui.draw_text(screen, time, location=(9 * W/10, 2 * H/10), align=1)
        self.ui.draw_text(screen, 'Bonus', (3*W/10+20, H/10 - 60), align=-1)
        self.ui.draw_text(screen, 'Penalty', (7*W/10-20, H/10 - 60), align=1)

        # update positions
        for square in self.squares:
            pos, vel = square[0], square[4]
            pos = (pos[0] + vel[0], pos[1] + vel[1])

            # if near edges or barrier, reverse direction
            side = square[1][0]
            if pos[0] < 0:
                pos = (0, pos[1])
                vel = (vel[0] * -1, vel[1])
            elif pos[0] > W - side:
                pos = (W - side, pos[1])
                vel = (vel[0] * -1, vel[1])

            if pos[1] < 2.5 * H/10 + side:
                pos = (pos[0], 2.5 * H/10 + side)
                vel = (vel[0], vel[1] * -1)
            elif pos[1] > H - side:
                pos = (pos[0], H - side)
                vel = (vel[0], vel[1] * -1)

            # update square data before going to next square
            square[0] = pos
            square[4] = vel
            rect = square[3]
            rect.topleft = pos
            square[3] = rect


        # draw squares
        screen.lock()
        try:
            for square in self.squares:
                pos = square[0]
                size = square[1]
                color = square[2]
                self.ui.draw_rect(screen, pos, size, color)
            self.ui.draw_rect(screen, (3*W/10+50, H/10 - 15), (60, 60),
                              self.bonus_color)
            self.ui.draw_rect(screen, (6*W/10+10, H/10 - 15), (60, 60),
                              self.penalty_color)
        finally:
            screen.unlock()
开发者ID:CodeSkool,项目名称:SimpleGUI2Pygame,代码行数:104,代码来源:React.py


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