wuqramy 发表于 2020-3-31 18:56:24

Python学习心情记录 2020/3/31

本帖最后由 wuqramy 于 2020-3-31 18:56 编辑

今天我们来用Pygame画图。
首先下载Pygame第三方库:
在cmd中输入以下命令
pip install pygame
然后上代码(今天没有附件哦):
首先我们画三个不同的正方形:
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 // 2,size // 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 // 2,size // 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)

嗯,今天就到这里了哦!

DavidCT 发表于 2020-4-8 16:21:10

哇哇,大神!

Hello. 发表于 2020-3-31 19:04:42

帅气!

wuqramy 发表于 2020-3-31 19:05:54

Hello. 发表于 2020-3-31 19:04
帅气!

呵呵

wuqramy 发表于 2020-4-1 14:11:52

呃?@DavidCT 你没看到?

乘号 发表于 2020-4-3 08:43:55

用turtle他不香吗

wuqramy 发表于 2020-4-3 09:06:12

乘号 发表于 2020-4-3 08:43
用turtle他不香吗

不香

乘号 发表于 2020-4-3 09:08:15

wuqramy 发表于 2020-4-3 09:06
不香

因为你不会???

wuqramy 发表于 2020-4-9 09:42:40

DavidCT 发表于 2020-4-8 16:21
哇哇,大神!

没有什么啦
页: [1]
查看完整版本: Python学习心情记录 2020/3/31