前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python学习之GUI(pygame)

python学习之GUI(pygame)

作者头像
py3study
发布2020-01-06 15:36:21
7660
发布2020-01-06 15:36:21
举报
文章被收录于专栏:python3python3

画一个圆形

FFFFFF是白色,000000是黑色

代码语言:javascript
复制
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()

画矩形

代码语言:javascript
复制
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()

长方形彩虹

代码语言:javascript
复制
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()

颜色栅栏

代码语言:javascript
复制
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函数控制循环的速度,它确定循环每秒重复的次数

创建椭圆

代码语言:javascript
复制
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()

摆动的椭圆

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档