学到《父与子编程之旅》的16章了,跟书上的例子进行学习,学会了画圆,我又找到了画线的方法,于是就可以在screen上画日本国旗了:

Python中的画图初体验_python

手动画的不好看,也可以不手动画,直接画线:

Python中的画图初体验_python_02

当然也可以直接画个矩形套在外面:

Python中的画图初体验_python_03

代码如下:

 1 import pygame,sys  
 2 from pygame.locals import * 
 3 print pygame.ver
 4 pygame.init()
 5 screen = pygame.display.set_mode([640,480])
 6 color=(200,156,64)  
 7 points=[] 
 8 screen.fill([255,255,255])
 9 pygame.draw.circle(screen,[255,0,0],[200,200],70,0)
10 ##pygame.draw.line(screen,color,(20,20),(400,20),1)
11 ##pygame.draw.line(screen,color,(20,20),(20,400),1)
12 ##pygame.draw.line(screen,color,(20,400),(400,400),1)
13 ##pygame.draw.line(screen,color,(400,400),(400,20),1)
14 pygame.draw.rect(screen,[255,0,0],[20,20,380,380],1)
15 pygame.display.flip()
16 running = True
17 while running:
18     for event in pygame.event.get():
19         if event.type == pygame.QUIT:
20             running = False
21 ##        if event.type==MOUSEBUTTONDOWN:  
22 ##            points.append(event.pos)
23 ##    if len(points)>1:  
24 ##        pygame.draw.lines(screen,color, False, points, 5)
25 ##    pygame.display.update()  
26 pygame.quit()

 下面画一个“艺术品”,100个随机颜色随机大小的矩形,这也是书上的例子,不过让我改了一种写法,熟悉一下Python:

Python中的画图初体验_python_04

 1 import pygame,sys,random
 2 from pygame.color import THECOLORS
 3 class draw:
 4     def drawThePic(self):
 5         pygame.init()
 6         screen = pygame.display.set_mode([640,480])
 7         screen.fill([255,255,255])   
 8         for i in range(100):
 9             forRect = []
10             for j in range(4):
11                     forRect.append(random.randint(0,250))
12             pygame.draw.rect(screen,self.chooseColor(),forRect,2)
13         pygame.display.flip()
14         running = True
15         while running:
16             for event in pygame.event.get():
17                 if event.type == pygame.QUIT:
18                     running = False
19         pygame.quit()
20     def chooseColor(self):
21         color_name = random.choice(THECOLORS.keys())
22         color = THECOLORS[color_name]
23         return color
24 drawer = draw()
25 drawer.drawThePic()

上面这段程序需要注意一下,刚接触Python,调试中总会出现问题,比如改变列表中的变量的值的时候并不改变列表的值,还有方法定义的时候一定要在括号里加上self,还有调用的时候也要在前面加上self或者其他类的名字。

接下来,画一个正弦曲线:

Python中的画图初体验_python_05

以上是通过一个又一个小矩形拼成的不连续的正弦曲线,也可以画一个连续的正弦曲线,如下:

Python中的画图初体验_python_06

代码如下(##后面是之前画“艺术品”时的代码,#后面是不连续正弦曲线的代码):

 1 import pygame,sys,random
 2 from pygame.color import THECOLORS
 3 import math
 4 class draw:
 5     def drawThePic(self):
 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 #            forRect = [x,y,2,2]
14 ##            for j in range(4):
15 ##                    forRect.append(random.randint(0,500))
16 #            pygame.draw.rect(screen,self.chooseColor(),forRect,2)
17         pygame.draw.lines(screen,self.chooseColor(),False,plotPoints,1)
18         pygame.display.flip()
19         running = True
20         while running:
21             for event in pygame.event.get():
22                 if event.type == pygame.QUIT:
23                     running = False
24         pygame.quit()
25     def chooseColor(self):
26         color_name = random.choice(THECOLORS.keys())
27         color = THECOLORS[color_name]
28         return color
29 drawer = draw()
30 drawer.drawThePic()

至此,我已经学会了画线,画正弦曲线,画圆,画矩形,um……附上一个“艺术品”的图片:

Python中的画图初体验_python_07

接下来,我们来做些背景图片:

Python中的画图初体验_python_08

 代码如下:

 1 import pygame, sys
 2 class ImageRun:
 3     def letImageRun(self):
 4         pygame.init()
 5         screen = pygame.display.set_mode([640,480])
 6         screen.fill([255,255,255])
 7         background = pygame.image.load("C:/Users/dswu/Desktop/back.jpg")
 8         screen.blit(background,(0,0))
 9         my_image = pygame.image.load("C:/Users/dswu/Desktop/fuck16.jpg")
10         screen.blit(my_image, [50,50])
11         pygame.display.flip()
12         running = True
13         while running:
14             for event in pygame.event.get():
15                 if event.type ==pygame.QUIT:
16                     running = False
17         pygame.quit()          
18 runImage = ImageRun()
19 runImage.letImageRun()

 在此基础上我们就可以做个小动画了,让图片动起来:

Python中的画图初体验_python_09

以下代码能够让妙蛙种子左右来回运动(碰壁就反弹):

 1 import pygame, sys
 2 class ImageRun:
 3     def letImageRun(self):
 4         pygame.init()
 5         screen = pygame.display.set_mode([640,480])
 6         screen.fill([255,255,255])
 7         background = pygame.image.load("C:/Users/dswu/Desktop/back.png")
 8         screen.blit(background,(0,0))
 9         my_image = pygame.image.load("C:/Users/dswu/Desktop/player.png")
10         x = 400
11         y = 50
12         x_speed = 5
13         screen.blit(my_image, [x,y])
14         pygame.display.flip()
15         running = True
16         while running:
17             for event in pygame.event.get():
18                 if event.type ==pygame.QUIT:
19                     running = False
20             #Let the image run.
21             for looper in range(1,100):    
22                 pygame.time.delay(20)
23                 background = pygame.image.load("C:/Users/dswu/Desktop/back.png")
24                 screen.blit(background,(0,0))
25                 pygame.display.flip()
26                 pygame.time.delay(1)
27                 x = x + x_speed
28                 if x < 0 or x > screen.get_width() - my_image.get_width():
29                     x_speed = - x_speed
30                 screen.blit(my_image,[x,y])             
31                 pygame.display.flip()
32         pygame.quit()          
33 runImage = ImageRun()
34 runImage.letImageRun()

接下来……