博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python中的画图初体验
阅读量:5753 次
发布时间:2019-06-18

本文共 5444 字,大约阅读时间需要 18 分钟。

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

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

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

代码如下:

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 = True17 while running:18     for event in pygame.event.get():19         if event.type == pygame.QUIT:20             running = False21 ##        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:

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 = True15         while running:16             for event in pygame.event.get():17                 if event.type == pygame.QUIT:18                     running = False19         pygame.quit()20     def chooseColor(self):21         color_name = random.choice(THECOLORS.keys())22         color = THECOLORS[color_name]23         return color24 drawer = draw()25 drawer.drawThePic()

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

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

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

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

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 = True20         while running:21             for event in pygame.event.get():22                 if event.type == pygame.QUIT:23                     running = False24         pygame.quit()25     def chooseColor(self):26         color_name = random.choice(THECOLORS.keys())27         color = THECOLORS[color_name]28         return color29 drawer = draw()30 drawer.drawThePic()

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

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

 代码如下:

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 = True13         while running:14             for event in pygame.event.get():15                 if event.type ==pygame.QUIT:16                     running = False17         pygame.quit()          18 runImage = ImageRun()19 runImage.letImageRun()

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

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

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 = 40011         y = 5012         x_speed = 513         screen.blit(my_image, [x,y])14         pygame.display.flip()15         running = True16         while running:17             for event in pygame.event.get():18                 if event.type ==pygame.QUIT:19                     running = False20             #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_speed28                 if x < 0 or x > screen.get_width() - my_image.get_width():29                     x_speed = - x_speed30                 screen.blit(my_image,[x,y])             31                 pygame.display.flip()32         pygame.quit()          33 runImage = ImageRun()34 runImage.letImageRun()

接下来……

转载地址:http://gljkx.baihongyu.com/

你可能感兴趣的文章
iOS: Block的循环引用
查看>>
mysql实战02 | 日志系统:一条SQL更新语句是如何执行的?
查看>>
测试九 赛后感受
查看>>
ECC椭圆曲线详解(有具体实例)
查看>>
关于WechatApp学习总结
查看>>
Linux常见命令(二)
查看>>
document.write()的用法和清空的原因
查看>>
【EXLUCAS模板】【拓展卢卡斯详解】【组合数高级篇】LuoGu P4720
查看>>
PyCharm切换解释器
查看>>
一些基本的灰度变换函数
查看>>
12.12日个人工作总结
查看>>
jmp far ptr s所对应的机器码
查看>>
css详解1
查看>>
【转载】Presentation at from Yoshua Bengio
查看>>
MySQL类型转换
查看>>
HashSet HashMap 源码阅读笔记
查看>>
变量声明提升1
查看>>
UI前7天
查看>>
轻量级的Java 开发框架 Spring
查看>>
JS之路——浏览器window对象
查看>>