猫史档案馆


【Python作品分享】小球球【求助帖】

用户:勇敢兔妈,永不飞斧勇敢兔妈,永不飞斧查看:0 回复:0 评论:0 创建时间:2020-03-20T22:17:06


【作品展示】

center_image

 

【作品介绍】

呵和&盒∑he喝何

 

【作品源代码】

import pygame

import random


class Ball():
    def __init__(self,surface,color,size):
        self.surface = surface
        self.size = size
        self.color = color

    def create_ball(self,pos):
        pygame.draw.circle(self.surface,self.color,pos,self.size)

    def run(self,pos,sc_size,speedy_x,speedy_y):
        if pos[1] >= 0+self.size and pos[1] <=sc_size-self.size:
            if pos[0] <= 0+self.size or pos[0] >= sc_size-self.size:
                speedy_x = -speedy_x
        elif pos[0] >= 0+self.size and pos[0] <= sc_size-self.size:
            if pos[1] <= 0+self.size or pos[1] >= sc_size-self.size:
                speedy_y = -speedy_y
        pos[0] += speedy_x
        pos[1] += speedy_y
        return speedy_x, speedy_y, pos[0], pos[1]


pygame.init()
sc_size = 500

sc = pygame.display.set_mode((sc_size,sc_size))

pos_x, pos_y = 60, 180
speedy_x, speedy_y = (1, 1)

ball_1 = Ball(sc,(255,140,140),25)

pos_x_, pos_y_ = 375, 150
speedy_x_, speedy_y_ = (2, 1)

ball_2 = Ball(sc, (喵)

pos_x_a, pos_y_a = 280, 440
speedy_x_a, speedy_y_a = (2, 2)

ball_3 = Ball(sc, (140, 255, 140), 25)

pos_x_a_, pos_y_a_ = 360, 260
speedy_x_a_, speedy_y_a_ = (1, 2)

ball_4 = Ball(sc, (255, 255, 100), 25)

pos_x0, pos_y0 = 450, 450
speedy_x0, speedy_y0 = (3, 3)

ball_0 = Ball(sc, (0, 0, 0), 25)

a,b,c=255,255,255

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.display.quit()
            exit()
    sc.fill((a,b,c))
    ball_1.create_ball([pos_x, pos_y])
    ball_2.create_ball([pos_x_, pos_y_])
    ball_3.create_ball([pos_x_a, pos_y_a])
    ball_4.create_ball([pos_x_a_, pos_y_a_])
    ball_0.create_ball([pos_x0, pos_y0])
    pygame.time.delay(35)
    pygame.display.update()
    speedy_x, speedy_y, pos_x, pos_y = ball_1.run([pos_x, pos_y], sc_size, speedy_x, speedy_y)
    speedy_x_, speedy_y_, pos_x_, pos_y_ = ball_2.run([pos_x_, pos_y_], sc_size, speedy_x_, speedy_y_)
    speedy_x_a, speedy_y_a, pos_x_a, pos_y_a = ball_3.run([pos_x_a, pos_y_a], sc_size, speedy_x_a, speedy_y_a)
    speedy_x_a_, speedy_y_a_, pos_x_a_, pos_y_a_ = ball_4.run([pos_x_a_, pos_y_a_], sc_size, speedy_x_a_, speedy_y_a_)
    speedy_x0, speedy_y0, pos_x0, pos_y0 = ball_0.run([pos_x0, pos_y0], sc_size, speedy_x0, speedy_y0)
    a -= 5
    if a <= 0:
        a = random.randint(100,255)
    b -= 6
    if b <= 0:
        b = random.randint(100, 255)
    c -= 4
    if c <= 0:
        c = random.randint(100, 255)













    








        

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页