pygame小游戏:弹弹球小游戏or可互动的屏保??!

制作不精,不喜勿喷。

作者没实力,暂停功能没有捣鼓出来,有实力的大佬可以试试。

import pygame
import random
import winreg
import wmi

PATH = "SYSTEM\\ControlSet001\\Enum\\"

m = wmi.WMI()

monitors = m.Win32_DesktopMonitor()

#音效初始
pygame.mixer.init()
bo_sound = pygame.mixer.Sound("bo.wav")
bo_sound.set_volume(0.2)

for m in monitors:
    subPath = m.PNPDeviceID  #

    if subPath == None:
        continue

        key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, infoPath)

    value = winreg.QueryValueEx(key, "EDID")[0]
    winreg.CloseKey(key)

    widthResolution = value[56] + (value[58] >> 4) * 256
    heightResolution = value[59] + (value[61] >> 4) * 256

width = widthResolution
height = heightResolution


class ball(): #球对象
    def __init__(self):#球初始化数据
        super().__init__()
        self.ball_radius = 20
        self.ball_x = pygame.mouse.get_pos()[0]
        self.ball_y = pygame.mouse.get_pos()[1]
        self.ball_speed_x = random.randint(1, 5)
        self.ball_speed_y = random.randint(3, 5)
        self.color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
    def get_ball_radius(self):#获得球radius
        return self.ball_radius
    def set_ball_radius(self,x):#设置球radius
        self.ball_radius = x
    def get_ball_x(self):#获得球x坐标
        return self.ball_x
    def set_ball_x(self,x):#设置球x坐标
        self.ball_x += x
    def get_ball_y(self):#获得球y坐标
        return self.ball_y
    def set_ball_y(self,y):#设置球y坐标
        self.ball_y += y
    def get_ball_speed_x(self):#获得x速度
        return self.ball_speed_x
    def set_ball_speed_x(self):#设置x速度
        self.ball_speed_x = -self.ball_speed_x
    def get_ball_speed_y(self):#获得y速度
        return self.ball_speed_y
    def set_ball_speed_y(self):#设置y速度
        self.ball_speed_y = -self.ball_speed_y
    def get_ball_color(self):#获得颜色def
        return self.color


pygame.init()

window_size = (width, height)
screen = pygame.display.set_mode(window_size)
pygame.display.set_caption("弹弹球")

BLACK = ('light green')

balls = []

# 创建时钟对象
clock = pygame.time.Clock()

running = True
while running:
    event = pygame.event.poll()
    if event.type == pygame.QUIT:
        running = 0
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:#判断是否左键
        balls.append(ball())#创建球对象并存进balls列表
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 3:#判断是否右键
        if len(balls) != 0:
            balls.remove(i)#从balls列表删除球
    elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 2:#判断是否中键
        sj = random.randint(1,10)
        for q in range(sj):
            balls.append(ball())  # 创建球对象并存进balls列表
    elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:#没做完的烂摊子(懒)
        1+1
    b = len(balls)



    if b >= 2000:# 电脑保护程序
        balls=[]

    for i in balls:#遍历balls列表
        ball_x = i.get_ball_x()
        ball_y = i.get_ball_y()
        if ball_x < i.get_ball_radius() or ball_x >= width - i.get_ball_radius():
            i.set_ball_speed_x()#碰到边界更改x前进方向
            bo_sound.play()
        if ball_y < i.get_ball_radius() or ball_y >= height - i.get_ball_radius():
            i.set_ball_speed_y()#碰到边界更改y前进方向
            bo_sound.play()

        i.set_ball_x(i.get_ball_speed_x())#设置球x坐标
        i.set_ball_y(i.get_ball_speed_y())#设置球y坐标



    screen.fill(BLACK)

    for i in balls:  #变量列表balls
        pygame.draw.circle(screen, i.get_ball_color(),(i.get_ball_x(), i.get_ball_y()), i.get_ball_radius())


    #控制游戏帧率
    clock.tick(120)

    f = pygame.font.SysFont(['方正粗黑宋简体', 'microsoftsansserif'], 50)
    text = f.render(str(b), True, ('blue'))
    screen.blit(text, (50, 150))

    pygame.display.flip()

pygame.quit()

音效那一段可以自行删除,或自行从网络上找资源,作者下载处:

(记得把名字改成“bo”并且要wav格式)

软木塞的声音(啵)音乐音效(00:00) - 菜鸟图库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值