pygame 画图

抽象派大师

 1 #coding=utf8
 2 
 3 import pygame,sys,random
 4 from pygame.color import THECOLORS
 5 
 6 class Draw:
 7     def drawThePic(self):
 8         pygame.init()
 9         screen = pygame.display.set_mode([640,480])
10         screen.fill([255,255,255])
11 
12         for i in range(100):
13             forRect = []
14             for j in range(4):
15                 forRect.append(random.randint(0,250))
16             pygame.draw.rect(screen,self.chooseColor(),forRect,1)
17 
18             forCircle = []
19             r = random.randint(0,50)
20             for k in range(2):
21                 forCircle.append(random.randint(0,600))
22             pygame.draw.circle(screen,self.chooseColor(),forCircle,r,0)
23         pygame.display.flip()
24 
25         running = True
26         while running:
27             for event in pygame.event.get():
28                 if event.type == pygame.QUIT:
29                     running = False
30         pygame.quit()
31 
32     def chooseColor(self):
33         color_name = random.choice(THECOLORS.keys())
34         color = THECOLORS[color_name]
35         return color
36 
37 drawer = Draw()
38 drawer.drawThePic()

正弦

 1 #conding=utf8
 2 
 3 import pygame,sys
 4 import math
 5 
 6 pygame.init()
 7 screen = pygame.display.set_mode([640,480])
 8 screen.fill([255,255,255])
 9 plotPoints = []
10 for x in range(0,640):
11     y = int(math.sin(x / 640.0 * 4 * math.pi) * 200 + 240)
12     plotPoints.append([x, y])
13 #   pygame.draw.rect(screen,[0,0,0],[x,y,1,1],3)
14 pygame.draw.lines(screen, [0,0,0], False, plotPoints, 1)
15 
16 pygame.display.flip()
17 
18 while True:
19     for event in pygame.event.get():
20         if event.type == pygame.QUIT:
21             sys.exit()

点连成线

 1 #coding=utf8
 2 
 3 import pygame,sys
 4 pygame.init()
 5 
 6 dots = [[221,432],[225,331],[133,342],[141,310],
 7         [51 ,230],[74 ,217],[58 ,153],[114,164],
 8         [123,135],[176,190],[159, 77],[193, 93],
 9         [230, 28],[267, 93],[301, 77],[284,190],
10         [327,135],[336,164],[402,153],[386,217],
11         [409,230],[319,310],[327,342],[233,331],
12         [237,432]]
13 
14 screen = pygame.display.set_mode([640,480])
15 screen.fill([255,255,255])
16 pygame.draw.lines(screen,[255,0,0],True,dots,2)
17 pygame.display.flip()
18 
19 while True:
20     for event in pygame.event.get():
21         if event.type == pygame.QUIT:
22             sys.exit()

 

水平位移小球

 1 #coding=utf8
 2 
 3 import pygame,sys
 4 pygame.init()
 5 
 6 screen = pygame.display.set_mode([640,480])
 7 screen.fill([255,255,255])
 8 my_ball = pygame.image.load("beach_ball.png")
 9 x = 50
10 y = 50
11 x_speed = 2
12 
13 while True:
14     for event in pygame.event.get():
15         if event.type == pygame.QUIT:
16             sys.exit()
17 
18     pygame.time.delay(20)
19     pygame.draw.rect(screen,[255,255,255],[x,y,90,90],0)
20     x = x + x_speed
21     if x > screen.get_width() - 90 or x < 0:
22         x_speed = - x_speed
23     screen.blit(my_ball,[x,y])
24     pygame.display.flip()

水平+垂直运动

 1 #coding=utf8
 2 
 3 import pygame,sys
 4 
 5 pygame.init()
 6 
 7 screen = pygame.display.set_mode([640,480])
 8 screen.fill([255,255,255])
 9 my_ball = pygame.image.load("beach_ball.png")
10 x = 50
11 y = 50
12 x_speed = 10
13 y_speed = 10
14 
15 
16 while True:
17     for event in pygame.event.get():
18         if event.type == pygame.QUIT:
19             sys.exit()
20     pygame.time.delay(20)
21     pygame.draw.rect(screen,[255,255,255],[x,y,90,90],0)
22     x = x + x_speed
23     y = y + y_speed
24     if x > screen.get_width() - 90 or x < 0:
25         x_speed = - x_speed
26     if y > screen.get_height() - 90 or y < 0:
27         y_speed = - y_speed
28     # if x > screen.get_width():
29     #     x = -90
30     #     y = y + 45
31     # if y > screen.get_height()-45:
32     #     x = 50
33     #     y = 50
34     screen.blit(my_ball,[x,y])
35 
36     pygame.display.flip()
原文地址:https://www.cnblogs.com/zhuzhuqwa/p/10026229.html