python学习6

本章主要讲述了pygame的应用,pygame主要用于制作游戏模块。
除了该模块,我们还学习了Turtle模块用于画图,Tkinter用于制作GUI

1.pygame的安装
pygame的下载链接,该连接时第三方库,都是编译好了的。

然后下载好了.whl安装包,最后进入cmd命令窗口,cd到安装包的路径,然后输入
pip install xxx.whl就可以进行安装了

这个时候在Jupyter notebook中输入import pygame就可以导入pygame模块,并应用了。

2.画一个圆形

#主要的三个模块,Turtle用来绘制图形,Tkinter用来创建GUI,PyGame用来创建游戏

import pygame
pygame.init()

#定义一个窗口400*300
windowSize = [400 , 300]
screen = pygame.display.set_mode(windowSize)

#定义一个圆
pygame.display.set_caption('circles')
colour=pygame.color.Color('#FFFFFF')

#循环画圆
done=False
while not done:
    pygame.draw.circle(screen,colour,[200,150],50)
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True
pygame.quit()

案列:画一个矩形

#创建一个长方形
import pygame
pygame.init()

windowSize=(400,300)
screen=pygame.display.set_mode(windowSize)
colour=pygame.color.Color('#FE12AD')

done=False
while not done:#while not ..的意思就是当done是False的时候执行下列语句,while done就是当done是True的时候执行下列语句
    pygame.draw.rect(screen,colour,[10,20,30,40])
    pygame.display.flip()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True

pygame.quit()

案列:绘制一个矩形彩虹条

#创建一个长方形
import pygame
pygame.init()

height=300
width=400
windowSize=(400,300)
screen=pygame.display.set_mode(windowSize)
colour=pygame.color.Color('#FE12AD')

row=0
done=False
while not done:#while not ..的意思就是当done是False的时候执行下列语句,while done就是当done是True的时候执行下列语句
    increment=255/100
    while row<=height:
        pygame.draw.rect(screen,colour,[0,row,width,row + increment])
        pygame.display.flip()
        if colour[2]+increment<255:
            colour[2]+=increment
        row+=increment

#想要关闭窗口        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done=True

pygame.quit()

案列:划分矩形为若干小矩形,然后随机填充颜色

#创建一个颜色栅格

#导入需要用的模块,并且初始化
import pygame
import random
pygame.init()


#定义窗口的大小,并且划分窗口为10*10块
width=400
height=300
wsqr=10
hsqr=10
w=width/wsqr
h=height/hsqr
windowSize=(width,height)
screen=pygame.display.set_mode(windowSize)

clock=pygame.time.Clock()


#随机颜色,随机x,y的位置,画图pygame.draw.rect(screen,(r,g,b),(x,y,width,height))
done=False
while not done:
    red=random.randrange(0,256)
    green=random.randrange(0,256)
    blue=random.randrange(0,256)

    x=random.randrange(0,width,w)
    y=random.randrange(0,height,h)
    #画图形
    pygame.draw.rect(screen,(red,green,blue),(x,y,w,h))
    pygame.display.flip()

#关闭窗口循环
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done= True
    clock.tick(10)

#关闭
pygame.quit()

效果图:
这里写图片描述

注意:这里random.randrange(0,100,5)用于随机接受0——100以内以5为步长的随机数,如0,5,10,15………………

案列:不断放缩的一个椭圆

import pygame
import math
pygame.init()

windowSize=[400,300]
screen=pygame.display.set_mode(windowSize)
clock=pygame.time.Clock()

width=200
height=200
x=windowSize[0]/2-width/2
y=windowSize[1]/2-height/2
colour=pygame.color.Color('#FEA234')
black=pygame.color.Color('#12A3F5')

count=0
done=False
fileNo=0
while not done:
    screen.fill(black)
    pygame.draw.ellipse(screen,colour,[x,y,width,height])
    width+=math.cos(count)*10
    x-=(math.cos(count)*10)/2
    height+=math.sin(count)*10
    y-=(math.sin(count)*10)/2
    count+=0.5
    pygame.display.flip()

    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            done=True
    clock.tick(24)

#保存一幅图像
pygame.image.save(screen,"i.png")

pygame.quit()

案列:如上,保存一系列图像

import pygame
import math
pygame.init()

windowSize=[400,300]
screen=pygame.display.set_mode(windowSize)
clock=pygame.time.Clock()

width=200
height=200
x=windowSize[0]/2-width/2
y=windowSize[1]/2-height/2
colour=pygame.color.Color('#FEA234')
black=pygame.color.Color('#12A3F5')

count=0
done=False
fileNo=0
while not done:
    screen.fill(black)
    pygame.draw.ellipse(screen,colour,[x,y,width,height])
    width+=math.cos(count)*10
    x-=(math.cos(count)*10)/2
    height+=math.sin(count)*10
    y-=(math.sin(count)*10)/2
    count+=0.5
    pygame.display.flip()
#保存一系列的图像
    if fileNo<20:
        pygame.image.save(screen,"circle"+str(fileNo)+".jpg")
        fileNo+=1
#退出窗口
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            done=True
    clock.tick(24)

#保存一幅图像
#pygame.image.save(screen,"i.png")

pygame.quit()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值