PYTHON笔记第十二章

#入门显示个窗口
import pygame#导入PYGAME库
pygame.init()#假惺惺初始化
screen=pygame.display.set_mode((640,320))#开辟窗口
pygame.display.set_caption("basic frame")#定义标题
bakcground=pygame.Surface(screen.get_size())#得到尺寸
bakcground=bakcground.convert()#屏幕转换为像素
bakcground.fill((255,255,255))#填充白色
'''
pygame.time.get_ticks—得到以毫秒为间隔的时间
pygame.time.wait—暂停程序一段时间
pygame.time.delay—暂停程序一段时间
pygame.time.set_timer—在事件队列上重复创建事件
pygame.time.Clock—创建一个对象来帮助跟踪时间
'''
clock=pygame.time.Clock()#定义时间锁
running=True#定义变量
while running:#一直跑
    clock.tick(30)#计时30MS,时间到了就进下面的循环
    for event in pygame.event.get():#扫一次得到的每个结果
        if event.type==pygame.QUIT:#如果当前项目有退出标志
            running=False#标志位清0
    screen.blit(bakcground,(0,0))#屏幕传送到左上角
    pygame.display.update()#更新显示
pygame.quit()#退出
#两球自由移动
import pygame,random,math #导入PYTHON游戏模块,随机模块,数学模块
#终于可以面向对象啦!
class Ball(pygame.sprite.Sprite):#定义球类
    dx=0
    dy=0
    x=0
    y=0
    def __init__(self,speed,srx,sry,radium,color):#构造器
        pygame.sprite.Sprite.__init__(self)
        self.x=srx
        self.y=sry
        self.image=pygame.Surface([radium*2,radium*2])
        self.image.fill((255,255,255))
        pygame.draw.circle(self.image,color,(radium,radium),radium,0)
        self.rect=self.image.get_rect()
        self.rect.center=(srx,sry)
        direction=random.randint(20,70)
        radian=math.radians(direction)
        self.dx=speed*math.cos(radian)
        self.dy=-speed*math.sin(radian)
    def update(self):#更新
        print("{} {}".format(self.x,self.y))
        self.x+=self.dx
        self.y+=self.dy
        self.rect.x=self.x
        self.rect.y=self.y
        if(self.rect.left<=0 or self.rect.right >=screen.get_width()):
            self.dx*=-1
        elif(self.rect.top<=5 or self.rect.bottom>=screen.get_height()-5):
            self.dy*=-1

allsprite=pygame.sprite.Group()#创建一个精灵组用来管理同类对象(就不能开个数组?)
ball1=Ball(8,100,100,20,(0,0,255))#定义红球
allsprite.add(ball1)#压入精灵组
ball2=Ball(6,200,250,20,(255,0,0))#定义蓝球
allsprite.add(ball2)##压入精灵组

pygame.init()#初始化
screen=pygame.display.set_mode((640,320))#开窗口
pygame.display.set_caption("basic frame")#定标题
background=pygame.Surface(screen.get_size())#获大小
background=background.convert()#屏幕翻转,好像是标准动作,不翻就不能正常显示
background.fill((255,255,255))#白色填充

clock=pygame.time.Clock()#开时钟(用于刷新)
running=True#开标志
while running:#真
    clock.tick(30)#间隔30MS
    for event in pygame.event.get():#看有没有退出的信息
        if event.type==pygame.QUIT:#有的话
            running=False#更改标志位
    screen.blit(background,(0,0))#位块传送把背景回到左上角
    ball1.update()#球一更新
    ball2.update()#球二更新
    allsprite.draw(screen)#绘制
    pygame.display.update()#更新显示
    result=pygame.sprite.collide_rect(ball1,ball2)#检测两个矩形是否有重合部分
    if result==True:#有
        print("撞上了")#输出
pygame.quit()#退出
#单球自由移动
import pygame,random,math#导入三库
pygame.init()#初始化
screen=pygame.display.set_mode((640,320))#开窗
pygame.display.set_caption("basic frame")#定标
background=pygame.Surface(screen.get_size())#获尺
background=background.convert()#转像
background.fill((255,255,255))#填白

ball=pygame.Surface((30,30))#画块
ball=ball.convert()#转换
ball.fill((255,255,255))#填白
pygame.draw.circle(ball,(0,0,255),(15,15),15,0)#画圆
rect1=ball.get_rect()#得矩
rect1.center=(320,45)#得中心
x,y=rect1.topleft#得左上角坐标
direction=random.randint(20,70)#随机得到一个值,20~70间
radian=math.radians(direction)#转换为弧度
dx=5*math.cos(radian)#由弧得到到速度横偏移
dy=-5*math.sin(radian)#由弧得到速度竖偏移

clock=pygame.time.Clock()#开钟
running=True#标志
while running:#等死
    clock.tick(30)#间隔30MS
    for event in pygame.event.get():#扫一次获得的信息
        if event.type==pygame.QUIT:#有结束信息
            running=False#打标志
    screen.blit(background,(0,0))#背景移到左上角
    x+=dx#横偏移
    y+=dy#竖偏移
    rect1.center=(x,y)#得到中心
    if(rect1.left<=0 or rect1.right>=screen.get_width()):#碰左右墙
        dx*=-1#变方向
    if(rect1.top<=5 or rect1.bottom>=screen.get_height()-5):#碰上下墙
        dy*=-1#变方向
    screen.blit(ball,rect1.topleft)#把球移到矩形左上角
    pygame.display.update()#更新显示
    print(rect1.topleft)#输出坐标

pygame.quit()#结束
#显示文本
import pygame
pygame.init()
screen=pygame.display.set_mode((640,320))
pygame.display.set_caption("basic frame")
bakcground=pygame.Surface(screen.get_size())
bakcground=bakcground.convert()
bakcground.fill((0,255,0))

