python学习之GUI(pygame)
短信预约 -IT技能 免费直播动态提醒
画一个圆形
FFFFFF是白色,000000是黑色
import pygame
pygame.init()
windowSize=[400,300]
screen=pygame.display.set_mode(windowSize)
pygame.display.set_caption("CircleGame")
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)
pygame.display.set_caption("RectGame")
colour=pygame.color.Color("#0A32F4")
done=False
while not done:
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()
width=400
height=300
windowSize=[width,height]
screen=pygame.display.set_mode(windowSize)
colour=pygame.color.Color('#646400')
row=0
done=False
while not done:
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 random
import pygame
pygame.init()
width=400
height=300
windowSize=[width,height]
screen=pygame.display.set_mode(windowSize)
clock=pygame.time.Clock()
sqrW=width/10
sqrH=height/10
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,sqrW)
y=random.randrange(0,height,sqrH)
pygame.draw.rect(screen,(red,green,blue),(x,y,sqrW,sqrH))
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
done=True
clock.tick(10)
pygame.quit()
tick函数控制循环的速度,它确定循环每秒重复的次数
创建椭圆
import math
import pygame
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('#57B0F6')
black=pygame.color.Color('#000000')
count=0
done=False
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)/2
count+=0.5
pygame.display.flip()
for event in pygame.event.get():
if event.type==pygame.QUIT:
done=True
clock.tick(1000)
pygame.quit()
摆动的椭圆
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341