美文网首页
day11作业 球球大作战

day11作业 球球大作战

作者: HavenYoung | 来源:发表于2018-07-30 19:11 被阅读0次
import pygame
import random
import math


# random_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800, 450))
    screen.fill((255, 255, 255))
    pygame.display.set_caption('bll game 2')

    pygame.display.flip()

    # all_balls中保存多个球
    # 每个球要保存:半径、圆心坐标、颜色、x速度、y速度
    all_balls = [
        {
            'r': random.randint(10,30),
            'pos':(100, 100),
            'color': (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
            'x_speed': random.randint(-3, 3),
            'y_speed': random.randint(-3, 3)
         },
        {
            'r': random.randint(10, 30),
            'pos': (200, 200),
            'color': (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
            'x_speed': random.randint(-3, 3),
            'y_speed': random.randint(-3, 3)
        },
        {
            'r': random.randint(10, 30),
            'pos': (300, 300),
            'color': (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
            'x_speed': random.randint(-3, 3),
            'y_speed': random.randint(-3, 3)
        }
    ]

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit(0)

            if event.type == pygame.MOUSEBUTTONDOWN:
                ball_new = {
                    'r': random.randint(10, 30),
                    'pos': event.pos,
                    'color': (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
                    'x_speed': random.randint(-3, 3),
                    'y_speed': random.randint(-3, 3)
                }
                all_balls.append(ball_new)

        # 刷新界面
        screen.fill((255, 255, 255))
        pygame.time.delay(10)

        for ball in all_balls:
            x, y = ball['pos']
            x_speed = ball['x_speed']
            y_speed = ball['y_speed']
            x += x_speed
            y += y_speed
            # 边界检测
            if x + ball['r'] >= 800:
                ball['x_speed'] *= -1

            if x <= ball['r']:
                ball['x_speed'] *= -1

            if y + ball['r'] >= 450:
                ball['y_speed'] *= -1

            if y <= ball['r']:
                ball['y_speed'] *= -1

            for ball1 in all_balls:
                for ball2 in all_balls:
                    if ball1 == ball2:
                        continue
                    distance = ball1['r'] + ball2['r']
                    x_edge1, y_edge1 = ball1['pos']
                    x_edge2, y_edge2 = ball2['pos']
                    if distance >= math.sqrt((x_edge1 - x_edge2) * (x_edge1 - x_edge2) +
                                             (y_edge1 - y_edge2) * (y_edge1 - y_edge2)):
                        index_random = random.randint(0, 1)
                        if index_random:
                            ball_del = ball1
                            ball2['r'] += ball1['r']
                            all_balls.remove(ball_del)
                            break
                        else:
                            ball_del = ball2
                            ball1['r'] += ball2['r']
                            all_balls.remove(ball_del)

            pygame.draw.circle(screen, ball['color'], (x,y), ball['r'])
            # 更新球对应的坐标
            ball['pos'] = x, y

        pygame.display.update()

ball_game3.gif

简单的碰撞

import pygame


def draw_ball(local, color, pos):
    """
    画球
    :param local:
    :return:
    """
    pygame.draw.circle(local, color, pos, 20)


# 方向对应的key值
up = 273
down = 274
left = 276
right = 275



if __name__ == '__main__':
    pygame.init()
    screen = pygame.display.set_mode((800, 450))
    screen.fill((255, 255, 255))
    pygame.display.set_caption('ball game')

    pygame.display.flip()

    # 保存初始坐标
    ball_x = 100
    ball_y = 100
    x_speed = 10
    y_speed = 0

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit(0)

            if event.type == pygame.KEYDOWN:
                if event.key == up:
                    y_speed = -10
                    x_speed = 0
                elif event.key == down:
                    y_speed = 10
                    x_speed = 0
                elif event.key == right:
                    x_speed = 10
                    y_speed = 0
                elif event.key == left:
                    x_speed = -10
                    y_speed = 0

        # 刷新屏幕
        screen.fill((255, 255, 255))
        # 设置初始方向
        ball_x += x_speed
        ball_y += y_speed
        # 边界检测
        if ball_x + 20 >= 800:
            ball_x = 800 - 20
            x_speed *= -1

        if ball_x <= 20:
            ball_x = 20
            x_speed *= -1

        if ball_y + 20 >= 450:
            ball_y = 450 - 20
            y_speed *= -1

        if ball_y <= 20:
            ball_y = 20
            y_speed *= -1

        # 设置障碍
        pygame.draw.rect(screen, (0, 0, 0), (300, 300, 100,10))

        if ball_x - 20 <= 300 + 100 and ball_x + 20 >= 300 and ball_y - 20 <= 310 and ball_y + 20 >= 300:
            x_speed = 0
            y_speed = 0
            font = pygame.font.SysFont('SimSun', 50)
            title = font.render('Game Over', True, (255, 0, 0))
            screen.blit(title,(300, 180))


        pygame.time.delay(17)
        draw_ball(screen, (255, 255, 0), (ball_x, ball_y))
        pygame.display.update()

ball_game4.gif

相关文章

  • day11作业 球球大作战

    简单的碰撞

  • day13_python_pygame(老师例题)

    球球大作战

  • 球球大作战

  • 球球大作战

    欢迎来到球球大作战,也是一个不错的选择,球球大作战是一个大球吃小球的一个游戏

  • 球球大作战

    球球大作战,大家应该都玩过,可是我今天玩的球球大作战可不是游戏哦! 这个星期五的晚上,妈妈说要带我...

  • 2017-07-20

    我最喜欢的电子游戏是球球大作战,跟贪吃蛇大作战相似。它的规则是求底下还没有标志的时候不能吐球、喷球。如果球在你...

  • 2017-08-25

    今天我们就来聊一款网上热门游戏球球大作战! 今天装逼松鼠带你挑战球球大作战生存模式! 首先让我看到生存模式,你有可...

  • 中秋佳节!球球大作战中秋皮肤花好月圆获取方法

    球球大作战中秋节花好月圆新皮肤上线啦,本次更新了2款光环皮肤,下面小编为大家带来的是球球大作战中秋光环皮肤花好月圆...

  • 球球大作战

  • 球球大作战

    前不久玩了一款游戏,球球大作战,知道它还是因为朋友们在各种群里分享的,刚开始也没当回事,就当一个广告一样置之不理,...

网友评论

      本文标题:day11作业 球球大作战

      本文链接:https://www.haomeiwen.com/subject/rwqqvftx.html