python学习之GUI(pygame)

发布时间:2019-09-16 07:39:01编辑:auto阅读(1747)

    画一个圆形

    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()

    摆动的椭圆

关键字

上一篇: python 内联函数

下一篇: Python GIL(Global In