用户:
兴奋的木叶龙mC2查看:0 回复:0 评论:0 创建时间:2021-12-12T16:29:13
【作品展示】

【作品介绍】
这是一个……算了,发了这么多版本与介绍了
就不必写了吧……
简单说,这是一个能辅助你进行pygame编程的模块
里面有详细介绍,自己去看吧
注意:此版本是测试版,欢迎大家测试,发现问题希望能回帖,谢谢!
此版本无法在海龟编辑器上正常运行,请在官方python上运行!
【作品源代码】
from time import sleep as wait
import pygame
import sys
import random
import time
import io
import datetime
def Quit():
'''直接退出程序'''
pygame.quit()
sys.exit()
pygame.init()
class Error(Exception):
pass
def error(e,isexit=True,second=3):
'''可以实现自定义报错,内容为e,second秒自动退出(为-1为按下enter退出)'''
print(str(e)+"\n",flush=False,end="",file=sys.stderr)
if isexit:
if second==-1:
print("Press Enter to exit.",file=sys.stderr)
input()
Quit()
if type(second)==int or type(second)==float:
print(second,"second to auto exit",file=sys.stderr)
wait(second)
Quit()
Quit()
from pygame.locals import *
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
class colours:
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)
colors=colours
#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,outline=0,outcolor=colors.white):
'''画方形'''
recto=pygame.Rect(x-outline,y-outline,width+outline*2,height+outline*2)
rect1=pygame.Rect(x,y,width,height)
pygame.draw.rect(window,outcolor,recto)
pygame.draw.rect(window,color,rect1)
pygame.display.update()
def circle(window,color,coords,radius,width=None,outline=0,outcolor=colors.white):
'''在指定的中心点画圆,如果width为none,画实心圆'''
pygame.draw.circle(window,outcolor,coords,radius+outline)
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,outline=0,outcolor=colors.white,text="",textsize=30,textcolor=colors.black,textfile=None):
'''在指定的中心点画方形'''
recto=pygame.Rect(x-width//2-outline,y-height//2-outline,width+outline*2,height+outline*2)
rect1=pygame.Rect(x-width//2,y-height//2,width,height)
pygame.draw.rect(window,outcolor,recto)
pygame.draw.rect(window,color,rect1)
centext(window,str(text),x,y,textsize,textcolor,textfile)
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,text,centerx,centery,size=40,color=(0,0,0),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()
centext = Systext
def printtext(window,text,x,y,size=40,color=(255,255,255),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()
mouse_px=lambda:pygame.mouse.get_pos()[0]
mouse_py=lambda:pygame.mouse.get_pos()[1]
m_right=lambda:pygame.mouse.get_pressed()[2]
m_middle=lambda:pygame.mouse.get_pressed()[1]
m_left=lambda:pygame.mouse.get_pressed()[0]
mouse_r=lambda:pygame.mouse.get_rel()[0]
def m_click(x,y,height,width,key=m_left):
'''检测鼠标key键是否点击x,y,height,width所形成的区域(key选项:m_right,m_middle,m_left(不加引号))'''
if key():
if mouse_px() >x and mouse_px()y and mouse_py()0:
pygame.quit()
sys.exit()
pygame.init()
except KeyboardInterrupt:
error("Find error:KeyboardInterrupt")
CheckQUIT=checkQUIT
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)]=newvalue
return tuple(list1)
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)
def writebf(filename,mode='a',text="\r\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():
print("\n本模块导入了sys,random,time,datetime,pygame,io模块,以及pygame.locals里所有内容。","使用前,请先创建一个窗口(可以用initwindow()创建)。","版本:v21.12.12",sep='\n')
print("新版功能:\n1.修复已知bug\n2.新增类:Button,Time\n3.新增函数c_area,c_girth,demo,rect,circle等等,常量pi。\n4.优化error,mouse_px,CheckQUIT()等函数。\n5.修复HELP函数",file=sys.stderr)
print("导入完毕,开始使用吧!若需要帮助请调用HELP()")
#demo()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn