pygame入门之四:线的艺术

太极

0929-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import math
import pygame

white=255,255,255
black=0,0,0

pygame.init()
pygame.display.set_caption("TaiChi")
screen = pygame.display.set_mode([800,800])
screen.fill(white)

def draw():
pygame.draw.circle(screen,black,[400,400],300,0)
pygame.draw.rect(screen, white,[100,400,700,400],0)
pygame.draw.circle(screen,black,[250,400],150,0)
pygame.draw.circle(screen,white,[550,400],150,0)
pygame.draw.circle(screen,white,[225,400],50,0)
pygame.draw.circle(screen,black,[575,400],50,0)
pygame.draw.arc(screen,black,[100,100,600,600],math.pi,2*math.pi,1)

is_running = True
while is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
if event.type == pygame.KEYDOWN and event.key == ord('s'): # 按s保存图片
pygame.image.save(screen, 'TaiChi.jpg')
draw()
pygame.display.update()
pygame.quit()

分形树

0929-5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import pygame
import math
import random

white=255,255,255
black=0,0,0

pygame.init()
pygame.display.set_caption("Tree")
pygame.display.set_mode([600,600])
screen = pygame.display.get_surface()
clock = pygame.time.Clock()

def draw(depth=10,angle=-90, x=300, y=600):
if depth:
x1,y1=x,y
x2,y2 = x1 + int(math.cos(math.radians(angle)) * depth * 10.0),y1 + int(math.sin(math.radians(angle)) * depth * 10.0)
color=(random.randint(0,255),random.randint(0,255),random.randint(0,255))
pygame.draw.line(screen, color, (x1, y1), (x2, y2), 2)
draw(depth - 1,angle - 20, x2, y2,)
draw(depth - 1,angle + 20, x2, y2, )

is_running = True
depth=1

while is_running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
is_running = False
if event.type == pygame.KEYDOWN and event.key == ord('s'): # 按s保存图片
pygame.image.save(screen, 'Tree.jpg')
depth+=1
if depth>10:
depth%=10
screen.fill(black) # 清屏
draw(depth)
clock.tick(5)
pygame.display.update()
pygame.quit()
不要打赏,只求关注呀QAQ