font1=pygame.font.SysFont("新细明体",24)#定义字体
text1=font1.render("Chinese",True,(255,0,0),(255,255,255))#定义文本
bakcground.blit(text1,(20,10))#把文本移到(20,10)的位置
text2=font1.render("English",True,(0,0,255),(0,255,0))#同理
bakcground.blit(text2,(20,50))

screen.blit(bakcground,(0,0))#移屏
pygame.display.update()#更新

running=True#等死
while running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
pygame.quit()
#鼠标事件
import pygame#导入库
pygame.init()#初始化
screen=pygame.display.set_mode((640,320))#设置SIZE
pygame.display.set_caption("basic frame")#设置标题
background=pygame.Surface(screen.get_size())#获取SIZE
background=background.convert()#翻转
background.fill((255,255,255))#填白色

ball=pygame.Surface((30,30))#定义一个
ball=ball.convert()#修改图像(Surface 对象)的像素格式
ball.fill((255,255,255))#涂白色
pygame.draw.circle(ball,(0,0,255),(15,15),15,0)#画圆
rect1=ball.get_rect()#得到球的矩形
rect1.center=(320,45)#定义矩形的中心
x,y=rect1.topleft#得到左上角左坐
dx=3#定义变量

clock=pygame.time.Clock()#开计时器及标置位
playing=False
running=True
while running:
    clock.tick(30)
    for event in pygame.event.get():#这四行是标配
        if event.type==pygame.QUIT:
            running=False
    screen.blit(background,(0,0))#把背景传回左上角

    buttons=pygame.mouse.get_pressed()#获取当前鼠标的按下状态
    if buttons[0]:#第0位标志为1表示有左键按下
        playing=True#游戏标志位为真
    elif buttons[2]:#第2位标志为1表示右键按下
        playing=False#游戏标志位为假
    if playing==True:#如果为真
        mouses=pygame.mouse.get_pos()#就获得鼠标坐标
        rect1.centerx=mouses[0]#矩形中心X就是鼠标的X
        rect1.centery=mouses[1]#矩形中心y就是鼠标的y

    screen.blit(ball,rect1.topleft)#把球传送到矩形的左上角
    pygame.display.update()#更新

pygame.quit()#退出
#画图
import pygame
pygame.init()
screen=pygame.display.set_mode((640,320))#开窗
pygame.display.set_caption("basic frame")#定标
bakcground=pygame.Surface(screen.get_size())#获尺
bakcground=bakcground.convert()#转像
bakcground.fill((255,255,255))#填白

pygame.draw.circle(bakcground,(0,0,0),(150,150),130,4)#画圆
pygame.draw.circle(bakcground,(0,0,255),(100,120),25,0)#画圆
pygame.draw.circle(bakcground,(0,0,255),(200,120),25,0)#画圆
pygame.draw.ellipse(bakcground,(255,0,255),[135,130,30,80],0)#画椭圆
pygame.draw.arc(bakcground,(255,0,0),[80,130,150,120],3.4,6.1,9)#画弧

screen.blit(bakcground,(0,0))#屏幕移回左上角
pygame.display.update()#更新

running=True
while running:#等待退出
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
pygame.quit()
#导入图片
import pygame
pygame.init()
screen=pygame.display.set_mode((800,600))
pygame.display.set_caption("basic frame")
bakcground=pygame.Surface(screen.get_size())
bakcground=bakcground.convert()
bakcground.fill((0,255,0))#绿背景

image=pygame.image.load("img01.jpg")#加载图1
image.convert()#转为像素
bakcground.blit(image,(20,10))#把图片位块传送到点(20,10)位置
compass=pygame.image.load("img02.jpg")#下面三行同上
compass.convert()
bakcground.blit(compass,(400,50))

screen.blit(bakcground,(0,0))#下面同上一例
pygame.display.update()

running=True
while running:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
pygame.quit()
#水平移动&键盘响应
import pygame#导入库
pygame.init()#初始化
screen=pygame.display.set_mode((640,320))#开窗
pygame.display.set_caption("basic frame")#定标
background=pygame.Surface(screen.get_size())#获尺
background=background.convert()#转像
background.fill((255,255,255))#填白

ball=pygame.Surface((30,30))#定义一个图像块,宽高都是30
ball=ball.convert()#转变为像素
ball.fill((255,255,255))#填白
pygame.draw.circle(ball,(0,0,255),(15,15),15,0)#在块里画圆,注意圆心一定是15,15,然然半径取15
rect1=ball.get_rect()#画矩
rect1.center=(320,45)#定义矩形中心为320,45
x,y=rect1.topleft#得到矩形的左上角坐标
dx=3#初始化

clock=pygame.time.Clock()#开钟
running=True#标志
while running:#等待
    clock.tick(30)#刷新间隔
    for event in pygame.event.get():#等死三连
        if event.type==pygame.QUIT:
            running=False
            
    keys=pygame.key.get_pressed()#键盘点击状态获取
    if keys[pygame.K_RIGHT] and dx<0:#按了右键且当前在左移
        dx*=-1#往左移
    elif keys[pygame.K_LEFT] and dx>0:#按了左键且当前在右移
        dx*=-1#往右移
    
    screen.blit(background,(0,0))#把屏幕移到左上角
    x+=dx#偏移
    rect1.center=(x,y)#矩形中心获取
    if(rect1.left<=0 or rect1.right>=screen.get_width()):#如果矩形超边界
        dx*=-1#速度取反
    #print("bbb")    
    screen.blit(ball,rect1.topleft)#把球块移回矩形的左上角
    #print("ccc")
    pygame.display.update()
    #print(rect1.topleft)

pygame.quit()

猜你喜欢

转载自blog.csdn.net/cj1064789374/article/details/84887507