多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] ### 课前准确: * 在`Windows`系统里安装`python`,参考 [安装python](https://www.kancloud.cn/k12edu/k_12/785427#Windowspython_4) ***** ### 本节课参考资料 * `Teach Your Kids to Code.pdf` 第十章:人类最早的电子游戏 ***** ### 1972 年电子游戏诞生 ![](https://box.kancloud.cn/e5faf947a6e9db5aa08361e179d9acfe_463x363.png) 这张图是人类最早期的游戏,距离现在有40多年了。今天我们也重温一下那些40年前的惊喜。当然经过40多年的进步,我们可以把这个游戏画得更好看一些。 ***** ### 打笑脸游戏基础版 ``` # SmileyPong1.py import pygame pygame.init() screen = pygame.display.set_mode([800,600]) pygame.display.set_caption("Smiley Pong") keepGoing = 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) WHITE = (255,255,255) timer = pygame.time.Clock() speedx = 5 speedy = 5 paddlew = 200 paddleh = 25 paddlex = 300 paddley = 550 picw = 100 pich = 100 points = 0 lives = 5 font = pygame.font.SysFont("Times", 24) while keepGoing: # Game loop for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False picx += speedx picy += speedy if picx <= 0 or picx + pic.get_width() >= 800: speedx = -speedx if picy <= 0: speedy = -speedy if picy >= 500: lives -= 1 speedy = -speedy screen.fill(BLACK) screen.blit(pic, (picx, picy)) # 画木板 paddlex = pygame.mouse.get_pos()[0] paddlex -= paddlew/2 pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh)) # Check for paddle bounce if picy + pich >= paddley and picy + pich <= paddley + paddleh \ and speedy > 0: if picx + picw / 2 >= paddlex and picx + picw / 2 <= paddlex + \ paddlew: points += 1 speedy = -speedy # 在屏幕上写上还能复活几次,得了多少分 draw_string = "Lives: " + str(lives) + " Points: " + str(points) text = font.render(draw_string, True, WHITE) text_rect = text.get_rect() text_rect.centerx = screen.get_rect().centerx text_rect.y = 10 screen.blit(text, text_rect) pygame.display.update() timer.tick(60) pygame.quit() # Exit ``` ![](https://box.kancloud.cn/8d68f8572e85d12d4fe67c50e4369ab7_465x365.png) ***** ### 打笑脸游戏升级版 在基础版再添加一些功能: * 如果复活次数用完了,就显示游戏结束 * 增加一个可以重新开始游戏的快捷键 * 重新开始时,球的速度从一个很慢的速度开始 * 球不能卡在屏幕的地板上(上节课同学们发现了这个bug) ``` # SmileyPong2.py import pygame # Setup pygame.init() screen = pygame.display.set_mode([800,600]) pygame.display.set_caption("Smiley Pong") keepGoing = 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) WHITE = (255,255,255) timer = pygame.time.Clock() speedx = 5 speedy = 5 paddlew = 200 paddleh = 25 paddlex = 300 paddley = 550 picw = 100 pich = 100 points = 0 lives = 5 font = pygame.font.SysFont("Times", 24) while keepGoing: # Game loop for event in pygame.event.get(): if event.type == pygame.QUIT: keepGoing = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_F1: # F1 = New Game points = 0 lives = 5 picx = 0 picy = 0 speedx = 5 speedy = 5 picx += speedx picy += speedy if picx <= 0 or picx >= 700: speedx = -speedx * 1.1 if picy <= 0: speedy = -speedy + 1 if picy >= 500: lives -= 1 speedy = -5 speedx = 5 picy = 499 screen.fill(BLACK) screen.blit(pic, (picx, picy)) # Draw paddle paddlex = pygame.mouse.get_pos()[0] paddlex -= paddlew/2 pygame.draw.rect(screen, WHITE, (paddlex, paddley, paddlew, paddleh)) # Check for paddle bounce if picy + pich >= paddley and picy + pich <= paddley + paddleh \ and speedy > 0: if picx + picw/2 >= paddlex and picx + picw/2 <= paddlex + \ paddlew: speedy = -speedy points += 1 # Draw text on screen draw_string = "Lives: " + str(lives) + " Points: " + str(points) # Check whether the game is over if lives < 1: speedx = speedy = 0 draw_string = "Game Over. Your score was: " + str(points) draw_string += ". Press F1 to play again. " text = font.render(draw_string, True, WHITE) text_rect = text.get_rect() text_rect.centerx = screen.get_rect().centerx text_rect.y = 10 screen.blit(text, text_rect) pygame.display.update() timer.tick(60) pygame.quit() # Exit ``` ![](https://box.kancloud.cn/7cb671164a41468202ac976af6909461_475x373.png) ### 课程小结 这节课我们仍然使用 `pygame` 完成一些有趣的游戏,包括: * 仿照打乒乓球做一个游戏 * 显示游戏当前的状态和结果 * 结束一个游戏以及重启一个游戏