💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
[TOC] ### 课前准确: * 在`Windows`系统里安装`python`,参考 [安装python](https://www.kancloud.cn/k12edu/k_12/785427#Windowspython_4) ***** ### 本节课参考资料 * `Teach Your Kids to Code.pdf` 第八章:计数器和动漫 ***** ### 一个简单的静态动漫 ``` #chapter8_1.py import pygame #导入动漫工具pygame pygame.init() #初始化,相当于打开电视 screen = pygame.display.set_mode([800,600]) #建立一个动漫基地 keep_going = True GREEN = (0,255,0) # RGB color triplet for GREEN #给一个球涂上绿色 radius = 50 #定义球的大小 while keep_going: #检查是不是要继续动漫 for event in pygame.event.get(): if event.type == pygame.QUIT: #从动漫基地寻找是不是要退出的信号 keep_going = False # 如果有退出的信号,告诉keep_going pygame.draw.circle(screen, GREEN, (100,100), radius) # 画球 pygame.display.update() pygame.quit() #关闭动漫 ``` ![](https://box.kancloud.cn/c806a601d2b64a6cbc26b17256d26aa8_527x384.png) ***** ### 复杂一点的静态笑脸动漫 ``` #chapter8_2 import pygame pygame.init() screen = pygame.display.set_mode([800,600]) keep_going = True pic = pygame.image.load("CrazySmile.bmp") #和上面那个例子的区别是,我们不再画图,而是直接导入一张笑脸 while keep_going: for event in pygame.event.get(): if event.type == pygame.QUIT: keep_going = False screen.blit(pic, (100,100)) pygame.display.update() pygame.quit() # Exit ``` ![](https://box.kancloud.cn/914254cda5c9c9d567214dfd23377364_434x365.png) ***** ### 一个会动的笑脸动漫 ``` #chapter8_3.py import pygame # Setup pygame.init() screen = pygame.display.set_mode([600,600]) keep_going = True pic = pygame.image.load("CrazySmile.bmp") colorkey = pic.get_at((0,0)) pic.set_colorkey(colorkey) picx = 0 picy = 0 BLACK = (0,0,0) timer = pygame.time.Clock() # Timer for animation while keep_going: # Game loop for event in pygame.event.get(): if event.type == pygame.QUIT: keep_going = False picx += 1 # 移动笑脸 picy += 1 # 移动笑脸 screen.fill(BLACK) # 用黑板擦把笑脸移动的痕迹抹掉 screen.blit(pic, (picx,picy)) pygame.display.update() timer.tick(60) # 每秒钟显示60幅画面 pygame.quit() ``` ![](https://box.kancloud.cn/f1bc6ce7b757a3f70c573e025bf0d62d_456x447.png) ***** ### 一个加速了、可以回弹的笑脸动漫 ``` #chapter8_4.py import pygame # Setup pygame.init() screen = pygame.display.set_mode([600,600]) keep_going = True pic = pygame.image.load("CrazySmile.bmp") colorkey = pic.get_at((0,0)) pic.set_colorkey(colorkey) picx = 0 picy = 0 BLACK = (0,0,0) timer = pygame.time.Clock() speed = 5 #给动漫加速 while keep_going: # Game loop for event in pygame.event.get(): if event.type == pygame.QUIT: keep_going = False picx += speed picy += speed if picx <= 0 or picx + pic.get_width() >= 600: speed = -speed # 回弹 screen.fill(BLACK) screen.blit(pic, (picx,picy)) pygame.display.update() timer.tick(60) pygame.quit() ``` ![](https://box.kancloud.cn/01b8dc1f00d1e04bde1f697cdad78eab_446x448.png) ***** ### 一个更加活跃的笑脸动漫 ``` #chapter8_5 import pygame # Setup pygame.init() screen = pygame.display.set_mode([800,600]) keep_going = True pic = pygame.image.load("CrazySmile.bmp") colorkey = pic.get_at((0,0)) pic.set_colorkey(colorkey) picx = 0 picy = 0 BLACK = (0,0,0) timer = pygame.time.Clock() speedx = 5 speedy = 5 while keep_going: # Game loop for event in pygame.event.get(): if event.type == pygame.QUIT: keep_going = False picx += speedx picy += speedy if picx <= 0 or picx + pic.get_width() >= 800: speedx = -speedx if picy <= 0 or picy + pic.get_height() >= 600: speedy = -speedy screen.fill(BLACK) screen.blit(pic, (picx, picy)) pygame.display.update() timer.tick(60) pygame.quit() # Exit ``` ![](https://box.kancloud.cn/9e58c60c8bee8e255212d993087b0279_593x444.png) ***** ### 课程小结 `pygame` 是动漫开发工具,使用这个工具我们可以知道动漫是怎么实现的。