admin 管理员组文章数量: 887021
---------------------------------------------------------------------------------------------------------------------------------
pygame小游戏:弹弹球小游戏or可互动的屏保??!_2301_79532845的博客-CSDN博客https://blog.csdn/2301_79532845/article/details/132760034?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522169485089616800188528852%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=169485089616800188528852&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-12-132760034-null-null.142%5Ev94%5EchatsearchT3_1&utm_term=pygame%E5%BC%B9%E5%BC%B9%E7%90%83&spm=1018.2226.3001.4187---------------------------------------------------------------------------------------------------------------------------------
本文为上文链接的更新版,有兴趣的可以康康↑
更新内容:
0,添加了暂停功能(空格键暂停)
1,优化了在屏幕边缘点击会粘在边边角角的问题
2,200多个bug被修掉了!(我也不知道为什么有两百多个,可能写着写着就有了)
3,修改了小球生成时的只有往右下角发射的问题(现在可以全角度发射了!)
4,添加了一些错误提示(好像也没什么用嘻嘻嘻)
5,添加了许多注释,新手也能看懂!
如果直接复制的代码有报错请看文章最后(有解决方法)
---------------------------------------------------------------------------------------------------------------------------------
注意!
球多了巨卡,请根据自己的电脑量力而行!
什么?球少了不刺激?第144行可以修改球的上限,具体如下:
if b >= (你想要数量上限):
直接拿去替换第144行的if语句就可以了
---------------------------------------------------------------------------------------------------------------------------------
一,直接复制代码↓
一定要有音效文件才能运行,没有会报错的,具体看文章最后【二,报错解决方案】!
想要无需音效文件的代码看文章末尾【三,无需音效文件版(极速版)】!
"""↓所有系统全部启动启动启动↓"""
import pygame
import random
import winreg
import wmi
import datetime
while True:
print('设置球的大小:')
print('A,大 B,中 C,小 D,自定义')
qd_input = input('请输入:')
if qd_input == 'A' or qd_input == 'a':
qd = 85
ball_big = qd + 20
break
elif qd_input == 'B' or qd_input == 'b':
qd = 55
ball_big = qd + 20
break
elif qd_input == 'C' or qd_input == 'c':
qd = 25
ball_big = qd + 20
break
elif qd_input == 'D' or qd_input == 'd':
while True:
try:
qd = int(input('请输入自定义大小:'))
if qd <= 10:
print('太小了,请重新输入')
continue
ball_big = qd + 20
break
except:
print('非法输入')
else:
print('输入不正确,请重试')
break
'''获取屏幕大小↓'''
PATH = "SYSTEM\\ControlSet001\\Enum\\"
m = wmi.WMI()
# 获取屏幕信息
monitors = m.Win32_DesktopMonitor()
for m in monitors:
subPath = m.PNPDeviceID #
if subPath is None:
continue
'''这个是没有错误的他就叫↓subpath'''
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
'''获取屏幕大小↑'''
'''音效初始↓'''
pygame.mixer.init()
bo_sound = pygame.mixer.Sound("bo.wav")
bo_sound.set_volume(0.2)
'''音效初始↑'''
'''球的初始数据↓'''
class ball(): # 球对象
def __init__(self): # 球初始化数据
super().__init__()
self.ball_radius = qd # 大小
cs_x = pygame.mouse.get_pos()[0] # 初始位置
if cs_x <= 0:
cs_x += ball_big
if cs_x >= widthResolution - 20:
cs_x -= ball_big
cs_y = pygame.mouse.get_pos()[1]
if cs_y <= 0:
cs_y += ball_big
if cs_y >= heightResolution - 20:
cs_y -= ball_big
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启动↓'''
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 >= 1800: # 电脑保护程序(可更改)
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()
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(['fang—song'], 50)
text = f.render(str(b), True, 'blue')
screen.blit(text, (50, 150))
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
pygame.quit()
break
now_time = datetime.datetime.now()
text = f.render(
str('{}月{}日{}:{}:{}'.format(now_time.month, now_time.day, now_time.hour, now_time.minute, now_time.second)),
True, 'black')
screen.blit(text, (1350, 900))
pygame.display.flip()
'''程序主循环↑'''
print('退出成功!') # 按ESC退出后输出
---------------------------------------------------------------------------------------------------------------------------------
二,报错解决方案
报错了请检查是否下载了random,pygame,wmi,winreg,datetime库,如没有在终端输入:
pip install random
pip install pygame
pip install winreg
pip install wmi
pip install datetime
如果已经下载了这些库,那就是没有音效文件,我已经贴到文章顶部了,如果没有可以点击文章开头链接,在链接文章的底部有下载地址,记得改成wav格式并放在默认python目录(就是你平常新建python文件的地方)里,不然没法正常运行!
三,无需音效文件版(极速版)
如果觉得装音效太麻烦了可以试试这个直接复制版,没有音效文件,可以直接用↓
http://t.csdn/358Lmhttp://t.csdn/358Lm
看在这么贴心的份上点个赞吧(球球了)!
版权声明:本文标题:pygame小游戏弹弹球屏保(代码全注释,新手也能看懂) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1724521012h755286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论