|
| 1 | +# coding:utf-8 |
| 2 | +import random |
| 3 | +import time |
| 4 | + |
| 5 | +import pygame |
| 6 | + |
| 7 | +W = 600 |
| 8 | +H = 500 |
| 9 | + |
| 10 | +class Ball: |
| 11 | + |
| 12 | + x = None # x坐标 |
| 13 | + y = None # y坐标 |
| 14 | + speed_x = None # x方向的速度 |
| 15 | + speed_y = None # y方向的速度 |
| 16 | + radius = None # 半径 |
| 17 | + color = None # 颜色 |
| 18 | + |
| 19 | + def __init__(self, x, y, speed_x, speed_y, radius, color): |
| 20 | + """ |
| 21 | + 初始化 |
| 22 | + :param x: X坐标 |
| 23 | + :param y: Y坐标 |
| 24 | + :param speed_x: X轴方向速度 |
| 25 | + :param speed_y: Y轴方向速度 |
| 26 | + :param radius: 半径 |
| 27 | + :param color: 颜色 |
| 28 | + """ |
| 29 | + self.x = x |
| 30 | + self.y = y |
| 31 | + self.speed_x = speed_x |
| 32 | + self.speed_y = speed_y |
| 33 | + self.radius = radius |
| 34 | + self.color = color |
| 35 | + |
| 36 | + def draw(self, screen): |
| 37 | + """ |
| 38 | + 绘制小球 |
| 39 | + :param screen: 窗口 |
| 40 | + :return: |
| 41 | + """ |
| 42 | + pygame.draw.circle(screen, self.color, [self.x, self.y], self.radius) |
| 43 | + |
| 44 | + |
| 45 | + def move(self, screen): |
| 46 | + """ |
| 47 | + 小球移动 |
| 48 | + :param screen: 窗口 |
| 49 | + :return: |
| 50 | + """ |
| 51 | + self.x += self.speed_x |
| 52 | + self.y += self.speed_y |
| 53 | + |
| 54 | + # 左右边界 |
| 55 | + if self.x > W - self.radius or self.x < self.radius: |
| 56 | + self.speed_x = -self.speed_x |
| 57 | + |
| 58 | + # 上下边界 |
| 59 | + if self.y > H - self.radius or self.y < self.radius: |
| 60 | + self.speed_y = -self.speed_y |
| 61 | + # 移动频率 |
| 62 | + time.sleep(0.001) |
| 63 | + self.draw(screen) |
| 64 | + |
| 65 | + |
| 66 | +class Player: |
| 67 | + |
| 68 | + radius = None |
| 69 | + color = None |
| 70 | + x = 1000 |
| 71 | + y = 1000 |
| 72 | + |
| 73 | + def __init__(self, radius, color): |
| 74 | + """ |
| 75 | + 初始化 |
| 76 | + :param radius: 半径 |
| 77 | + :param color: 颜色 |
| 78 | + """ |
| 79 | + self.radius = radius |
| 80 | + self.color = color |
| 81 | + |
| 82 | + def move(self, screen): |
| 83 | + """ |
| 84 | + 大球移动 |
| 85 | + :return: |
| 86 | + """ |
| 87 | + # 鼠标检测 |
| 88 | + if pygame.mouse.get_focused(): |
| 89 | + # 获取光标位置, |
| 90 | + x, y = pygame.mouse.get_pos() |
| 91 | + |
| 92 | + mouse = pygame.mouse.get_pressed() |
| 93 | + |
| 94 | + pygame.draw.circle(screen, self.color, [x, y], self.radius) |
| 95 | + self.x = x |
| 96 | + self.y = y |
| 97 | + |
| 98 | +balls = [] |
| 99 | +def create_ball(screen): |
| 100 | + """ |
| 101 | + 创建小球 |
| 102 | + :param screen: |
| 103 | + :return: |
| 104 | + """ |
| 105 | + x = random.randint(0, W) |
| 106 | + y = random.randint(0, H) |
| 107 | + speed_x = random.randint(-5, 5) |
| 108 | + speed_y = random.randint(-5, 5) |
| 109 | + r = 3 |
| 110 | + color = 'white' |
| 111 | + b = Ball(x, y, speed_x, speed_y, r, color) |
| 112 | + |
| 113 | + balls.append(b) |
| 114 | + |
| 115 | + b.draw(screen) |
| 116 | + |
| 117 | +def show_text(screen, text, pos, color, font_bold=False, font_size=18, font_italic=False): |
| 118 | + """ |
| 119 | + 显示文字 |
| 120 | + :param screen: 窗口 |
| 121 | + :param text: 文字 |
| 122 | + :param pos: 坐标 |
| 123 | + :param color: 颜色 |
| 124 | + :param font_bold: 是否粗体 |
| 125 | + :param font_size: 大小 |
| 126 | + :param font_italic: 是否斜体 |
| 127 | + :return: |
| 128 | + """ |
| 129 | + cur_font = pygame.font.SysFont('Courier', font_size) |
| 130 | + cur_font.set_bold(font_bold) |
| 131 | + cur_font.set_italic(font_italic) |
| 132 | + text_fmt = cur_font.render(text, 1, color) |
| 133 | + screen.blit(text_fmt, pos) |
| 134 | + |
| 135 | +def close(): |
| 136 | + for event in pygame.event.get(): |
| 137 | + if event.type == pygame.QUIT: |
| 138 | + pygame.quit() |
| 139 | + exit(0) |
| 140 | + |
| 141 | +def main(): |
| 142 | + # 初始化pygame模块 |
| 143 | + pygame.init() |
| 144 | + # 设置窗口大小 |
| 145 | + screen = pygame.display.set_mode((W,H)) |
| 146 | + # 设置窗口标题 |
| 147 | + pygame.display.set_caption('是男人就坚持100秒') |
| 148 | + |
| 149 | + for i in range(0, 10): |
| 150 | + create_ball(screen) |
| 151 | + |
| 152 | + p = Player(10, 'red') |
| 153 | + text_time = "TIME:%.3d" % (time.perf_counter()) |
| 154 | + |
| 155 | + is_loop = True |
| 156 | + while is_loop: |
| 157 | + # 重绘屏幕 |
| 158 | + screen.fill((0)) |
| 159 | + |
| 160 | + p.move(screen) |
| 161 | + |
| 162 | + for ball in balls: |
| 163 | + ball.move(screen) |
| 164 | + if abs(p.x - ball.x) < 13 and abs(p.y - ball.y) < 13: |
| 165 | + is_loop = False |
| 166 | + break |
| 167 | + |
| 168 | + # 刷新显示 |
| 169 | + text_time = "TIME:%.3d" % (time.perf_counter()) |
| 170 | + show_text(screen, text_time, (500, 40), (0, 255, 0), True) |
| 171 | + pygame.display.update() |
| 172 | + |
| 173 | + close() |
| 174 | + |
| 175 | + while True: |
| 176 | + close() |
| 177 | + show_text(screen, "Game over!", (120, 180), "green", True, 60) |
| 178 | + show_text(screen, text_time, (220, 270), "green", True, 30) |
| 179 | + |
| 180 | + pygame.display.update() |
| 181 | + |
| 182 | + |
| 183 | +if __name__ == '__main__': |
| 184 | + main() |
0 commit comments