用户:
兴奋的木叶龙mC2查看:0 回复:2 评论:0 创建时间:2021-10-02T15:47:42
【作品展示】

【作品介绍】
一个模块一个有用的模块
一个能辅助您进行pygame编程的模块
一个非常非常……(省略800字)好的模块
最新版本,pygame_helper(mypygame)模块,来了!这引进了超多功能,由起初的200多代码变为300+!
比如一个弹弹球,用它也只要60+代码←
【作品源代码】
from pygame.locals import *
def error(e):
'''可以实现自定义报错,内容为e'''
raise Error(str(e))
try:
import random
except:
error("缺少必须模块random或导入错误!")
try:
import sys
except:
error("缺少必须模块sys或导入错误!")
try:
import pygame
except:
error("缺少必须模块pygame或导入错误!")
try:
import time
except:
error("缺少必须模块time或导入错误!")
try:
import datetime
except:
error("缺少必须模块datetime或导入错误!")
try:
import io
except:
error("缺少必须模块io或导入错误!")
pygame.init()
update = pygame.display.update
set_mode = pygame.display.set_mode
set_caption = pygame.display.set_caption
now_time = datetime.datetime.now
wait = time.sleep
print("本模块导入了sys,random,time,datetime,pygame,io模块,以及pygame.locals里所有内容。",
"使用前,请先创建一个窗口(可以用fit_window()创建)。若需要帮助请调用HELP()。", "文件路径:python/Lib/site-packages/mypygame", sep='\n')
class colors:
black = (0, 0, 0, 255)
so_dark_grey = (30, 30, 30, 255)
dark_grey = (80, 80, 80, 255)
grey = (122, 122, 122, 255)
light_grey = (230, 230, 230, 255)
so_light_grey = (250, 250, 250, 255)
white = (255, 255, 255, 255)
brown = (122, 122, 0, 255)
red = (255, 0, 0, 255)
orange = (255, 122, 0, 255)
light_yellow = (255, 255, 122, 255)
yellow = (255, 255, 0, 255)
green = (0, 255, 0, 255)
dark_green = (0, 122, 0, 255)
light_green = (0, 122, 0, 255)
ching = (0, 255, 255, 255)
dark_blue = (0, 0, 122, 255)
blue = (0, 0, 255, 255)
light_blue = (122, 122, 255, 255)
dark_purple = (122, 0, 122, 255)
purple = (122, 0, 122, 255)
pink = (255, 0, 255, 255)
#pygame辅助函数
def fill(window, color):
'''填充窗口'''
window.fill(color)
pygame.display.update()
class Draw:
def line(window, color, startcoords, endcoords, width=5):
'''画线'''
pygame.draw.line(
window, color, startcoords, endcoords, width)
pygame.display.update()
def rect(window, x, y, height, width, color):
'''画方形'''
rect1 = pygame.Rect(x, y, width, height)
pygame.draw.rect(window, color, r)
pygame.display.update()
def circle(window, color, coords, radius, width=None):
'''在指定的中心点画圆,如果width为none,画实心圆'''
if width:
pygame.draw.circle(
window, color, coords, radius, width)
elif not width:
pygame.draw.circle(window, color, coords,radius)
update()
def arc(window, color, x,y,width,height,start,end,width1=None):
'''画弧形,width为宽,width1为边长'''
from math import radians
if width1:
pygame.draw.arc(window, color, (x,y,width,height),radians(start),radians(end),width1)
elif not width1:
pygame.draw.arc(window, color, (x,y,width,height),radians(start),radians(end))
pygame.display.update()
def crect(window, x, y,height,width,color):
'''在指定的中心点画方形'''
rect1 = pygame.Rect(x-width//2, y-height//2,width,height)
pygame.draw.rect(window, color, rect1)
pygame.display.update()
def cloadpic(window, picobject, x,y,width=100,height=100,angle=0,auto=True):
'''在指定的中心点加载图片,如果auto为True,按图片大小加载,否则按用户的设置加载'''
space = pygame.image.load(str(picobject)).convert_alpha()
nspace = pygame.transform.rotate(space, angle)
w, h = nspace.get_size()
if auto:
window.blit(nspace, (x-w//2, y-h//2))
update()
if auto == False:
nnspace = pygame.transform.喵oothscale(nspace, (width,height))
window.blit(nnspace, (x-width//2, y-height//2))
update()
def loadpic(window, picobject, x,y,width=100,height=100,angle=0,auto=True):
'''加载图片,如果auto为True,按图片大小加载,否则按用户的设置加载'''
space = pygame.image.load(str(picobject)).convert_alpha()
nspace = pygame.transform.rotate(space, angle)
if auto:
window.blit(nspace, (x, y))
update()
if auto == False:
nnspace = pygame.transform.喵oothscale(nspace, (width,height))
window.blit(nnspace, (x, y))
update()
def Systext(window, size, text,color,centerx,centery,filename=None,backcolor=None,angle=0):
'''在指定的中心点打印文字'''
font = pygame.font.SysFont(None, size)
if backcolor != None:
text = font.render(str(text), True,pygame.Color(color),pygame.Color(backcolor))
else:
text = font.render(str(text), True,pygame.Color(color))
content = pygame.transform.rotate(text, angle)
rect = content.get_rect()
rect.center = (centerx, centery)
window.blit(content, rect)
pygame.display.update()
def printtext(window, size, text,color,x,y,filename=None,backcolor=None,angle=0):
'''打印文字'''
font = pygame.font.SysFont(None, size)
if backcolor != None:
text = font.render(str(text), True,pygame.Color(color),pygame.Color(backcolor))
else:
text = font.render(str(text), True,pygame.Color(color))
content = pygame.transform.rotate(text, angle)
rect = content.get_rect()
rect.topleft = (x, y)
window.blit(content, rect)
pygame.display.update()
def mouse_px():
'''鼠标的x坐标'''
return pygame.mouse.get_pos()[0]
def mouse_py():
'''鼠标的y坐标'''
return pygame.mouse.get_pos()[1]
def m_right():
'''鼠标的右键是否按下'''
return pygame.mouse.get_pressed()[2]
def m_middle():
'''鼠标的滚轮是否按下'''
return pygame.mouse.get_pressed()[1]
def m_left():
'''鼠标的左键是否按下'''
return pygame.mouse.get_pressed()[0]
def mouse_r():
'''鼠标的相对移动'''
return pygame.mouse.get_rel()[0]
def list_str(listobject, sep="\n"):
'''列表转字符串,每个元素后加上sep'''
s = ""
for i in listobject:
s += (str(i)+str(sep))
return s
def fit_window(x, y, title='python game',color=colors.white):
'''创建窗口,返回这个窗口'''
window = set_mode((x, y))
set_caption(str(title))
fill(window, color)
return window
#其他辅助函数
def KeyCheck(key='any'):
'''如果参数为any按下任意键返回True'''
if key == 'any':
key = pygame.key.get_pressed()
for i in key:
if i:
return True
elif key != 'any':
keys = pygame.key.get_pressed()
if keys[key]:
return True
else:
return False
def Key():
'''返回按下的键的ASCLL码'''
for event in pygame.event.get():
if event.type == (KEYDOWN):
return pygame.event.get(KEYDOWN)
def CheckQUIT():
'''如果QUIT事件响应,终止程序'''
if len(pygame.event.get(QUIT)) > 0:
pygame.quit()
sys.exit()
pygame.init()
def Quit():
'''直接退出程序'''
pygame.quit()
sys.exit()
pygame.init()
def readc(filename, num):
'''读取第num个字符(num不能为0)'''
file = open(str(filename), "r")
c = file.read(int(num))
file.close()
return str(c)[-1]
def fit_tuple(tup, key, newvalue):
'''修改元祖'''
list1 = list(tup)
list1[int(key)] = newkey
list2 = tuple(list1)
return list2
def r_line(filename, line):
'''读取第line行'''
file = open(str(filename))
data = file.readlines()
file.close()
return data[line-1]
def r_lines(filename):
'''读取整个文件,以列表形式返回'''
file = open(str(filename), "r")
ss = file.readlines()
file.close()
return list(ss)
def writef(filename, s='\n', mode='a',text="\n"):
'''写入filename'''
if mode == 'w':
file = open(filename, 'w')
file.write(str(text))
file.close()
elif mode == "a":
file = open(filename, 'a')
file.write(str(text))
file.close()
elif mode == "x":
file = open(filename, 'x')
file.write(str(text))
file.close()
elif mode == "r":
raise Error("io.UnsupportedOperation:函数‘writebf ’不能读文件")
else:
raise Error("ValueError:请输入有效的打开模式!(w,a,x)")
def readbc(filename, num):
'''读取第num个字符(num不能为0)(二进制)'''
file = open(str(filename), "rb")
c = file.read(int(num))
file.close()
return str(c)[-1]
def rb_line(filename, line):
'''读取第line行(二进制)'''
file = open(str(filename), "rb")
data = file.readlines()
file.close()
return data[line-1]
def rb_lines(filename):
'''读取整个文件,以列表形式返回(二进制)'''
file = open(str(filename), "rb")
ss = file.readlines()
file.close()
return list(ss)
class Error(Exception):
pass
def writebf(filename, s='\n', mode='a',text="\n"):
'''写入filename(二进制)'''
if mode == 'w':
file = open(filename, 'wb')
file.write(str(text))
file.close()
elif mode == "a":
file = open(filename, 'ab')
file.write(str(text))
file.close()
elif mode == "x":
file = open(filename, 'xb')
file.write(str(text))
file.close()
elif mode == "r":
raise Error("io.UnsupportedOperation:函数‘writebf ’不能读文件")
else:
raise Error("ValueError:请输入有效的打开模式!(w,a,x)")
def HELP():
help("mypygame")
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn