|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 wuqramy 于 2020-3-31 18:56 编辑
今天我们来用Pygame画图。
首先下载Pygame第三方库:
在cmd中输入以下命令
然后上代码(今天没有附件哦):
首先我们画三个不同的正方形:
- import pygame
- import sys
- from pygame.locals import *
- pygame.init()
- WHITE = (255,255,255)
- BLACK = (0,0,0)
- size = whidth,height = 640,150
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('drawing')
- clock = pygame.time.Clock()
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
- screen.fill(WHITE)
- pygame.draw.rect(screen,BLACK,(50,50,150,50),0)
- pygame.draw.rect(screen,BLACK,(250,50,150,50),1)
- pygame.draw.rect(screen,BLACK,(450,50,150,50),10)
- pygame.display.flip()
- clock.tick(10)
复制代码
效果如下:
然后是一条鱼:
- import pygame
- import sys
- from pygame.locals import *
- pygame.init()
- WHITE = (255,255,255)
- BLACK = (0,0,0)
- GREEN = (0,255,0)
- points = [(200,75),(300,25),(400,75),(450,25),(450,125),(400,75),(300,125)]
- size = whidth,height = 640,200
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('drawing')
- clock = pygame.time.Clock()
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
- screen.fill(WHITE)
- pygame.draw.polygon(screen,GREEN,points,0)
- pygame.display.flip()
- clock.tick(10)
复制代码
效果如下:
接着:
- import pygame
- import sys
- from pygame.locals import *
- pygame.init()
- WHITE = (255,255,255)
- BLACK = (0,0,0)
- GREEN = (0,255,0)
- RED = (255,0,0)
- BLUE = (0,0,255)
- size = whidth,height = 620,480
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('drawing')
- position = size[0] // 2,size[1] // 2
- moving = False
- clock = pygame.time.Clock()
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
- if event.type == MOUSEBUTTONDOWN:
- if event.button == 1:
- moving = True
- if event.type == MOUSEBUTTONUP:
- if event.button == 1:
- moving = False
- if moving:
- position = pygame.mouse.get_pos()
- screen.fill(WHITE)
- pygame.draw.circle(screen,RED,position,25,1)
- pygame.draw.circle(screen,GREEN,position,75,1)
- pygame.draw.circle(screen,BLUE,position,125,1)
- pygame.display.flip()
- clock.tick(120)
复制代码
画出了个靶子:
嘿!这个靶子还能移动!:
这里还剩下了两段代码,大家可以把运行结果留言在评论区:
代码1:
- import pygame
- import sys
- from pygame.locals import *
- pygame.init()
- WHITE = (255,255,255)
- BLACK = (0,0,0)
- GREEN = (0,255,0)
- RED = (255,0,0)
- BLUE = (0,0,255)
- size = whidth,height = 620,300
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('drawing')
- position = size[0] // 2,size[1] // 2
- clock = pygame.time.Clock()
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
- screen.fill(WHITE)
- pygame.draw.ellipse(screen,BLACK,(100,100,440,100),1)
- pygame.draw.ellipse(screen,BLACK,(220,50,200,200),1)
- pygame.display.flip()
- clock.tick(120)
复制代码
代码2:
- import pygame
- import sys
- from pygame.locals import *
- pygame.init()
- WHITE = (255,255,255)
- BLACK = (0,0,0)
- GREEN = (0,255,0)
- points = [(200,75),(300,25),(400,75),(450,25),(450,125),(400,75),(300,125)]
- size = whidth,height = 640,480
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption('drawing')
- clock = pygame.time.Clock()
- while True:
- for event in pygame.event.get():
- if event.type == QUIT:
- sys.exit()
- screen.fill(WHITE)
- pygame.draw.lines(screen,GREEN,1,points,1)
- pygame.draw.line(screen,BLACK,(100,200),(540,250),1)
- pygame.draw.aaline(screen,BLACK,(100,250),(540,300),1)
- pygame.draw.aaline(screen,BLACK,(100,300),(540,350),0)
- pygame.display.flip()
- clock.tick(10)
复制代码
嗯,今天就到这里了哦! |
|