python练习---大球吃小球游戏

要求:
鼠标点击屏幕会自动产生不同大小的小球,小球会自动移动,碰到屏幕边界反弹回来,当不同大小的小球相遇在一起发生碰撞,大的小球会吃掉较小的小球,变得更大
代码:

import pygame
import random
import math

def random_color():
    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.set_caption('大球吃小球')
    pygame.display.flip()
    #all_ball中保存多少个球
    #每个球要保存:半径,圆心坐标,颜色,x方向的速度,y方向的速度
    all_balls=[
        {
            'r':random.randint(10,15),
            'pos':(100,100),
            'color':(random_color()),
            'x_speed':random.randint(0,1),
            'y_speed': random.randint(0, 1),
            'live':True
        }
    ]
    #这里用到了python里面的字典!
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                exit()

            if event.type == pygame.MOUSEBUTTONDOWN:
                #点击一下鼠标创建一个球
                ball = {
                    'r': random.randint(10,15),
                    'pos': event.pos,
                    'color': (random.randint(0,255),random.randint(0,255),random.randint(0,255)),
                    'x_speed': random.randint(0,1),
                    'y_speed': random.randint(0,1),
                    'live': True
                }
                #保存球
                all_balls.append(ball)
            #刷新界面
        screen.fill((255,255, 255))
        for ball in all_balls:
            # 取出球原来的x和y以及速度
            x,y = ball['pos']
            x_speed = ball['x_speed']
            y_speed = ball['y_speed']

            if x + ball['r'] >= 600:
                x_speed *= -1
            if x - ball['r'] <= 0:
                x_speed *= -1
            if y + ball['r'] >= 400:
                y_speed *= -1
            if y - ball['r'] <= 0:
                y_speed *= -1
            x += x_speed
            y += y_speed
            pygame.draw.circle(screen, ball['color'], (x,y), ball['r'])
            # 更新球的坐标
            ball['pos'] = x,y
            ball['x_speed'] = x_speed
            ball['y_speed']=y_speed

            for ball1 in all_balls:
                    if all_balls.index(ball) == all_balls.index(ball1):
                        pass
                    else:
                        other_x, other_y=ball1['pos']
                        dx = x - other_x
                        dy = y - other_y
                        distance = math.sqrt(dx**2+dy**2) #距离
                        if distance < ball['r']+ball1['r']:
                            if ball['r'] > ball1['r']:
                                ball['r']=int(ball['r']+ball1['r']/5)
                                all_balls.remove(ball1)

        pygame.display.update()
        #这里是定义了大球吃小球

运行效果:
在这里插入图片描述
在这里插入图片描述
也可以鼠标点击产生很多小球:
在这里插入图片描述
是不是很神奇快来尝试一下!

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会为你编写一个简单的Python游戏,名为"大球小球"。在这个游戏中,你将控制一个大球,不断地掉随机生成的小球来增加得分。当大球小球时,得分增加,小球会重新生成在随机位置上。游戏结束的条件是大球碰到边界或者自己的身体。 下面是游戏的代码实现: ```python import pygame import sys import random # 初始化游戏 pygame.init() # 定义游戏窗口大小和背景颜色 window_width = 800 window_height = 600 bg_color = (255, 255, 255) # 定义大球的初始位置、大小和移动速度 ball_pos = [window_width/2, window_height/2] ball_radius = 20 ball_color = (255, 0, 0) ball_speed = 5 # 定义小球的初始位置、大小和移动速度 small_ball_pos = [random.randint(0, window_width), random.randint(0, window_height)] small_ball_radius = 10 small_ball_color = (0, 0, 255) small_ball_speed = 3 # 定义得分 score = 0 # 创建游戏窗口 window = pygame.display.set_mode((window_width, window_height)) pygame.display.set_caption("大球小球") # 游戏主循环 while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 移动大球 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: ball_pos[0] -= ball_speed if keys[pygame.K_RIGHT]: ball_pos[0] += ball_speed if keys[pygame.K_UP]: ball_pos[1] -= ball_speed if keys[pygame.K_DOWN]: ball_pos[1] += ball_speed # 判断大球是否碰到边界 if ball_pos[0] - ball_radius < 0 or ball_pos[0] + ball_radius > window_width or \ ball_pos[1] - ball_radius < 0 or ball_pos[1] + ball_radius > window_height: print("游戏结束,得分:", score) pygame.quit() sys.exit() # 判断大球是否小球 distance = ((ball_pos[0] - small_ball_pos[0]) ** 2 + (ball_pos[1] - small_ball_pos[1]) ** 2) ** 0.5 if distance <= ball_radius + small_ball_radius: score += 1 small_ball_pos = [random.randint(0, window_width), random.randint(0, window_height)] # 绘制游戏界面 window.fill(bg_color) pygame.draw.circle(window, ball_color, (int(ball_pos[0]), int(ball_pos[1])), ball_radius) pygame.draw.circle(window, small_ball_color, (int(small_ball_pos[0]), int(small_ball_pos[1])), small_ball_radius) pygame.display.update() ``` 你可以将上述代码保存为一个.py文件,并运行它来开始游戏。在游戏中,你可以使用键盘的方向键来控制大球的移动。当大球小球时,得分会增加。当大球碰到边界或者自己的身体时,游戏结束并显示得分。 希望这个小游戏能满足你的需求!如果有任何问题,请随时告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值