day11 作業(yè)

一個打球吃小球的游戲


from math import sqrt
import  pygame
import  random
def r_c():
    return random.randint(0,255),random.randint(0,255),random.randint(0,255)


if __name__ == "__main__":
    pygame.init()
    screen =pygame.display.set_mode((600,400))
    screen.fill((255,255,255))
    pygame.display.flip()

    # all_balls 中保存多個球
    # 每個球的 半徑 圓心坐標 顏色 x速度 y速度
    all_balls = []






    while True:
        pygame.time.delay(25)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()
            if event.type ==pygame.MOUSEBUTTONDOWN:
                ball ={
                    'r': random.randint(10, 25),
                    'pos': event.pos,
                    'color': r_c(),
                    'x_speed': random.randint(-3, 3),
                    'y_speed': random.randint(-3, 3)
                }
                all_balls.append(ball)

        screen.fill((255,255,255))
        for ball_dict in all_balls:
            x , y = ball_dict["pos"]
            x_speed = ball_dict["x_speed"]
            y_speed =ball_dict["y_speed"]
            x += x_speed
            y += y_speed
            pygame.draw.circle(screen, ball_dict['color'], (x, y), ball_dict['r'])
            # 更新球對應的坐標
            ball_dict['pos'] = x, y


        for ball_dict in all_balls:
            x, y = ball_dict["pos"]
            x_speed = ball_dict["x_speed"]
            y_speed = ball_dict["y_speed"]
            y += y_speed
            x+= x_speed
            if x + ball_dict["r"] >= 600 or x -ball_dict["r"]<0:
                x_speed *= -1
            if y + ball_dict["r"]>=400 or y -ball_dict["r"]<0:
                y_speed *= -1
            for index1 in all_balls:
                for index2 in all_balls:
                    if index1 == index2:
                        continue
                    else:
                        x1 ,y1 = index1[]

            pygame.draw.circle(screen, ball_dict['color'], (x, y), ball_dict['r'])
            ball_dict['pos'] = x, y
            ball_dict['x_speed'] = x_speed
            ball_dict['y_speed'] = y_speed


        pygame.display.update()
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容