- 本文是纯净版本,无乱七八糟的文件要搞,可以直接复制。具体介绍或代码有报错请看↓
http://t.csdn.cn/lZ5Z2http://t.csdn.cn/lZ5Z2
import pygame
import random
import winreg
import wmi
PATH = "SYSTEM\\ControlSet001\\Enum\\"
m = wmi.WMI()
# 获取屏幕信息
monitors = m.Win32_DesktopMonitor()
for m in monitors:
subPath = m.PNPDeviceID
if subPath is None:
continue
infoPath = PATH + subPath + "\\Device Parameters"
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
class ball(): # 球对象
def __init__(self): # 球初始化数据
super().__init__()
self.ball_radius = 20 # 大小
cs_x = pygame.mouse.get_pos()[0] # 初始位置
if cs_x >= 0:
cs_x += 30
if cs_x >= widthResolution:
cs_x -= 50
cs_y = pygame.mouse.get_pos()[1]
if cs_y >= 0:
cs_y += 30
if cs_y >= heightResolution:
cs_y -= 50
self.ball_x = cs_x
self.ball_y = cs_y
# 初始速度/方向
balls_fx_x = random.randint(-5, 5)
balls_fx_y = random.randint(-5, 5)
if balls_fx_x == 0:
balls_fx_x += random.randint(1, 4)
elif balls_fx_y == 0:
balls_fx_y -= random.randint(1, 4)
self.ball_speed_x = balls_fx_x
self.ball_speed_y = balls_fx_y
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()
width = widthResolution
height = heightResolution
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:
b = len(balls)
try:
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.pop(0) # 从balls列表删除球
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 2: # 判断是否中键
sj = random.randint(1, 10)
for q in range(sj):
balls.append(ball()) # 创建球对象并存进balls列表
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: # 判断是否按下空格
print('暂停')
run = True
while run:
pygame.event.get()
f = pygame.font.SysFont(['fang-song'], 50)
text = f.render(str('暂停中'), True, 'YELLOW')
screen.blit(text, (160, 150))
pygame.display.flip()
event = pygame.event.poll()
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: # 判断是否再次按下空格
print('继续')
break
except:
print('键盘中断,已退出!')
break
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前进方向
if ball_y < i.get_ball_radius() or ball_y >= height - i.get_ball_radius():
i.set_ball_speed_y() # 碰到边界更改y前进方向
try:
i.set_ball_x(i.get_ball_speed_x()) # 设置球x坐标
i.set_ball_y(i.get_ball_speed_y()) # 设置球y坐标
except:
print('球设置失败,已退出!')
try:
screen.fill(BLACK)
except:
print('颜色填充失败,已退出!')
try:
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())
except:
print('键盘中断,已退出!')
break
# 控制游戏帧率
clock.tick(120)
f = pygame.font.SysFont(['方正粗黑宋简体'], 50)
text = f.render(str(b), True, 'blue')
screen.blit(text, (50, 150))
pygame.display.flip()
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
break
print('退出成功!')