我是靠谱客的博主 甜甜芹菜,最近开发中收集的这篇文章主要介绍pygame 大球吃小球,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

import pygame, random, math
#介于面向对象编程的理解不深,能写出来已实属不易,我就将我复盘的代码贴出来,自我反省. 其实不想写注释的,因为会的人一眼就能看懂.

BLACK = [0, 0, 0]
WHITE = [255, 255, 255]
RED = [255, 0, 0]
GREEN = [0, 255, 255]
BLUE = [0, 0, 255]

class Ball(object):
    def __init__(self, x, y, color, radius, dir1):
        self._x = x
        self._y = y
        self._color = color
        self._radius = radius
        self._dir1 = dir1

    @property
    def x(self):
        return self._x
    @property
    def y(self):
        return self._y
    @property
    def radius(self):
        return self._radius
    @radius.setter
    def radius(self, new_radius):
        self._radius = new_radius
    @property
    def color(self):
        return self._color

    def draw(self, screen):
        pygame.draw.circle(screen, self._color, (self._x, self._y), self._radius, 0)

    def move(self):
        self._x += self._dir1[0]
        self._y += self._dir1[1]
        if self._x - self._radius <= 20 or self._x + self._radius >= 780:
            self._dir1[0] = -self._dir1[0]
        if self._y - self._radius <= 20 or self._y + self._radius >= 780:
            self._dir1[1] = -self._dir1[1]

    def eat(self, balls):
        for ball2 in balls:
            if math.sqrt((self._x - ball2.x) ** 2 + (self._y - ball2.y) ** 2) < self._radius + ball2.radius 
                    and self._radius > ball2.radius:
                self._radius += round(ball2.radius * 0.05)
                balls.remove(ball2)
                break







def main():
    def create_ball():
        pos = event.pos
        x, y = pos[0], pos[1]
        red = random.randint(0, 255)
        green = random.randint(0, 255)
        blue = random.randint(0, 255)
        new_radius = random.randint(10, 100)
        color = [red, green, blue]
        dir1 =[random.randint(-20, 20), random.randint(-20, 20)]
        if  780 - new_radius >= x >= 20 + new_radius and 780 - new_radius >= y >= 20 + new_radius:
            ball = Ball(x, y, color, new_radius, dir1)

            balls.append(ball)
    pygame.display.init()
    screen = pygame.display.set_mode((800, 800))
    pygame.display.set_caption('球球大作战')
    clock = pygame.time.Clock()
    balls = []
    running = True

    while running:


        for event in pygame.event.get():
            print(event)
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                create_ball()
        screen.fill([255, 255, 255])
        pygame.draw.rect(screen, [0, 0, 0], [20, 20, 760, 760], 10)
        for ball in balls:
            ball.move()
            ball.eat(balls)
            ball.draw(screen)

        pygame.display.flip()
        clock.tick(20)

    pygame.quit()


if __name__ == '__main__':
    main()

 

最后

以上就是甜甜芹菜为你收集整理的pygame 大球吃小球的全部内容,希望文章能够帮你解决pygame 大球吃小球所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(35)

评论列表共有 0 条评论

立即
投稿
返回
顶部