飞道的博客

Python基础-Pygame游戏框架之飞机大战

779人阅读  评论(0)

                                      Pygame游戏框架

Pygame

Pygame是一套跨平台的Python模块,专为编写游戏而设计。

它包括计算机图形和声音库,旨在与Python编程语言一起使用。

展示窗体

引入pygame模块

import pygame

初始化游戏

pygame.init()

设置窗体大小

pygame.display.set_mode((400, 400))

展示窗体-程序执行流程

展示窗体

图形化界面程序都是阻塞式的

通常阻塞的实现策略就是死循环

事件捕获之窗体关闭

窗体关闭

pygame.event.get()提供事件的清单

event.type指明了事件的类型

pygame.locals模块提供了常量

QUIT就是窗体关闭事件

 

01.pygame窗口创建.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. # 初始化pygame
  4. pygame.init()
  5. # 创建窗口
  6. # size 窗口大小 宽度和高度
  7. pygame.display.set_mode(size=( 800, 800))

02.pygame窗口一直展示.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. # 初始化pygame
  4. pygame.init()
  5. # 创建窗口
  6. # size 窗口大小 宽度和高度
  7. pygame.display.set_mode(size=( 800, 800))
  8. # 避免程序结束
  9. while True:
  10. pass

03.游戏停止.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. # 避免程序结束
  11. while True:
  12. # 处理事件
  13. eventList = pygame. event. get()
  14. # if len(eventList)>0:
  15. # 有事件发生
  16. # 判断是否是退出事件
  17. # if len(eventList)>0:
  18. # print(eventList)
  19. for event in eventList:
  20. # 退出事件类型
  21. if event.type==QUIT:
  22. # 退出界面
  23. pygame.display.quit()
  24. # sys退出
  25. sys. exit()

04.窗口标题和图标.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. # 设置窗口标题
  11. pygame.display.set_caption( '飞机大战')
  12. # 加载图标
  13. icon = pygame.image.load( 'img/app.ico')
  14. # 设置图标
  15. pygame.display.set_icon(icon)
  16. # 避免程序结束
  17. while True:
  18. # 处理事件
  19. eventList = pygame. event. get()
  20. for event in eventList:
  21. # 退出事件类型
  22. if event.type==QUIT:
  23. # 退出界面
  24. pygame.display.quit()
  25. # sys退出
  26. sys. exit()

05.展示图片到窗口.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. # 设置窗口标题
  11. pygame.display.set_caption( '飞机大战')
  12. # 加载图标
  13. icon = pygame.image.load( 'img/app.ico')
  14. # 设置图标
  15. pygame.display.set_icon(icon)
  16. # 加载飞机图片
  17. heroImg = pygame.image.load( 'img/hero2.png')
  18. # 避免程序结束
  19. while True:
  20. # 展示飞机图片
  21. window.blit(heroImg,( 40, 40))
  22. # 刷新
  23. pygame.display.flip()
  24. # 处理事件
  25. eventList = pygame. event. get()
  26. for event in eventList:
  27. # 退出事件类型
  28. if event.type==QUIT:
  29. # 退出界面
  30. pygame.display.quit()
  31. # sys退出
  32. sys. exit()

06.键盘事件处理.py

06.1 键盘按压一次触发一次


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. # 避免程序结束
  11. while True:
  12. # 处理事件
  13. eventList = pygame. event. get()
  14. for event in eventList:
  15. # 退出事件类型
  16. if event.type==QUIT: # 退出事件类型
  17. # 退出界面
  18. pygame.display.quit()
  19. # sys退出
  20. sys. exit()
  21. elif event.type==KEYDOWN: # 键盘按下的事件
  22. # 判断按下的是哪个键
  23. if event. key==K_a:
  24. print( '向左')
  25. elif event. key==K_d:
  26. print( '向右')
  27. elif event. key==K_w:
  28. print( '向前')
  29. elif event. key==K_s:
  30. print( '向后')
  31. elif event. key==K_RETURN:
  32. print( '点击了enter键')

06.2 键盘一直按压,就一直触发


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. # 避免程序结束
  11. while True:
  12. # 处理事件
  13. eventList = pygame.event.get()
  14. for event in eventList:
  15. # 退出事件类型
  16. if event.type==QUIT: # 退出事件类型
  17. # 退出界面
  18. pygame.display.quit()
  19. # sys退出
  20. sys.exit()
  21. # 返回所有键对应的状态 0:没有按压 1:有按压
  22. states = pygame.key.get_pressed() # 键盘一直按压,就一直触发
  23. # 有按压事件
  24. if 1 in states:
  25. # 是否是关心的a d s w键
  26. if states[K_a]: # 0 1
  27. plane.moveLeft()
  28. elif states[K_d]:
  29. plane.moveRight()
  30. elif states[K_w]:
  31. plane.moveUp()
  32. elif states[K_s]:
  33. plane.moveDown()

 

07.展示图片到窗口.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. # 设置窗口标题
  11. pygame.display.set_caption( '飞机大战')
  12. # 加载图标
  13. icon = pygame.image.load( 'img/app.ico')
  14. # 设置图标
  15. pygame.display.set_icon(icon)
  16. # 加载飞机图片
  17. heroImg = pygame.image.load( 'img/hero2.png')
  18. heroX = 200
  19. heroY = 200
  20. # 避免程序结束
  21. while True:
  22. # 清空窗口,通过背景颜色覆盖
  23. # R G B
  24. window.fill(( 0, 0, 0))
  25. # 展示飞机图片
  26. window.blit(heroImg,(heroX,heroY))
  27. # 刷新
  28. pygame.display.flip()
  29. # 处理事件
  30. eventList = pygame. event. get()
  31. for event in eventList:
  32. # 退出事件类型
  33. if event.type==QUIT:
  34. # 退出界面
  35. pygame.display.quit()
  36. # sys退出
  37. sys. exit()
  38. elif event.type==KEYDOWN: # 键盘按下的事件
  39. # 判断按下的是哪个键
  40. if event. key==K_a:
  41. heroX-= 10
  42. elif event. key==K_d:
  43. heroX+= 10
  44. elif event. key==K_w:
  45. heroY-= 10
  46. elif event. key==K_s:
  47. heroY+= 10

08.文字展示.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. # 加载字体
  11. font = pygame.font.Font( 'font/happy.ttf',50)
  12. # 渲染字体
  13. fontSurface = font.render( '游戏开始',True,(255,255,255))
  14. # 避免程序结束
  15. while True:
  16. # 显示字体控件
  17. window.blit(fontSurface,( 200, 200))
  18. # 刷新(只需要执行一次)
  19. pygame.display.flip()
  20. # 处理事件
  21. eventList = pygame. event. get()
  22. for event in eventList:
  23. # 退出事件类型
  24. if event.type==QUIT:
  25. # 退出界面
  26. pygame.display.quit()
  27. # sys退出
  28. sys. exit()

09.背景音效播放.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. # 加载背景音效
  11. pygame.mixer.music.load( 'snd/bomb.wav')
  12. # 避免程序结束
  13. while True:
  14. # 处理事件
  15. eventList = pygame. event. get()
  16. for event in eventList:
  17. # 退出事件类型
  18. if event.type==QUIT: # 退出事件类型
  19. # 退出界面
  20. pygame.display.quit()
  21. # sys退出
  22. sys. exit()
  23. elif event.type==KEYDOWN: # 键盘按下的事件
  24. # 判断按下的是哪个键
  25. if event. key==K_a:
  26. # 播放背景音效
  27. pygame.mixer.music.play(loops= -1)

10.特殊音效处理.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. # 加载特殊音效
  11. sn = pygame.mixer.Sound( 'snd/bomb.wav')
  12. # 避免程序结束
  13. while True:
  14. # 处理事件
  15. eventList = pygame. event. get()
  16. for event in eventList:
  17. # 退出事件类型
  18. if event.type==QUIT: # 退出事件类型
  19. # 退出界面
  20. pygame.display.quit()
  21. # sys退出
  22. sys. exit()
  23. elif event.type==KEYDOWN: # 键盘按下的事件
  24. # 判断按下的是哪个键
  25. if event. key==K_a:
  26. sn. stop()
  27. elif event. key==K_s:
  28. sn.play(loops= 0)

11.FPS计算.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. import time
  6. # 初始化pygame
  7. pygame.init()
  8. # 创建窗口
  9. # size 窗口大小 宽度和高度
  10. window = pygame.display.set_mode(size=( 800, 800))
  11. # 设置窗口标题
  12. pygame.display.set_caption( '飞机大战')
  13. # 加载图标
  14. icon = pygame.image.load( 'img/app.ico')
  15. # 设置图标
  16. pygame.display.set_icon(icon)
  17. # 加载飞机图片
  18. heroImg = pygame.image.load( 'img/hero2.png')
  19. heroX = 200
  20. heroY = 200
  21. # 避免程序结束
  22. while True:
  23. # 开始时间
  24. startTime = time.time()
  25. # 清空窗口,通过背景颜色覆盖
  26. # R G B
  27. window.fill(( 0, 0, 0))
  28. # 展示飞机图片
  29. window.blit(heroImg,(heroX,heroY))
  30. # 刷新
  31. pygame.display.flip()
  32. # 处理事件
  33. eventList = pygame. event. get()
  34. for event in eventList:
  35. # 退出事件类型
  36. if event.type==QUIT:
  37. # 退出界面
  38. pygame.display.quit()
  39. # sys退出
  40. sys. exit()
  41. elif event.type==KEYDOWN: # 键盘按下的事件
  42. # 判断按下的是哪个键
  43. if event. key==K_a:
  44. heroX-= 10
  45. elif event. key==K_d:
  46. heroX+= 10
  47. elif event. key==K_w:
  48. heroY-= 10
  49. elif event. key==K_s:
  50. heroY+= 10
  51. # 降低fps
  52. time.sleep( 0.008)
  53. # 结束时间
  54. endTime = time.time()
  55. # 这一次渲染时间
  56. renderTime = endTime-startTime
  57. try:
  58. # 求FPS
  59. fps = int( 1/renderTime)
  60. print(fps)
  61. except:
  62. pass

12.Surface.py


  
  1. # 导入游戏引擎
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. # 初始化pygame
  6. pygame.init()
  7. # 创建窗口
  8. # size 窗口大小 宽度和高度
  9. window = pygame.display.set_mode(size=( 800, 800))
  10. print(window.get_width(),window.get_height())
  11. # 设置窗口标题
  12. pygame.display.set_caption( '飞机大战')
  13. # 加载图标
  14. icon = pygame.image.load( 'img/app.ico')
  15. # 设置图标
  16. pygame.display.set_icon(icon)
  17. # 加载飞机图片
  18. heroImg = pygame.image.load( 'img/hero2.png')
  19. # 获取图片大小
  20. # print(heroImg.get_width(),heroImg.get_height())
  21. print(heroImg.get_rect())
  22. heroX = 200
  23. heroY = 200
  24. # 避免程序结束
  25. while True:
  26. # 清空窗口,通过背景颜色覆盖
  27. # R G B
  28. window.fill(( 0, 0, 0))
  29. # 展示飞机图片
  30. window.blit(heroImg,(heroX,heroY))
  31. # 刷新
  32. pygame.display.flip()
  33. # 处理事件
  34. eventList = pygame. event. get()
  35. for event in eventList:
  36. # 退出事件类型
  37. if event.type==QUIT:
  38. # 退出界面
  39. pygame.display.quit()
  40. # sys退出
  41. sys. exit()
  42. elif event.type==KEYDOWN: # 键盘按下的事件
  43. # 判断按下的是哪个键
  44. if event. key==K_a:
  45. heroX-= 10
  46. elif event. key==K_d:
  47. heroX+= 10
  48. elif event. key==K_w:
  49. heroY-= 10
  50. elif event. key==K_s:
  51. heroY+= 10

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

飞机大战

01.创建飞机大战窗口


  
  1. import pygame
  2. # 初始化
  3. pygame.init()
  4. # 创建游戏窗口
  5. window = pygame.display.set_mode(size=( 600, 800))
  6. # 保证窗口不关闭
  7. while True:
  8. pass

02.窗口处理:


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. # 初始化
  9. pygame.init()
  10. # 创建游戏窗口
  11. window = pygame.display.set_mode(size=( 600, 800))
  12. # 窗口标题
  13. pygame.display.set_caption( '飞机大战')
  14. # 加载图标图片
  15. iconSurface = pygame.image.load( 'img/app.ico')
  16. # 设置图标
  17. pygame.display.set_icon(iconSurface)
  18. # 保证窗口不关闭
  19. while True:
  20. # 处理窗口关闭
  21. eventList = pygame.event.get()
  22. for event in eventList:
  23. # 判断是否是退出类型
  24. if event.type==QUIT:
  25. # 退出游戏
  26. pygame.quit()
  27. # 退出程序
  28. sys.exit()

03.展示背景


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. # 初始化
  9. pygame.init()
  10. # 加载背景图片
  11. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  12. # 获取背景图片的大小
  13. bgWidth = bgSurface.get_width()
  14. bgHeight = bgSurface.get_height()
  15. # 创建游戏窗口
  16. window = pygame.display.set_mode(size=(bgWidth,bgHeight))
  17. # 窗口标题
  18. pygame.display.set_caption( '飞机大战')
  19. # 加载图标图片
  20. iconSurface = pygame.image.load( 'img/app.ico')
  21. # 设置图标
  22. pygame.display.set_icon(iconSurface)
  23. # 保证窗口不关闭
  24. while True:
  25. # 展示背景
  26. window.blit(bgSurface,( 0, 0))
  27. # 刷新
  28. pygame.display.flip()
  29. # 处理窗口关闭
  30. eventList = pygame.event.get()
  31. for event in eventList:
  32. # 判断是否是退出类型
  33. if event.type==QUIT:
  34. # 退出游戏
  35. pygame.quit()
  36. # 退出程序
  37. sys.exit()

04.入口函数处理


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. def start():
  9. # 初始化
  10. pygame.init()
  11. # 加载背景图片
  12. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  13. # 获取背景图片的大小
  14. bgWidth = bgSurface.get_width()
  15. bgHeight = bgSurface.get_height()
  16. # 创建游戏窗口
  17. window = pygame.display.set_mode(size=(bgWidth,bgHeight))
  18. # 窗口标题
  19. pygame.display.set_caption( '飞机大战')
  20. # 加载图标图片
  21. iconSurface = pygame.image.load( 'img/app.ico')
  22. # 设置图标
  23. pygame.display.set_icon(iconSurface)
  24. # 保证窗口不关闭
  25. while True:
  26. # 展示背景
  27. window.blit(bgSurface,( 0, 0))
  28. # 刷新
  29. pygame.display.flip()
  30. # 处理窗口关闭
  31. eventList = pygame.event.get()
  32. for event in eventList:
  33. # 判断是否是退出类型
  34. if event.type==QUIT:
  35. # 退出游戏
  36. pygame.quit()
  37. # 退出程序
  38. sys.exit()
  39. if __name__ == '__main__':
  40. start()

05.添加我方飞机


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. def start():
  9. # 初始化
  10. pygame.init()
  11. # 加载背景图片
  12. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  13. # 获取背景图片的大小
  14. bgWidth = bgSurface.get_width()
  15. bgHeight = bgSurface.get_height()
  16. # 创建游戏窗口
  17. window = pygame.display.set_mode(size=(bgWidth,bgHeight))
  18. # 窗口标题
  19. pygame.display.set_caption( '飞机大战')
  20. # 加载图标图片
  21. iconSurface = pygame.image.load( 'img/app.ico')
  22. # 设置图标
  23. pygame.display.set_icon(iconSurface)
  24. # 加载我方飞机图片
  25. heroSurface = pygame.image.load( 'img/hero2.png')
  26. # 保证窗口不关闭
  27. while True:
  28. # 展示背景
  29. window.blit(bgSurface,( 0, 0))
  30. # 展示我方飞机
  31. window.blit(heroSurface,( 200, 600))
  32. # 刷新
  33. pygame.display.flip()
  34. # 处理窗口关闭
  35. eventList = pygame.event.get()
  36. for event in eventList:
  37. # 判断是否是退出类型
  38. if event.type==QUIT:
  39. # 退出游戏
  40. pygame.quit()
  41. # 退出程序
  42. sys.exit()
  43. if __name__ == '__main__':
  44. start()

06.飞机类抽取


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 我方飞机类 ------------------"""
  13. class Plane:
  14. def __init__(self,x,y,window):
  15. # 坐标
  16. self.x = x
  17. self.y = y
  18. # 被展示到的窗口
  19. self.window = window
  20. # 飞机展示的Surface
  21. self.surface = pygame.image.load( 'img/hero2.png')
  22. def display(self):
  23. '''
  24. 被展示
  25. :return:
  26. '''
  27. # 展示我方飞机
  28. self.window.blit(self.surface, (self.x, self.y))
  29. def start():
  30. # 初始化
  31. pygame.init()
  32. # 加载背景图片
  33. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  34. # 获取背景图片的大小
  35. bgWidth = bgSurface.get_width()
  36. bgHeight = bgSurface.get_height()
  37. # 创建游戏窗口
  38. window = pygame.display.set_mode(size=(bgWidth,bgHeight))
  39. # 窗口标题
  40. pygame.display.set_caption( '飞机大战')
  41. # 加载图标图片
  42. iconSurface = pygame.image.load( 'img/app.ico')
  43. # 设置图标
  44. pygame.display.set_icon(iconSurface)
  45. # 创建飞机类
  46. plane = Plane( 200, 600,window)
  47. # 保证窗口不关闭
  48. while True:
  49. # 展示背景
  50. window.blit(bgSurface,( 0, 0))
  51. # 展示飞机
  52. plane.display()
  53. # 刷新
  54. pygame.display.flip()
  55. # 处理窗口关闭
  56. eventList = pygame.event.get()
  57. for event in eventList:
  58. # 判断是否是退出类型
  59. if event.type==QUIT:
  60. # 退出游戏
  61. pygame.quit()
  62. # 退出程序
  63. sys.exit()
  64. if __name__ == '__main__':
  65. start()

07.飞机的移动


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 我方飞机类 ------------------"""
  13. class Plane:
  14. def __init__(self, x, y, window):
  15. # 坐标
  16. self.__x = x
  17. self.__y = y
  18. # 被展示到的窗口
  19. self.__window = window
  20. # 飞机展示的Surface
  21. self.__surface = pygame.image.load( 'img/hero2.png')
  22. def display(self):
  23. '''
  24. 被展示
  25. :return:
  26. '''
  27. # 展示我方飞机
  28. self.__window.blit(self.__surface, (self.__x, self.__y))
  29. def moveLeft(self):
  30. '''
  31. 向左移动
  32. :return:
  33. '''
  34. self.__x -= 1
  35. def moveRight(self):
  36. '''
  37. 向右移动
  38. :return:
  39. '''
  40. self.__x += 1
  41. def moveUp(self):
  42. '''
  43. 向上移动
  44. :return:
  45. '''
  46. self.__y -= 1
  47. def moveDown(self):
  48. '''
  49. 向下移动
  50. :return:
  51. '''
  52. self.__y += 1
  53. def start():
  54. # 初始化
  55. pygame.init()
  56. # 加载背景图片
  57. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  58. # 获取背景图片的大小
  59. bgWidth = bgSurface.get_width()
  60. bgHeight = bgSurface.get_height()
  61. # 创建游戏窗口
  62. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  63. # 窗口标题
  64. pygame.display.set_caption( '飞机大战')
  65. # 加载图标图片
  66. iconSurface = pygame.image.load( 'img/app.ico')
  67. # 设置图标
  68. pygame.display.set_icon(iconSurface)
  69. # 创建飞机类
  70. plane = Plane( 200, 600, window)
  71. # 保证窗口不关闭
  72. while True:
  73. # 展示背景
  74. window.blit(bgSurface, ( 0, 0))
  75. # 展示飞机
  76. plane.display()
  77. # 刷新
  78. pygame.display.flip()
  79. # 处理窗口关闭
  80. eventList = pygame.event.get()
  81. for event in eventList:
  82. # 判断是否是退出类型
  83. if event.type == QUIT:
  84. # 退出游戏
  85. pygame.quit()
  86. # 退出程序
  87. sys.exit()
  88. elif event.type == KEYDOWN: # 按下事件
  89. if event.key == K_a:
  90. plane.moveLeft()
  91. elif event.key == K_d:
  92. plane.moveRight()
  93. elif event.key == K_w:
  94. plane.moveUp()
  95. elif event.key == K_s:
  96. plane.moveDown()
  97. if __name__ == '__main__':
  98. start()

08.飞机的移动细节处理


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 我方飞机类 ------------------"""
  13. class Plane:
  14. def __init__(self, x, y, window):
  15. # 坐标
  16. self.__x = x
  17. self.__y = y
  18. # 被展示到的窗口
  19. self.__window = window
  20. # 飞机展示的Surface
  21. self.__surface = pygame.image.load( 'img/hero2.png')
  22. # 运动速度
  23. self.speed = 1
  24. def display(self):
  25. '''
  26. 被展示
  27. :return:
  28. '''
  29. # 展示我方飞机
  30. self.__window.blit(self.__surface, (self.__x, self.__y))
  31. def moveLeft(self):
  32. '''
  33. 向左移动
  34. :return:
  35. '''
  36. self.__x -= self.speed
  37. def moveRight(self):
  38. '''
  39. 向右移动
  40. :return:
  41. '''
  42. self.__x += self.speed
  43. def moveUp(self):
  44. '''
  45. 向上移动
  46. :return:
  47. '''
  48. self.__y -= self.speed
  49. def moveDown(self):
  50. '''
  51. 向下移动
  52. :return:
  53. '''
  54. self.__y += self.speed
  55. def start():
  56. # 初始化
  57. pygame.init()
  58. # 加载背景图片
  59. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  60. # 获取背景图片的大小
  61. bgWidth = bgSurface.get_width()
  62. bgHeight = bgSurface.get_height()
  63. # 创建游戏窗口
  64. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  65. # 窗口标题
  66. pygame.display.set_caption( '飞机大战')
  67. # 加载图标图片
  68. iconSurface = pygame.image.load( 'img/app.ico')
  69. # 设置图标
  70. pygame.display.set_icon(iconSurface)
  71. # 创建飞机类
  72. plane = Plane( 200, 600, window)
  73. # 保证窗口不关闭
  74. while True:
  75. # 展示背景
  76. window.blit(bgSurface, ( 0, 0))
  77. # 展示飞机
  78. plane.display()
  79. # 刷新
  80. pygame.display.flip()
  81. # 处理窗口关闭
  82. eventList = pygame.event.get()
  83. for event in eventList:
  84. # 判断是否是退出类型
  85. if event.type == QUIT:
  86. # 退出游戏
  87. pygame.quit()
  88. # 退出程序
  89. sys.exit()
  90. # 处理按压事件
  91. # 返回所有键对应的状态 0:没有按压 1:有按压
  92. states = pygame.key.get_pressed()
  93. # 有按压事件
  94. if 1 in states:
  95. # 是否是关心的a d s w键
  96. if states[K_a]: # 0 1
  97. plane.moveLeft()
  98. elif states[K_d]:
  99. plane.moveRight()
  100. elif states[K_w]:
  101. plane.moveUp()
  102. elif states[K_s]:
  103. plane.moveDown()
  104. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  105. # if event.key == K_a:
  106. # plane.moveLeft()
  107. # elif event.key == K_d:
  108. # plane.moveRight()
  109. # elif event.key == K_w:
  110. # plane.moveUp()
  111. # elif event.key == K_s:
  112. # plane.moveDown()
  113. if __name__ == '__main__':
  114. start()

09.子弹类的创建


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 子弹类 ------------------"""
  13. class Bullet:
  14. def __init__(self,x,y,window):
  15. # 展示的坐标
  16. self.x = x
  17. self.y = y
  18. # 需要展示的窗口
  19. self.window = window
  20. # 展示的控件
  21. self.surface = pygame.image.load( 'img/bullet_10.png')
  22. def display(self):
  23. '''
  24. 展示控件
  25. :return:
  26. '''
  27. self.window.blit(self.surface,(self.x,self.y))
  28. """------------------ 我方飞机类 ------------------"""
  29. class Plane:
  30. def __init__(self, x, y, window):
  31. # 坐标
  32. self.__x = x
  33. self.__y = y
  34. # 被展示到的窗口
  35. self.__window = window
  36. # 飞机展示的Surface
  37. self.__surface = pygame.image.load( 'img/hero2.png')
  38. # 运动速度
  39. self.speed = 1
  40. def display(self):
  41. '''
  42. 被展示
  43. :return:
  44. '''
  45. # 展示我方飞机
  46. self.__window.blit(self.__surface, (self.__x, self.__y))
  47. def moveLeft(self):
  48. '''
  49. 向左移动
  50. :return:
  51. '''
  52. self.__x -= self.speed
  53. def moveRight(self):
  54. '''
  55. 向右移动
  56. :return:
  57. '''
  58. self.__x += self.speed
  59. def moveUp(self):
  60. '''
  61. 向上移动
  62. :return:
  63. '''
  64. self.__y -= self.speed
  65. def moveDown(self):
  66. '''
  67. 向下移动
  68. :return:
  69. '''
  70. self.__y += self.speed
  71. def start():
  72. # 初始化
  73. pygame.init()
  74. # 加载背景图片
  75. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  76. # 获取背景图片的大小
  77. bgWidth = bgSurface.get_width()
  78. bgHeight = bgSurface.get_height()
  79. # 创建游戏窗口
  80. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  81. # 窗口标题
  82. pygame.display.set_caption( '飞机大战')
  83. # 加载图标图片
  84. iconSurface = pygame.image.load( 'img/app.ico')
  85. # 设置图标
  86. pygame.display.set_icon(iconSurface)
  87. # 创建飞机类
  88. plane = Plane( 200, 600, window)
  89. # 保证窗口不关闭
  90. while True:
  91. # 展示背景
  92. window.blit(bgSurface, ( 0, 0))
  93. # 展示飞机
  94. plane.display()
  95. # 刷新
  96. pygame.display.flip()
  97. # 处理窗口关闭
  98. eventList = pygame.event.get()
  99. for event in eventList:
  100. # 判断是否是退出类型
  101. if event.type == QUIT:
  102. # 退出游戏
  103. pygame.quit()
  104. # 退出程序
  105. sys.exit()
  106. # 处理按压事件
  107. # 返回所有键对应的状态 0:没有按压 1:有按压
  108. states = pygame.key.get_pressed()
  109. # 有按压事件
  110. if 1 in states:
  111. # 是否是关心的a d s w键
  112. if states[K_a]: # 0 1
  113. plane.moveLeft()
  114. elif states[K_d]:
  115. plane.moveRight()
  116. elif states[K_w]:
  117. plane.moveUp()
  118. elif states[K_s]:
  119. plane.moveDown()
  120. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  121. # if event.key == K_a:
  122. # plane.moveLeft()
  123. # elif event.key == K_d:
  124. # plane.moveRight()
  125. # elif event.key == K_w:
  126. # plane.moveUp()
  127. # elif event.key == K_s:
  128. # plane.moveDown()
  129. if __name__ == '__main__':
  130. start()

10.发射子弹


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 子弹类 ------------------"""
  13. class Bullet:
  14. def __init__(self,x,y,window):
  15. # 展示的坐标
  16. self.x = x
  17. self.y = y
  18. # 需要展示的窗口
  19. self.window = window
  20. # 展示的控件
  21. self.surface = pygame.image.load( 'img/bullet_10.png')
  22. def display(self):
  23. '''
  24. 展示控件
  25. :return:
  26. '''
  27. self.window.blit(self.surface,(self.x,self.y))
  28. """------------------ 我方飞机类 ------------------"""
  29. class Plane:
  30. def __init__(self, x, y, window):
  31. # 坐标
  32. self.__x = x
  33. self.__y = y
  34. # 被展示到的窗口
  35. self.__window = window
  36. # 飞机展示的Surface
  37. self.__surface = pygame.image.load( 'img/hero2.png')
  38. # 运动速度
  39. self.speed = 1
  40. # 发射的子弹容器
  41. self.__bullets = []
  42. def display(self):
  43. '''
  44. 被展示
  45. :return:
  46. '''
  47. # 展示我方飞机
  48. self.__window.blit(self.__surface, (self.__x, self.__y))
  49. # 把所有发射的子弹渲染出来
  50. for bullet in self.__bullets:
  51. # 把子弹展示出来
  52. bullet.display()
  53. def fire(self):
  54. '''
  55. 发射子弹
  56. :return:
  57. '''
  58. # 创建子弹对象
  59. bullet = Bullet(self.__x,self.__y,self.__window)
  60. # 子弹对象添加到子弹列表中
  61. self.__bullets.append(bullet)
  62. def moveLeft(self):
  63. '''
  64. 向左移动
  65. :return:
  66. '''
  67. self.__x -= self.speed
  68. def moveRight(self):
  69. '''
  70. 向右移动
  71. :return:
  72. '''
  73. self.__x += self.speed
  74. def moveUp(self):
  75. '''
  76. 向上移动
  77. :return:
  78. '''
  79. self.__y -= self.speed
  80. def moveDown(self):
  81. '''
  82. 向下移动
  83. :return:
  84. '''
  85. self.__y += self.speed
  86. def start():
  87. # 初始化
  88. pygame.init()
  89. # 加载背景图片
  90. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  91. # 获取背景图片的大小
  92. bgWidth = bgSurface.get_width()
  93. bgHeight = bgSurface.get_height()
  94. # 创建游戏窗口
  95. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  96. # 窗口标题
  97. pygame.display.set_caption( '飞机大战')
  98. # 加载图标图片
  99. iconSurface = pygame.image.load( 'img/app.ico')
  100. # 设置图标
  101. pygame.display.set_icon(iconSurface)
  102. # 创建飞机类
  103. plane = Plane( 200, 600, window)
  104. # 保证窗口不关闭
  105. while True:
  106. # 展示背景
  107. window.blit(bgSurface, ( 0, 0))
  108. # 展示飞机
  109. plane.display()
  110. # 刷新
  111. pygame.display.flip()
  112. # 处理窗口关闭
  113. eventList = pygame.event.get()
  114. for event in eventList:
  115. # 判断是否是退出类型
  116. if event.type == QUIT:
  117. # 退出游戏
  118. pygame.quit()
  119. # 退出程序
  120. sys.exit()
  121. elif event.type==KEYDOWN:
  122. if event.key==K_RETURN:
  123. # 发射子弹
  124. plane.fire()
  125. # 处理按压事件
  126. # 返回所有键对应的状态 0:没有按压 1:有按压
  127. states = pygame.key.get_pressed()
  128. # 有按压事件
  129. if 1 in states:
  130. # 是否是关心的a d s w键
  131. if states[K_a]: # 0 1
  132. plane.moveLeft()
  133. elif states[K_d]:
  134. plane.moveRight()
  135. elif states[K_w]:
  136. plane.moveUp()
  137. elif states[K_s]:
  138. plane.moveDown()
  139. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  140. # if event.key == K_a:
  141. # plane.moveLeft()
  142. # elif event.key == K_d:
  143. # plane.moveRight()
  144. # elif event.key == K_w:
  145. # plane.moveUp()
  146. # elif event.key == K_s:
  147. # plane.moveDown()
  148. if __name__ == '__main__':
  149. start()

11.子弹位置确定


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 子弹类 ------------------"""
  13. class Bullet:
  14. def __init__(self,px,py,pw,window):
  15. # 需要展示的窗口
  16. self.__window = window
  17. # 展示的控件
  18. self.__surface = pygame.image.load( 'img/bullet_10.png')
  19. # 控件宽度和高度
  20. self.width = self.__surface.get_width()
  21. self.height = self.__surface.get_height()
  22. # bx = px + (pw - bw) / 2
  23. # by = py - bh / 2
  24. self.__x = px+(pw-self.width)/ 2
  25. self.__y = py-self.height/ 2
  26. def display(self):
  27. '''
  28. 展示控件
  29. :return:
  30. '''
  31. self.__window.blit(self.__surface,(self.__x,self.__y))
  32. """------------------ 我方飞机类 ------------------"""
  33. class Plane:
  34. def __init__(self, x, y, window):
  35. # 坐标
  36. self.__x = x
  37. self.__y = y
  38. # 被展示到的窗口
  39. self.__window = window
  40. # 飞机展示的Surface
  41. self.__surface = pygame.image.load( 'img/hero2.png')
  42. # 控件宽度和高度
  43. self.width = self.__surface.get_width()
  44. self.height = self.__surface.get_height()
  45. # 运动速度
  46. self.speed = 1
  47. # 发射的子弹容器
  48. self.__bullets = []
  49. def display(self):
  50. '''
  51. 被展示
  52. :return:
  53. '''
  54. # 展示我方飞机
  55. self.__window.blit(self.__surface, (self.__x, self.__y))
  56. # 把所有发射的子弹渲染出来
  57. for bullet in self.__bullets:
  58. # 把子弹展示出来
  59. bullet.display()
  60. def fire(self):
  61. '''
  62. 发射子弹
  63. :return:
  64. '''
  65. # 子弹的x和y
  66. # bx = px + (pw - bw) / 2
  67. # by = py - bh / 2
  68. # 创建子弹对象
  69. bullet = Bullet(self.__x,self.__y,self.width,self.__window)
  70. # 子弹对象添加到子弹列表中
  71. self.__bullets.append(bullet)
  72. def moveLeft(self):
  73. '''
  74. 向左移动
  75. :return:
  76. '''
  77. self.__x -= self.speed
  78. def moveRight(self):
  79. '''
  80. 向右移动
  81. :return:
  82. '''
  83. self.__x += self.speed
  84. def moveUp(self):
  85. '''
  86. 向上移动
  87. :return:
  88. '''
  89. self.__y -= self.speed
  90. def moveDown(self):
  91. '''
  92. 向下移动
  93. :return:
  94. '''
  95. self.__y += self.speed
  96. def start():
  97. # 初始化
  98. pygame.init()
  99. # 加载背景图片
  100. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  101. # 获取背景图片的大小
  102. bgWidth = bgSurface.get_width()
  103. bgHeight = bgSurface.get_height()
  104. # 创建游戏窗口
  105. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  106. # 窗口标题
  107. pygame.display.set_caption( '飞机大战')
  108. # 加载图标图片
  109. iconSurface = pygame.image.load( 'img/app.ico')
  110. # 设置图标
  111. pygame.display.set_icon(iconSurface)
  112. # 创建飞机类
  113. plane = Plane( 200, 600, window)
  114. # 保证窗口不关闭
  115. while True:
  116. # 展示背景
  117. window.blit(bgSurface, ( 0, 0))
  118. # 展示飞机
  119. plane.display()
  120. # 刷新
  121. pygame.display.flip()
  122. # 处理窗口关闭
  123. eventList = pygame.event.get()
  124. for event in eventList:
  125. # 判断是否是退出类型
  126. if event.type == QUIT:
  127. # 退出游戏
  128. pygame.quit()
  129. # 退出程序
  130. sys.exit()
  131. elif event.type==KEYDOWN:
  132. if event.key==K_RETURN:
  133. # 发射子弹
  134. plane.fire()
  135. # 处理按压事件
  136. # 返回所有键对应的状态 0:没有按压 1:有按压
  137. states = pygame.key.get_pressed()
  138. # 有按压事件
  139. if 1 in states:
  140. # 是否是关心的a d s w键
  141. if states[K_a]: # 0 1
  142. plane.moveLeft()
  143. elif states[K_d]:
  144. plane.moveRight()
  145. elif states[K_w]:
  146. plane.moveUp()
  147. elif states[K_s]:
  148. plane.moveDown()
  149. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  150. # if event.key == K_a:
  151. # plane.moveLeft()
  152. # elif event.key == K_d:
  153. # plane.moveRight()
  154. # elif event.key == K_w:
  155. # plane.moveUp()
  156. # elif event.key == K_s:
  157. # plane.moveDown()
  158. if __name__ == '__main__':
  159. start()

12.子弹的移动


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 子弹类 ------------------"""
  13. class Bullet:
  14. def __init__(self,px,py,pw,window):
  15. # 需要展示的窗口
  16. self.__window = window
  17. # 展示的控件
  18. self.__surface = pygame.image.load( 'img/bullet_10.png')
  19. # 控件宽度和高度
  20. self.width = self.__surface.get_width()
  21. self.height = self.__surface.get_height()
  22. # bx = px + (pw - bw) / 2
  23. # by = py - bh / 2
  24. self.__x = px+(pw-self.width)/ 2
  25. self.__y = py-self.height/ 2
  26. # 自动运行速度
  27. self.__speed = 2
  28. def display(self):
  29. '''
  30. 展示控件
  31. :return:
  32. '''
  33. self.__window.blit(self.__surface,(self.__x,self.__y))
  34. # 自动移动
  35. self.autoMove()
  36. def autoMove(self):
  37. '''
  38. 自动移动
  39. :return:
  40. '''
  41. self.__y-=self.__speed
  42. """------------------ 我方飞机类 ------------------"""
  43. class Plane:
  44. def __init__(self, x, y, window):
  45. # 坐标
  46. self.__x = x
  47. self.__y = y
  48. # 被展示到的窗口
  49. self.__window = window
  50. # 飞机展示的Surface
  51. self.__surface = pygame.image.load( 'img/hero2.png')
  52. # 控件宽度和高度
  53. self.width = self.__surface.get_width()
  54. self.height = self.__surface.get_height()
  55. # 运动速度
  56. self.speed = 1
  57. # 发射的子弹容器
  58. self.__bullets = []
  59. def display(self):
  60. '''
  61. 被展示
  62. :return:
  63. '''
  64. # 展示我方飞机
  65. self.__window.blit(self.__surface, (self.__x, self.__y))
  66. # 把所有发射的子弹渲染出来
  67. for bullet in self.__bullets:
  68. # 把子弹展示出来
  69. bullet.display()
  70. def fire(self):
  71. '''
  72. 发射子弹
  73. :return:
  74. '''
  75. # 子弹的x和y
  76. # bx = px + (pw - bw) / 2
  77. # by = py - bh / 2
  78. # 创建子弹对象
  79. bullet = Bullet(self.__x,self.__y,self.width,self.__window)
  80. # 子弹对象添加到子弹列表中
  81. self.__bullets.append(bullet)
  82. def moveLeft(self):
  83. '''
  84. 向左移动
  85. :return:
  86. '''
  87. self.__x -= self.speed
  88. def moveRight(self):
  89. '''
  90. 向右移动
  91. :return:
  92. '''
  93. self.__x += self.speed
  94. def moveUp(self):
  95. '''
  96. 向上移动
  97. :return:
  98. '''
  99. self.__y -= self.speed
  100. def moveDown(self):
  101. '''
  102. 向下移动
  103. :return:
  104. '''
  105. self.__y += self.speed
  106. def start():
  107. # 初始化
  108. pygame.init()
  109. # 加载背景图片
  110. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  111. # 获取背景图片的大小
  112. bgWidth = bgSurface.get_width()
  113. bgHeight = bgSurface.get_height()
  114. # 创建游戏窗口
  115. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  116. # 窗口标题
  117. pygame.display.set_caption( '飞机大战')
  118. # 加载图标图片
  119. iconSurface = pygame.image.load( 'img/app.ico')
  120. # 设置图标
  121. pygame.display.set_icon(iconSurface)
  122. # 创建飞机类
  123. plane = Plane( 200, 600, window)
  124. # 保证窗口不关闭
  125. while True:
  126. # 展示背景
  127. window.blit(bgSurface, ( 0, 0))
  128. # 展示飞机
  129. plane.display()
  130. # 刷新
  131. pygame.display.flip()
  132. # 处理窗口关闭
  133. eventList = pygame.event.get()
  134. for event in eventList:
  135. # 判断是否是退出类型
  136. if event.type == QUIT:
  137. # 退出游戏
  138. pygame.quit()
  139. # 退出程序
  140. sys.exit()
  141. elif event.type==KEYDOWN:
  142. if event.key==K_RETURN:
  143. # 发射子弹
  144. plane.fire()
  145. # 处理按压事件
  146. # 返回所有键对应的状态 0:没有按压 1:有按压
  147. states = pygame.key.get_pressed()
  148. # 有按压事件
  149. if 1 in states:
  150. # 是否是关心的a d s w键
  151. if states[K_a]: # 0 1
  152. plane.moveLeft()
  153. elif states[K_d]:
  154. plane.moveRight()
  155. elif states[K_w]:
  156. plane.moveUp()
  157. elif states[K_s]:
  158. plane.moveDown()
  159. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  160. # if event.key == K_a:
  161. # plane.moveLeft()
  162. # elif event.key == K_d:
  163. # plane.moveRight()
  164. # elif event.key == K_w:
  165. # plane.moveUp()
  166. # elif event.key == K_s:
  167. # plane.moveDown()
  168. if __name__ == '__main__':
  169. start()

13.子弹的越界处理


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 子弹类 ------------------"""
  13. class Bullet:
  14. def __init__(self,px,py,pw,window):
  15. # 需要展示的窗口
  16. self.__window = window
  17. # 展示的控件
  18. self.__surface = pygame.image.load( 'img/bullet_10.png')
  19. # 控件宽度和高度
  20. self.width = self.__surface.get_width()
  21. self.height = self.__surface.get_height()
  22. # bx = px + (pw - bw) / 2
  23. # by = py - bh / 2
  24. self.__x = px+(pw-self.width)/ 2
  25. self.__y = py-self.height/ 2
  26. # 自动运行速度
  27. self.__speed = 2
  28. def display(self):
  29. '''
  30. 展示控件
  31. :return:
  32. '''
  33. self.__window.blit(self.__surface,(self.__x,self.__y))
  34. # 自动移动
  35. self.autoMove()
  36. def autoMove(self):
  37. '''
  38. 自动移动
  39. :return:
  40. '''
  41. self.__y-=self.__speed
  42. def needDestroy(self):
  43. '''
  44. 是否需要被销毁
  45. :return:
  46. '''
  47. return self.__y<=-self.height
  48. """------------------ 我方飞机类 ------------------"""
  49. class Plane:
  50. def __init__(self, x, y, window):
  51. # 坐标
  52. self.__x = x
  53. self.__y = y
  54. # 被展示到的窗口
  55. self.__window = window
  56. # 飞机展示的Surface
  57. self.__surface = pygame.image.load( 'img/hero2.png')
  58. # 控件宽度和高度
  59. self.width = self.__surface.get_width()
  60. self.height = self.__surface.get_height()
  61. # 运动速度
  62. self.speed = 1
  63. # 发射的子弹容器
  64. self.__bullets = []
  65. def display(self):
  66. '''
  67. 被展示
  68. :return:
  69. '''
  70. print(len(self.__bullets))
  71. # 展示我方飞机
  72. self.__window.blit(self.__surface, (self.__x, self.__y))
  73. # 先销毁越界子弹
  74. for bullet in self.__bullets:
  75. # 如果子弹需要销毁
  76. if bullet.needDestroy():
  77. # 移除子弹对象
  78. self.__bullets.remove(bullet)
  79. # 内存销毁对象
  80. del bullet
  81. # 把所有发射的子弹渲染出来
  82. for bullet in self.__bullets:
  83. # 把子弹展示出来
  84. bullet.display()
  85. def fire(self):
  86. '''
  87. 发射子弹
  88. :return:
  89. '''
  90. # 子弹的x和y
  91. # bx = px + (pw - bw) / 2
  92. # by = py - bh / 2
  93. # 创建子弹对象
  94. bullet = Bullet(self.__x,self.__y,self.width,self.__window)
  95. # 子弹对象添加到子弹列表中
  96. self.__bullets.append(bullet)
  97. def moveLeft(self):
  98. '''
  99. 向左移动
  100. :return:
  101. '''
  102. self.__x -= self.speed
  103. def moveRight(self):
  104. '''
  105. 向右移动
  106. :return:
  107. '''
  108. self.__x += self.speed
  109. def moveUp(self):
  110. '''
  111. 向上移动
  112. :return:
  113. '''
  114. self.__y -= self.speed
  115. def moveDown(self):
  116. '''
  117. 向下移动
  118. :return:
  119. '''
  120. self.__y += self.speed
  121. def start():
  122. # 初始化
  123. pygame.init()
  124. # 加载背景图片
  125. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  126. # 获取背景图片的大小
  127. bgWidth = bgSurface.get_width()
  128. bgHeight = bgSurface.get_height()
  129. # 创建游戏窗口
  130. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  131. # 窗口标题
  132. pygame.display.set_caption( '飞机大战')
  133. # 加载图标图片
  134. iconSurface = pygame.image.load( 'img/app.ico')
  135. # 设置图标
  136. pygame.display.set_icon(iconSurface)
  137. # 创建飞机类
  138. plane = Plane( 200, 600, window)
  139. # 保证窗口不关闭
  140. while True:
  141. # 展示背景
  142. window.blit(bgSurface, ( 0, 0))
  143. # 展示飞机
  144. plane.display()
  145. # 刷新
  146. pygame.display.flip()
  147. # 处理窗口关闭
  148. eventList = pygame.event.get()
  149. for event in eventList:
  150. # 判断是否是退出类型
  151. if event.type == QUIT:
  152. # 退出游戏
  153. pygame.quit()
  154. # 退出程序
  155. sys.exit()
  156. elif event.type==KEYDOWN:
  157. if event.key==K_RETURN:
  158. # 发射子弹
  159. plane.fire()
  160. # 处理按压事件
  161. # 返回所有键对应的状态 0:没有按压 1:有按压
  162. states = pygame.key.get_pressed()
  163. # 有按压事件
  164. if 1 in states:
  165. # 是否是关心的a d s w键
  166. if states[K_a]: # 0 1
  167. plane.moveLeft()
  168. elif states[K_d]:
  169. plane.moveRight()
  170. elif states[K_w]:
  171. plane.moveUp()
  172. elif states[K_s]:
  173. plane.moveDown()
  174. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  175. # if event.key == K_a:
  176. # plane.moveLeft()
  177. # elif event.key == K_d:
  178. # plane.moveRight()
  179. # elif event.key == K_w:
  180. # plane.moveUp()
  181. # elif event.key == K_s:
  182. # plane.moveDown()
  183. if __name__ == '__main__':
  184. start()

14.子弹的越界处理错误解决


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 子弹类 ------------------"""
  13. class Bullet:
  14. def __init__(self, px, py, pw, window):
  15. # 需要展示的窗口
  16. self.__window = window
  17. # 展示的控件
  18. self.__surface = pygame.image.load( 'img/bullet_10.png')
  19. # 控件宽度和高度
  20. self.width = self.__surface.get_width()
  21. self.height = self.__surface.get_height()
  22. # bx = px + (pw - bw) / 2
  23. # by = py - bh / 2
  24. self.__x = px + (pw - self.width) / 2
  25. self.__y = py - self.height / 2
  26. # 自动运行速度
  27. self.__speed = 2
  28. def display(self):
  29. '''
  30. 展示控件
  31. :return:
  32. '''
  33. self.__window.blit(self.__surface, (self.__x, self.__y))
  34. # 自动移动
  35. self.autoMove()
  36. def autoMove(self):
  37. '''
  38. 自动移动
  39. :return:
  40. '''
  41. self.__y -= self.__speed
  42. def needDestroy(self):
  43. '''
  44. 是否需要被销毁
  45. :return:
  46. '''
  47. return self.__y <= -self.height
  48. """------------------ 我方飞机类 ------------------"""
  49. class Plane:
  50. def __init__(self, x, y, window):
  51. # 坐标
  52. self.__x = x
  53. self.__y = y
  54. # 被展示到的窗口
  55. self.__window = window
  56. # 飞机展示的Surface
  57. self.__surface = pygame.image.load( 'img/hero2.png')
  58. # 控件宽度和高度
  59. self.width = self.__surface.get_width()
  60. self.height = self.__surface.get_height()
  61. # 运动速度
  62. self.speed = 1
  63. # 发射的子弹容器
  64. self.__bullets = []
  65. def display(self):
  66. '''
  67. 被展示
  68. :return:
  69. '''
  70. print(len(self.__bullets))
  71. # 展示我方飞机
  72. self.__window.blit(self.__surface, (self.__x, self.__y))
  73. # 在循环遍历的时候做了移除元素的操作
  74. # 先销毁越界子弹
  75. # for bullet in self.__bullets:
  76. # # 如果子弹需要销毁
  77. # if bullet.needDestroy():
  78. # # 移除子弹对象
  79. # self.__bullets.remove(bullet)
  80. # # 内存销毁对象
  81. # del bullet
  82. for index in range(len(self.__bullets) - 1, -1, -1):
  83. bullet = self.__bullets[index]
  84. if bullet.needDestroy():
  85. # 移除子弹对象
  86. self.__bullets.remove(bullet)
  87. # 内存销毁对象
  88. del bullet
  89. else:
  90. # 把子弹展示出来
  91. bullet.display()
  92. # # 把所有发射的子弹渲染出来
  93. # for bullet in self.__bullets:
  94. # # 把子弹展示出来
  95. # bullet.display()
  96. def fire(self):
  97. '''
  98. 发射子弹
  99. :return:
  100. '''
  101. # 子弹的x和y
  102. # bx = px + (pw - bw) / 2
  103. # by = py - bh / 2
  104. # 创建子弹对象
  105. bullet = Bullet(self.__x, self.__y, self.width, self.__window)
  106. # 子弹对象添加到子弹列表中
  107. self.__bullets.append(bullet)
  108. def moveLeft(self):
  109. '''
  110. 向左移动
  111. :return:
  112. '''
  113. self.__x -= self.speed
  114. # 越界处理
  115. if self.__x < 0:
  116. self.__x = 0
  117. def moveRight(self):
  118. '''
  119. 向右移动
  120. :return:
  121. '''
  122. self.__x += self.speed
  123. # 越界处理
  124. if self.__x > bgWidth - self.width:
  125. self.__x = bgWidth - self.width
  126. def moveUp(self):
  127. '''
  128. 向上移动
  129. :return:
  130. '''
  131. self.__y -= self.speed
  132. # 越界处理
  133. if self.__y < 0:
  134. self.__y = 0
  135. def moveDown(self):
  136. '''
  137. 向下移动
  138. :return:
  139. '''
  140. self.__y += self.speed
  141. # 越界处理
  142. if self.__y > bgHeight - self.height:
  143. self.__y = bgHeight - self.height
  144. # 全局变量
  145. bgWidth = 0
  146. bgHeight = 0
  147. def start():
  148. global bgWidth, bgHeight
  149. # 初始化
  150. pygame.init()
  151. # 加载背景图片
  152. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  153. # 获取背景图片的大小
  154. bgWidth = bgSurface.get_width()
  155. bgHeight = bgSurface.get_height()
  156. # 创建游戏窗口
  157. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  158. # 窗口标题
  159. pygame.display.set_caption( '飞机大战')
  160. # 加载图标图片
  161. iconSurface = pygame.image.load( 'img/app.ico')
  162. # 设置图标
  163. pygame.display.set_icon(iconSurface)
  164. # 创建飞机类
  165. plane = Plane( 200, 600, window)
  166. # 保证窗口不关闭
  167. while True:
  168. # 展示背景
  169. window.blit(bgSurface, ( 0, 0))
  170. # 展示飞机
  171. plane.display()
  172. # 刷新
  173. pygame.display.flip()
  174. # 处理窗口关闭
  175. eventList = pygame.event.get()
  176. for event in eventList:
  177. # 判断是否是退出类型
  178. if event.type == QUIT:
  179. # 退出游戏
  180. pygame.quit()
  181. # 退出程序
  182. sys.exit()
  183. elif event.type == KEYDOWN:
  184. if event.key == K_RETURN:
  185. # 发射子弹
  186. plane.fire()
  187. # 处理按压事件
  188. # 返回所有键对应的状态 0:没有按压 1:有按压
  189. states = pygame.key.get_pressed()
  190. # 有按压事件
  191. if 1 in states:
  192. # 是否是关心的a d s w键
  193. if states[K_a]: # 0 1
  194. plane.moveLeft()
  195. elif states[K_d]:
  196. plane.moveRight()
  197. elif states[K_w]:
  198. plane.moveUp()
  199. elif states[K_s]:
  200. plane.moveDown()
  201. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  202. # if event.key == K_a:
  203. # plane.moveLeft()
  204. # elif event.key == K_d:
  205. # plane.moveRight()
  206. # elif event.key == K_w:
  207. # plane.moveUp()
  208. # elif event.key == K_s:
  209. # plane.moveDown()
  210. if __name__ == '__main__':
  211. start()

15.提取敌方飞机类


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 敌方飞机类 ------------------"""
  13. class EnemyPlane:
  14. def __init__(self, x, y, window):
  15. # 坐标
  16. self.__x = x
  17. self.__y = y
  18. # 被展示到的窗口
  19. self.__window = window
  20. # 飞机展示的Surface
  21. self.__surface = pygame.image.load( 'img/img-plane_1.png')
  22. # 控件宽度和高度
  23. self.width = self.__surface.get_width()
  24. self.height = self.__surface.get_height()
  25. def display(self):
  26. '''
  27. 被展示
  28. :return:
  29. '''
  30. # 展示我方飞机
  31. self.__window.blit(self.__surface, (self.__x, self.__y))
  32. """------------------ 子弹类 ------------------"""
  33. class Bullet:
  34. def __init__(self, px, py, pw, window):
  35. # 需要展示的窗口
  36. self.__window = window
  37. # 展示的控件
  38. self.__surface = pygame.image.load( 'img/bullet_10.png')
  39. # 控件宽度和高度
  40. self.width = self.__surface.get_width()
  41. self.height = self.__surface.get_height()
  42. # bx = px + (pw - bw) / 2
  43. # by = py - bh / 2
  44. self.__x = px + (pw - self.width) / 2
  45. self.__y = py - self.height / 2
  46. # 自动运行速度
  47. self.__speed = 2
  48. def display(self):
  49. '''
  50. 展示控件
  51. :return:
  52. '''
  53. self.__window.blit(self.__surface, (self.__x, self.__y))
  54. # 自动移动
  55. self.autoMove()
  56. def autoMove(self):
  57. '''
  58. 自动移动
  59. :return:
  60. '''
  61. self.__y -= self.__speed
  62. def needDestroy(self):
  63. '''
  64. 是否需要被销毁
  65. :return:
  66. '''
  67. return self.__y <= -self.height
  68. """------------------ 我方飞机类 ------------------"""
  69. class Plane:
  70. def __init__(self, x, y, window):
  71. # 坐标
  72. self.__x = x
  73. self.__y = y
  74. # 被展示到的窗口
  75. self.__window = window
  76. # 飞机展示的Surface
  77. self.__surface = pygame.image.load( 'img/hero2.png')
  78. # 控件宽度和高度
  79. self.width = self.__surface.get_width()
  80. self.height = self.__surface.get_height()
  81. # 运动速度
  82. self.speed = 1
  83. # 发射的子弹容器
  84. self.__bullets = []
  85. def display(self):
  86. '''
  87. 被展示
  88. :return:
  89. '''
  90. print(len(self.__bullets))
  91. # 展示我方飞机
  92. self.__window.blit(self.__surface, (self.__x, self.__y))
  93. # 在循环遍历的时候做了移除元素的操作
  94. # 先销毁越界子弹
  95. # for bullet in self.__bullets:
  96. # # 如果子弹需要销毁
  97. # if bullet.needDestroy():
  98. # # 移除子弹对象
  99. # self.__bullets.remove(bullet)
  100. # # 内存销毁对象
  101. # del bullet
  102. for index in range(len(self.__bullets) - 1, -1, -1):
  103. bullet = self.__bullets[index]
  104. if bullet.needDestroy():
  105. # 移除子弹对象
  106. self.__bullets.remove(bullet)
  107. # 内存销毁对象
  108. del bullet
  109. else:
  110. # 把子弹展示出来
  111. bullet.display()
  112. # # 把所有发射的子弹渲染出来
  113. # for bullet in self.__bullets:
  114. # # 把子弹展示出来
  115. # bullet.display()
  116. def fire(self):
  117. '''
  118. 发射子弹
  119. :return:
  120. '''
  121. # 子弹的x和y
  122. # bx = px + (pw - bw) / 2
  123. # by = py - bh / 2
  124. # 创建子弹对象
  125. bullet = Bullet(self.__x, self.__y, self.width, self.__window)
  126. # 子弹对象添加到子弹列表中
  127. self.__bullets.append(bullet)
  128. def moveLeft(self):
  129. '''
  130. 向左移动
  131. :return:
  132. '''
  133. self.__x -= self.speed
  134. # 越界处理
  135. if self.__x < 0:
  136. self.__x = 0
  137. def moveRight(self):
  138. '''
  139. 向右移动
  140. :return:
  141. '''
  142. self.__x += self.speed
  143. # 越界处理
  144. if self.__x > bgWidth - self.width:
  145. self.__x = bgWidth - self.width
  146. def moveUp(self):
  147. '''
  148. 向上移动
  149. :return:
  150. '''
  151. self.__y -= self.speed
  152. # 越界处理
  153. if self.__y < 0:
  154. self.__y = 0
  155. def moveDown(self):
  156. '''
  157. 向下移动
  158. :return:
  159. '''
  160. self.__y += self.speed
  161. # 越界处理
  162. if self.__y > bgHeight - self.height:
  163. self.__y = bgHeight - self.height
  164. # 全局变量
  165. bgWidth = 0
  166. bgHeight = 0
  167. def start():
  168. global bgWidth, bgHeight
  169. # 初始化
  170. pygame.init()
  171. # 加载背景图片
  172. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  173. # 获取背景图片的大小
  174. bgWidth = bgSurface.get_width()
  175. bgHeight = bgSurface.get_height()
  176. # 创建游戏窗口
  177. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  178. # 窗口标题
  179. pygame.display.set_caption( '飞机大战')
  180. # 加载图标图片
  181. iconSurface = pygame.image.load( 'img/app.ico')
  182. # 设置图标
  183. pygame.display.set_icon(iconSurface)
  184. # 创建飞机类
  185. plane = Plane( 200, 600, window)
  186. # 保证窗口不关闭
  187. while True:
  188. # 展示背景
  189. window.blit(bgSurface, ( 0, 0))
  190. # 展示飞机
  191. plane.display()
  192. # 刷新
  193. pygame.display.flip()
  194. # 处理窗口关闭
  195. eventList = pygame.event.get()
  196. for event in eventList:
  197. # 判断是否是退出类型
  198. if event.type == QUIT:
  199. # 退出游戏
  200. pygame.quit()
  201. # 退出程序
  202. sys.exit()
  203. elif event.type == KEYDOWN:
  204. if event.key == K_RETURN:
  205. # 发射子弹
  206. plane.fire()
  207. # 处理按压事件
  208. # 返回所有键对应的状态 0:没有按压 1:有按压
  209. states = pygame.key.get_pressed()
  210. # 有按压事件
  211. if 1 in states:
  212. # 是否是关心的a d s w键
  213. if states[K_a]: # 0 1
  214. plane.moveLeft()
  215. elif states[K_d]:
  216. plane.moveRight()
  217. elif states[K_w]:
  218. plane.moveUp()
  219. elif states[K_s]:
  220. plane.moveDown()
  221. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  222. # if event.key == K_a:
  223. # plane.moveLeft()
  224. # elif event.key == K_d:
  225. # plane.moveRight()
  226. # elif event.key == K_w:
  227. # plane.moveUp()
  228. # elif event.key == K_s:
  229. # plane.moveDown()
  230. if __name__ == '__main__':
  231. start()

16.展示敌方飞机以及敌方飞机自动移动


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 敌方飞机类 ------------------"""
  13. class EnemyPlane:
  14. def __init__(self, x, y, window):
  15. # 坐标
  16. self.__x = x
  17. self.__y = y
  18. # 被展示到的窗口
  19. self.__window = window
  20. # 飞机展示的Surface
  21. self.__surface = pygame.image.load( 'img/img-plane_1.png')
  22. # 控件宽度和高度
  23. self.width = self.__surface.get_width()
  24. self.height = self.__surface.get_height()
  25. # 自动移动
  26. self.__speed = 1
  27. def display(self):
  28. '''
  29. 被展示
  30. :return:
  31. '''
  32. # 展示我方飞机
  33. self.__window.blit(self.__surface, (self.__x, self.__y))
  34. # 自动移动
  35. self.autoMove()
  36. def autoMove(self):
  37. '''
  38. 自动移动
  39. :return:
  40. '''
  41. self.__y += self.__speed
  42. """------------------ 子弹类 ------------------"""
  43. class Bullet:
  44. def __init__(self, px, py, pw, window):
  45. # 需要展示的窗口
  46. self.__window = window
  47. # 展示的控件
  48. self.__surface = pygame.image.load( 'img/bullet_10.png')
  49. # 控件宽度和高度
  50. self.width = self.__surface.get_width()
  51. self.height = self.__surface.get_height()
  52. # bx = px + (pw - bw) / 2
  53. # by = py - bh / 2
  54. self.__x = px + (pw - self.width) / 2
  55. self.__y = py - self.height / 2
  56. # 自动运行速度
  57. self.__speed = 2
  58. def display(self):
  59. '''
  60. 展示控件
  61. :return:
  62. '''
  63. self.__window.blit(self.__surface, (self.__x, self.__y))
  64. # 自动移动
  65. self.autoMove()
  66. def autoMove(self):
  67. '''
  68. 自动移动
  69. :return:
  70. '''
  71. self.__y -= self.__speed
  72. def needDestroy(self):
  73. '''
  74. 是否需要被销毁
  75. :return:
  76. '''
  77. return self.__y <= -self.height
  78. """------------------ 我方飞机类 ------------------"""
  79. class Plane:
  80. def __init__(self, x, y, window):
  81. # 坐标
  82. self.__x = x
  83. self.__y = y
  84. # 被展示到的窗口
  85. self.__window = window
  86. # 飞机展示的Surface
  87. self.__surface = pygame.image.load( 'img/hero2.png')
  88. # 控件宽度和高度
  89. self.width = self.__surface.get_width()
  90. self.height = self.__surface.get_height()
  91. # 运动速度
  92. self.speed = 1
  93. # 发射的子弹容器
  94. self.__bullets = []
  95. def display(self):
  96. '''
  97. 被展示
  98. :return:
  99. '''
  100. print(len(self.__bullets))
  101. # 展示我方飞机
  102. self.__window.blit(self.__surface, (self.__x, self.__y))
  103. # 在循环遍历的时候做了移除元素的操作
  104. # 先销毁越界子弹
  105. # for bullet in self.__bullets:
  106. # # 如果子弹需要销毁
  107. # if bullet.needDestroy():
  108. # # 移除子弹对象
  109. # self.__bullets.remove(bullet)
  110. # # 内存销毁对象
  111. # del bullet
  112. # 是否和敌方飞机发生了碰撞
  113. # TODO 是否发生碰撞
  114. # 展示
  115. for index in range(len(self.__bullets) - 1, -1, -1):
  116. bullet = self.__bullets[index]
  117. if bullet.needDestroy():
  118. # 移除子弹对象
  119. self.__bullets.remove(bullet)
  120. # 内存销毁对象
  121. del bullet
  122. else:
  123. # 把子弹展示出来
  124. bullet.display()
  125. # # 把所有发射的子弹渲染出来
  126. # for bullet in self.__bullets:
  127. # # 把子弹展示出来
  128. # bullet.display()
  129. def fire(self):
  130. '''
  131. 发射子弹
  132. :return:
  133. '''
  134. # 子弹的x和y
  135. # bx = px + (pw - bw) / 2
  136. # by = py - bh / 2
  137. # 创建子弹对象
  138. bullet = Bullet(self.__x, self.__y, self.width, self.__window)
  139. # 子弹对象添加到子弹列表中
  140. self.__bullets.append(bullet)
  141. def moveLeft(self):
  142. '''
  143. 向左移动
  144. :return:
  145. '''
  146. self.__x -= self.speed
  147. # 越界处理
  148. if self.__x < 0:
  149. self.__x = 0
  150. def moveRight(self):
  151. '''
  152. 向右移动
  153. :return:
  154. '''
  155. self.__x += self.speed
  156. # 越界处理
  157. if self.__x > bgWidth - self.width:
  158. self.__x = bgWidth - self.width
  159. def moveUp(self):
  160. '''
  161. 向上移动
  162. :return:
  163. '''
  164. self.__y -= self.speed
  165. # 越界处理
  166. if self.__y < 0:
  167. self.__y = 0
  168. def moveDown(self):
  169. '''
  170. 向下移动
  171. :return:
  172. '''
  173. self.__y += self.speed
  174. # 越界处理
  175. if self.__y > bgHeight - self.height:
  176. self.__y = bgHeight - self.height
  177. # 全局变量
  178. bgWidth = 0
  179. bgHeight = 0
  180. def start():
  181. global bgWidth, bgHeight
  182. # 初始化
  183. pygame.init()
  184. # 加载背景图片
  185. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  186. # 获取背景图片的大小
  187. bgWidth = bgSurface.get_width()
  188. bgHeight = bgSurface.get_height()
  189. # 创建游戏窗口
  190. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  191. # 窗口标题
  192. pygame.display.set_caption( '飞机大战')
  193. # 加载图标图片
  194. iconSurface = pygame.image.load( 'img/app.ico')
  195. # 设置图标
  196. pygame.display.set_icon(iconSurface)
  197. # 创建飞机类
  198. plane = Plane( 200, 600, window)
  199. # 创建敌方飞机类
  200. enemyPlane = EnemyPlane( 100, 0,window)
  201. # 保证窗口不关闭
  202. while True:
  203. # 展示背景
  204. window.blit(bgSurface, ( 0, 0))
  205. # 展示我方飞机
  206. plane.display()
  207. # 展示敌方飞机
  208. enemyPlane.display()
  209. # 刷新
  210. pygame.display.flip()
  211. # 处理窗口关闭
  212. eventList = pygame.event.get()
  213. for event in eventList:
  214. # 判断是否是退出类型
  215. if event.type == QUIT:
  216. # 退出游戏
  217. pygame.quit()
  218. # 退出程序
  219. sys.exit()
  220. elif event.type == KEYDOWN:
  221. if event.key == K_RETURN:
  222. # 发射子弹
  223. plane.fire()
  224. # 处理按压事件
  225. # 返回所有键对应的状态 0:没有按压 1:有按压
  226. states = pygame.key.get_pressed()
  227. # 有按压事件
  228. if 1 in states:
  229. # 是否是关心的a d s w键
  230. if states[K_a]: # 0 1
  231. plane.moveLeft()
  232. elif states[K_d]:
  233. plane.moveRight()
  234. elif states[K_w]:
  235. plane.moveUp()
  236. elif states[K_s]:
  237. plane.moveDown()
  238. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  239. # if event.key == K_a:
  240. # plane.moveLeft()
  241. # elif event.key == K_d:
  242. # plane.moveRight()
  243. # elif event.key == K_w:
  244. # plane.moveUp()
  245. # elif event.key == K_s:
  246. # plane.moveDown()
  247. if __name__ == '__main__':
  248. start()

17.子弹和敌方飞机碰撞检测


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. """------------------ 敌方飞机类 ------------------"""
  13. class EnemyPlane:
  14. def __init__(self, x, y, window):
  15. # 坐标
  16. self.__x = x
  17. self.__y = y
  18. # 被展示到的窗口
  19. self.__window = window
  20. # 飞机展示的Surface
  21. self.__surface = pygame.image.load( 'img/img-plane_1.png')
  22. # 控件宽度和高度
  23. self.width = self.__surface.get_width()
  24. self.height = self.__surface.get_height()
  25. # 自动移动
  26. self.__speed = 1
  27. def getX(self):
  28. return self.__x
  29. def getY(self):
  30. return self.__y
  31. def display(self):
  32. '''
  33. 被展示
  34. :return:
  35. '''
  36. # 展示我方飞机
  37. self.__window.blit(self.__surface, (self.__x, self.__y))
  38. # 自动移动
  39. self.autoMove()
  40. def autoMove(self):
  41. '''
  42. 自动移动
  43. :return:
  44. '''
  45. self.__y += self.__speed
  46. """------------------ 子弹类 ------------------"""
  47. class Bullet:
  48. def __init__(self, px, py, pw, window):
  49. # 需要展示的窗口
  50. self.__window = window
  51. # 展示的控件
  52. self.__surface = pygame.image.load( 'img/bullet_10.png')
  53. # 控件宽度和高度
  54. self.width = self.__surface.get_width()
  55. self.height = self.__surface.get_height()
  56. # bx = px + (pw - bw) / 2
  57. # by = py - bh / 2
  58. self.__x = px + (pw - self.width) / 2
  59. self.__y = py - self.height / 2
  60. # 自动运行速度
  61. self.__speed = 2
  62. def display(self):
  63. '''
  64. 展示控件
  65. :return:
  66. '''
  67. self.__window.blit(self.__surface, (self.__x, self.__y))
  68. # 自动移动
  69. self.autoMove()
  70. def autoMove(self):
  71. '''
  72. 自动移动
  73. :return:
  74. '''
  75. self.__y -= self.__speed
  76. def needDestroy(self):
  77. '''
  78. 是否需要被销毁
  79. :return:
  80. '''
  81. return self.__y <= -self.height
  82. def hasShootEnemey(self,enemy):
  83. '''
  84. 是否已经射击到敌方飞机
  85. :param enemy: EnemyPlane
  86. :return:
  87. '''
  88. # 子弹矩形
  89. bulletRect = pygame.Rect(self.__x,self.__y,self.width,self.height)
  90. # 敌方飞机矩形
  91. enemyRect = pygame.Rect(enemy.getX(),enemy.getY(),enemy.width,enemy.height)
  92. return bulletRect.colliderect(enemyRect)
  93. """------------------ 我方飞机类 ------------------"""
  94. class Plane:
  95. def __init__(self, x, y, window):
  96. # 坐标
  97. self.__x = x
  98. self.__y = y
  99. # 被展示到的窗口
  100. self.__window = window
  101. # 飞机展示的Surface
  102. self.__surface = pygame.image.load( 'img/hero2.png')
  103. # 控件宽度和高度
  104. self.width = self.__surface.get_width()
  105. self.height = self.__surface.get_height()
  106. # 运动速度
  107. self.speed = 1
  108. # 发射的子弹容器
  109. self.__bullets = []
  110. def display(self):
  111. '''
  112. 被展示
  113. :return:
  114. '''
  115. print(len(self.__bullets))
  116. # 展示我方飞机
  117. self.__window.blit(self.__surface, (self.__x, self.__y))
  118. # 在循环遍历的时候做了移除元素的操作
  119. # 先销毁越界子弹
  120. # for bullet in self.__bullets:
  121. # # 如果子弹需要销毁
  122. # if bullet.needDestroy():
  123. # # 移除子弹对象
  124. # self.__bullets.remove(bullet)
  125. # # 内存销毁对象
  126. # del bullet
  127. # 是否和敌方飞机发生了碰撞
  128. for index in range(len(self.__bullets) - 1, -1, -1):
  129. bullet = self.__bullets[index]
  130. # bullet和enemy是否发生了碰撞
  131. if bullet.hasShootEnemey(enemyPlane):
  132. # 子弹销毁
  133. self.__bullets.remove(bullet)
  134. del bullet
  135. # 敌方飞机打掉
  136. # 敌方飞机复用
  137. # 展示
  138. for index in range(len(self.__bullets) - 1, -1, -1):
  139. bullet = self.__bullets[index]
  140. if bullet.needDestroy():
  141. # 移除子弹对象
  142. self.__bullets.remove(bullet)
  143. # 内存销毁对象
  144. del bullet
  145. else:
  146. # 把子弹展示出来
  147. bullet.display()
  148. # # 把所有发射的子弹渲染出来
  149. # for bullet in self.__bullets:
  150. # # 把子弹展示出来
  151. # bullet.display()
  152. def fire(self):
  153. '''
  154. 发射子弹
  155. :return:
  156. '''
  157. # 子弹的x和y
  158. # bx = px + (pw - bw) / 2
  159. # by = py - bh / 2
  160. # 创建子弹对象
  161. bullet = Bullet(self.__x, self.__y, self.width, self.__window)
  162. # 子弹对象添加到子弹列表中
  163. self.__bullets.append(bullet)
  164. def moveLeft(self):
  165. '''
  166. 向左移动
  167. :return:
  168. '''
  169. self.__x -= self.speed
  170. # 越界处理
  171. if self.__x < 0:
  172. self.__x = 0
  173. def moveRight(self):
  174. '''
  175. 向右移动
  176. :return:
  177. '''
  178. self.__x += self.speed
  179. # 越界处理
  180. if self.__x > bgWidth - self.width:
  181. self.__x = bgWidth - self.width
  182. def moveUp(self):
  183. '''
  184. 向上移动
  185. :return:
  186. '''
  187. self.__y -= self.speed
  188. # 越界处理
  189. if self.__y < 0:
  190. self.__y = 0
  191. def moveDown(self):
  192. '''
  193. 向下移动
  194. :return:
  195. '''
  196. self.__y += self.speed
  197. # 越界处理
  198. if self.__y > bgHeight - self.height:
  199. self.__y = bgHeight - self.height
  200. # 全局变量
  201. bgWidth = 0
  202. bgHeight = 0
  203. # 敌方飞机
  204. enemyPlane = None
  205. def start():
  206. global bgWidth, bgHeight,enemyPlane
  207. # 初始化
  208. pygame.init()
  209. # 加载背景图片
  210. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  211. # 获取背景图片的大小
  212. bgWidth = bgSurface.get_width()
  213. bgHeight = bgSurface.get_height()
  214. # 创建游戏窗口
  215. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  216. # 窗口标题
  217. pygame.display.set_caption( '飞机大战')
  218. # 加载图标图片
  219. iconSurface = pygame.image.load( 'img/app.ico')
  220. # 设置图标
  221. pygame.display.set_icon(iconSurface)
  222. # 创建飞机类
  223. plane = Plane( 200, 600, window)
  224. # 创建敌方飞机类
  225. enemyPlane = EnemyPlane( 100, 0,window)
  226. # 保证窗口不关闭
  227. while True:
  228. # 展示背景
  229. window.blit(bgSurface, ( 0, 0))
  230. # 展示我方飞机
  231. plane.display()
  232. # 展示敌方飞机
  233. enemyPlane.display()
  234. # 刷新
  235. pygame.display.flip()
  236. # 处理窗口关闭
  237. eventList = pygame.event.get()
  238. for event in eventList:
  239. # 判断是否是退出类型
  240. if event.type == QUIT:
  241. # 退出游戏
  242. pygame.quit()
  243. # 退出程序
  244. sys.exit()
  245. elif event.type == KEYDOWN:
  246. if event.key == K_RETURN:
  247. # 发射子弹
  248. plane.fire()
  249. # 处理按压事件
  250. # 返回所有键对应的状态 0:没有按压 1:有按压
  251. states = pygame.key.get_pressed()
  252. # 有按压事件
  253. if 1 in states:
  254. # 是否是关心的a d s w键
  255. if states[K_a]: # 0 1
  256. plane.moveLeft()
  257. elif states[K_d]:
  258. plane.moveRight()
  259. elif states[K_w]:
  260. plane.moveUp()
  261. elif states[K_s]:
  262. plane.moveDown()
  263. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  264. # if event.key == K_a:
  265. # plane.moveLeft()
  266. # elif event.key == K_d:
  267. # plane.moveRight()
  268. # elif event.key == K_w:
  269. # plane.moveUp()
  270. # elif event.key == K_s:
  271. # plane.moveDown()
  272. if __name__ == '__main__':
  273. start()

18.敌方飞机复用


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. import random
  13. """------------------ 敌方飞机类 ------------------"""
  14. class EnemyPlane:
  15. def __init__(self, window):
  16. # 被展示到的窗口
  17. self.__window = window
  18. # 自动移动
  19. self.__speed = 1
  20. # 重置
  21. self.__reset()
  22. def getX(self):
  23. return self.__x
  24. def getY(self):
  25. return self.__y
  26. def display(self):
  27. '''
  28. 被展示
  29. :return:
  30. '''
  31. # 展示我方飞机
  32. self.__window.blit(self.__surface, (self.__x, self.__y))
  33. # 自动移动
  34. self.autoMove()
  35. def autoMove(self):
  36. '''
  37. 自动移动
  38. :return:
  39. '''
  40. self.__y += self.__speed
  41. # 如果越界 需要重置
  42. if self.__y>bgHeight-self.height:
  43. self.__reset()
  44. def reUse(self):
  45. '''
  46. 飞机被打死后者飞机越界 需要重新复用飞机
  47. :return:
  48. '''
  49. self.__reset()
  50. def __reset(self):
  51. '''
  52. 重置
  53. :return:
  54. '''
  55. # 飞机展示的Surface
  56. self.__surface = pygame.image.load( 'img/img-plane_{}.png'.format(random.randint( 1, 7)))
  57. # 控件宽度和高度
  58. self.width = self.__surface.get_width()
  59. self.height = self.__surface.get_height()
  60. # 坐标
  61. self.__x = random.randint( 0,bgWidth-self.width)
  62. self.__y = -self.height
  63. """------------------ 子弹类 ------------------"""
  64. class Bullet:
  65. def __init__(self, px, py, pw, window):
  66. # 需要展示的窗口
  67. self.__window = window
  68. # 展示的控件
  69. self.__surface = pygame.image.load( 'img/bullet_10.png')
  70. # 控件宽度和高度
  71. self.width = self.__surface.get_width()
  72. self.height = self.__surface.get_height()
  73. # bx = px + (pw - bw) / 2
  74. # by = py - bh / 2
  75. self.__x = px + (pw - self.width) / 2
  76. self.__y = py - self.height / 2
  77. # 自动运行速度
  78. self.__speed = 2
  79. def display(self):
  80. '''
  81. 展示控件
  82. :return:
  83. '''
  84. self.__window.blit(self.__surface, (self.__x, self.__y))
  85. # 自动移动
  86. self.autoMove()
  87. def autoMove(self):
  88. '''
  89. 自动移动
  90. :return:
  91. '''
  92. self.__y -= self.__speed
  93. def needDestroy(self):
  94. '''
  95. 是否需要被销毁
  96. :return:
  97. '''
  98. return self.__y <= -self.height
  99. def hasShootEnemey(self,enemy):
  100. '''
  101. 是否已经射击到敌方飞机
  102. :param enemy: EnemyPlane
  103. :return:
  104. '''
  105. # 子弹矩形
  106. bulletRect = pygame.Rect(self.__x,self.__y,self.width,self.height)
  107. # 敌方飞机矩形
  108. enemyRect = pygame.Rect(enemy.getX(),enemy.getY(),enemy.width,enemy.height)
  109. return bulletRect.colliderect(enemyRect)
  110. """------------------ 我方飞机类 ------------------"""
  111. class Plane:
  112. def __init__(self, x, y, window):
  113. # 坐标
  114. self.__x = x
  115. self.__y = y
  116. # 被展示到的窗口
  117. self.__window = window
  118. # 飞机展示的Surface
  119. self.__surface = pygame.image.load( 'img/hero2.png')
  120. # 控件宽度和高度
  121. self.width = self.__surface.get_width()
  122. self.height = self.__surface.get_height()
  123. # 运动速度
  124. self.speed = 1
  125. # 发射的子弹容器
  126. self.__bullets = []
  127. def display(self):
  128. '''
  129. 被展示
  130. :return:
  131. '''
  132. print(len(self.__bullets))
  133. # 展示我方飞机
  134. self.__window.blit(self.__surface, (self.__x, self.__y))
  135. # 在循环遍历的时候做了移除元素的操作
  136. # 先销毁越界子弹
  137. # for bullet in self.__bullets:
  138. # # 如果子弹需要销毁
  139. # if bullet.needDestroy():
  140. # # 移除子弹对象
  141. # self.__bullets.remove(bullet)
  142. # # 内存销毁对象
  143. # del bullet
  144. # 是否和敌方飞机发生了碰撞
  145. for index in range(len(self.__bullets) - 1, -1, -1):
  146. bullet = self.__bullets[index]
  147. # bullet和enemy是否发生了碰撞
  148. if bullet.hasShootEnemey(enemyPlane):
  149. # 子弹销毁
  150. self.__bullets.remove(bullet)
  151. del bullet
  152. # 敌方飞机打掉
  153. # 敌方飞机复用
  154. enemyPlane.reUse()
  155. # 展示
  156. for index in range(len(self.__bullets) - 1, -1, -1):
  157. bullet = self.__bullets[index]
  158. if bullet.needDestroy():
  159. # 移除子弹对象
  160. self.__bullets.remove(bullet)
  161. # 内存销毁对象
  162. del bullet
  163. else:
  164. # 把子弹展示出来
  165. bullet.display()
  166. # # 把所有发射的子弹渲染出来
  167. # for bullet in self.__bullets:
  168. # # 把子弹展示出来
  169. # bullet.display()
  170. def fire(self):
  171. '''
  172. 发射子弹
  173. :return:
  174. '''
  175. # 子弹的x和y
  176. # bx = px + (pw - bw) / 2
  177. # by = py - bh / 2
  178. # 创建子弹对象
  179. bullet = Bullet(self.__x, self.__y, self.width, self.__window)
  180. # 子弹对象添加到子弹列表中
  181. self.__bullets.append(bullet)
  182. def moveLeft(self):
  183. '''
  184. 向左移动
  185. :return:
  186. '''
  187. self.__x -= self.speed
  188. # 越界处理
  189. if self.__x < 0:
  190. self.__x = 0
  191. def moveRight(self):
  192. '''
  193. 向右移动
  194. :return:
  195. '''
  196. self.__x += self.speed
  197. # 越界处理
  198. if self.__x > bgWidth - self.width:
  199. self.__x = bgWidth - self.width
  200. def moveUp(self):
  201. '''
  202. 向上移动
  203. :return:
  204. '''
  205. self.__y -= self.speed
  206. # 越界处理
  207. if self.__y < 0:
  208. self.__y = 0
  209. def moveDown(self):
  210. '''
  211. 向下移动
  212. :return:
  213. '''
  214. self.__y += self.speed
  215. # 越界处理
  216. if self.__y > bgHeight - self.height:
  217. self.__y = bgHeight - self.height
  218. # 全局变量
  219. bgWidth = 0
  220. bgHeight = 0
  221. # 敌方飞机
  222. enemyPlane = None
  223. def start():
  224. global bgWidth, bgHeight,enemyPlane
  225. # 初始化
  226. pygame.init()
  227. # 加载背景图片
  228. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  229. # 获取背景图片的大小
  230. bgWidth = bgSurface.get_width()
  231. bgHeight = bgSurface.get_height()
  232. # 创建游戏窗口
  233. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  234. # 窗口标题
  235. pygame.display.set_caption( '飞机大战')
  236. # 加载图标图片
  237. iconSurface = pygame.image.load( 'img/app.ico')
  238. # 设置图标
  239. pygame.display.set_icon(iconSurface)
  240. # 创建飞机类
  241. plane = Plane( 200, 600, window)
  242. # 创建敌方飞机类
  243. enemyPlane = EnemyPlane(window)
  244. # 保证窗口不关闭
  245. while True:
  246. # 展示背景
  247. window.blit(bgSurface, ( 0, 0))
  248. # 展示我方飞机
  249. plane.display()
  250. # 展示敌方飞机
  251. enemyPlane.display()
  252. # 刷新
  253. pygame.display.flip()
  254. # 处理窗口关闭
  255. eventList = pygame.event.get()
  256. for event in eventList:
  257. # 判断是否是退出类型
  258. if event.type == QUIT:
  259. # 退出游戏
  260. pygame.quit()
  261. # 退出程序
  262. sys.exit()
  263. elif event.type == KEYDOWN:
  264. if event.key == K_RETURN:
  265. # 发射子弹
  266. plane.fire()
  267. # 处理按压事件
  268. # 返回所有键对应的状态 0:没有按压 1:有按压
  269. states = pygame.key.get_pressed()
  270. # 有按压事件
  271. if 1 in states:
  272. # 是否是关心的a d s w键
  273. if states[K_a]: # 0 1
  274. plane.moveLeft()
  275. elif states[K_d]:
  276. plane.moveRight()
  277. elif states[K_w]:
  278. plane.moveUp()
  279. elif states[K_s]:
  280. plane.moveDown()
  281. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  282. # if event.key == K_a:
  283. # plane.moveLeft()
  284. # elif event.key == K_d:
  285. # plane.moveRight()
  286. # elif event.key == K_w:
  287. # plane.moveUp()
  288. # elif event.key == K_s:
  289. # plane.moveDown()
  290. if __name__ == '__main__':
  291. start()

批量产生敌方飞机:


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. import random
  13. """------------------ 敌方飞机类 ------------------"""
  14. class EnemyPlane:
  15. def __init__(self, window):
  16. # 被展示到的窗口
  17. self.__window = window
  18. # 自动移动
  19. self.__speed = 1
  20. # 重置
  21. self.__reset()
  22. def getX(self):
  23. return self.__x
  24. def getY(self):
  25. return self.__y
  26. def display(self):
  27. '''
  28. 被展示
  29. :return:
  30. '''
  31. # 展示我方飞机
  32. self.__window.blit(self.__surface, (self.__x, self.__y))
  33. # 自动移动
  34. self.autoMove()
  35. def autoMove(self):
  36. '''
  37. 自动移动
  38. :return:
  39. '''
  40. self.__y += self.__speed
  41. # 如果越界 需要重置
  42. if self.__y>bgHeight-self.height:
  43. self.__reset()
  44. def reUse(self):
  45. '''
  46. 飞机被打死后者飞机越界 需要重新复用飞机
  47. :return:
  48. '''
  49. self.__reset()
  50. def __reset(self):
  51. '''
  52. 重置
  53. :return:
  54. '''
  55. # 飞机展示的Surface
  56. self.__surface = pygame.image.load( 'img/img-plane_{}.png'.format(random.randint( 1, 7)))
  57. # 控件宽度和高度
  58. self.width = self.__surface.get_width()
  59. self.height = self.__surface.get_height()
  60. # 坐标
  61. self.__x = random.randint( 0,bgWidth-self.width)
  62. self.__y = -random.randint( 3*self.height, 10*self.height)
  63. """------------------ 子弹类 ------------------"""
  64. class Bullet:
  65. def __init__(self, px, py, pw, window):
  66. # 需要展示的窗口
  67. self.__window = window
  68. # 展示的控件
  69. self.__surface = pygame.image.load( 'img/bullet_10.png')
  70. # 控件宽度和高度
  71. self.width = self.__surface.get_width()
  72. self.height = self.__surface.get_height()
  73. # bx = px + (pw - bw) / 2
  74. # by = py - bh / 2
  75. self.__x = px + (pw - self.width) / 2
  76. self.__y = py - self.height / 2
  77. # 自动运行速度
  78. self.__speed = 2
  79. def display(self):
  80. '''
  81. 展示控件
  82. :return:
  83. '''
  84. self.__window.blit(self.__surface, (self.__x, self.__y))
  85. # 自动移动
  86. self.autoMove()
  87. def autoMove(self):
  88. '''
  89. 自动移动
  90. :return:
  91. '''
  92. self.__y -= self.__speed
  93. def needDestroy(self):
  94. '''
  95. 是否需要被销毁
  96. :return:
  97. '''
  98. return self.__y <= -self.height
  99. def hasShootEnemey(self,enemy):
  100. '''
  101. 是否已经射击到敌方飞机
  102. :param enemy: EnemyPlane
  103. :return:
  104. '''
  105. # 子弹矩形
  106. bulletRect = pygame.Rect(self.__x,self.__y,self.width,self.height)
  107. # 敌方飞机矩形
  108. enemyRect = pygame.Rect(enemy.getX(),enemy.getY(),enemy.width,enemy.height)
  109. return bulletRect.colliderect(enemyRect)
  110. """------------------ 我方飞机类 ------------------"""
  111. class Plane:
  112. def __init__(self, x, y, window):
  113. # 坐标
  114. self.__x = x
  115. self.__y = y
  116. # 被展示到的窗口
  117. self.__window = window
  118. # 飞机展示的Surface
  119. self.__surface = pygame.image.load( 'img/hero2.png')
  120. # 控件宽度和高度
  121. self.width = self.__surface.get_width()
  122. self.height = self.__surface.get_height()
  123. # 运动速度
  124. self.speed = 1
  125. # 发射的子弹容器
  126. self.__bullets = []
  127. # 血量值
  128. self.blood = 5
  129. def hasCollision(self,enemy):
  130. '''
  131. 是否和敌方飞机发生了碰撞
  132. :param enemy:
  133. :return:
  134. '''
  135. # 我方飞机矩形
  136. myRect = pygame.Rect(self.__x,self.__y,self.width,self.height)
  137. # 敌方飞机矩形
  138. enemyRect = pygame.Rect(enemy.getX(),enemy.getY(),enemy.width,enemy.height)
  139. return myRect.colliderect(enemyRect)
  140. def display(self):
  141. '''
  142. 被展示
  143. :return:
  144. '''
  145. # 展示我方飞机
  146. self.__window.blit(self.__surface, (self.__x, self.__y))
  147. # 在循环遍历的时候做了移除元素的操作
  148. # 先销毁越界子弹
  149. # for bullet in self.__bullets:
  150. # # 如果子弹需要销毁
  151. # if bullet.needDestroy():
  152. # # 移除子弹对象
  153. # self.__bullets.remove(bullet)
  154. # # 内存销毁对象
  155. # del bullet
  156. # 检测我方飞机和敌方飞机是否发生了碰撞
  157. for enemy in enemyList:
  158. # 判断是否发生了碰撞
  159. if self.hasCollision(enemy):
  160. # 我方飞机掉血
  161. self.blood -= 1
  162. print( '血量',self.blood)
  163. # 敌方飞机重复使用
  164. enemy.reUse()
  165. # 子弹是否和敌方飞机发生了碰撞
  166. for index in range(len(self.__bullets) - 1, -1, -1):
  167. bullet = self.__bullets[index]
  168. for enemy in enemyList:
  169. # bullet和enemy是否发生了碰撞
  170. if bullet.hasShootEnemey(enemy):
  171. # 子弹销毁
  172. self.__bullets.remove(bullet)
  173. del bullet
  174. # 敌方飞机打掉
  175. # 敌方飞机复用
  176. enemy.reUse()
  177. # 退出里层循环
  178. break
  179. # 展示
  180. for index in range(len(self.__bullets) - 1, -1, -1):
  181. bullet = self.__bullets[index]
  182. if bullet.needDestroy():
  183. # 移除子弹对象
  184. self.__bullets.remove(bullet)
  185. # 内存销毁对象
  186. del bullet
  187. else:
  188. # 把子弹展示出来
  189. bullet.display()
  190. # # 把所有发射的子弹渲染出来
  191. # for bullet in self.__bullets:
  192. # # 把子弹展示出来
  193. # bullet.display()
  194. def fire(self):
  195. '''
  196. 发射子弹
  197. :return:
  198. '''
  199. # 子弹的x和y
  200. # bx = px + (pw - bw) / 2
  201. # by = py - bh / 2
  202. # 创建子弹对象
  203. bullet = Bullet(self.__x, self.__y, self.width, self.__window)
  204. # 子弹对象添加到子弹列表中
  205. self.__bullets.append(bullet)
  206. def moveLeft(self):
  207. '''
  208. 向左移动
  209. :return:
  210. '''
  211. self.__x -= self.speed
  212. # 越界处理
  213. if self.__x < 0:
  214. self.__x = 0
  215. def moveRight(self):
  216. '''
  217. 向右移动
  218. :return:
  219. '''
  220. self.__x += self.speed
  221. # 越界处理
  222. if self.__x > bgWidth - self.width:
  223. self.__x = bgWidth - self.width
  224. def moveUp(self):
  225. '''
  226. 向上移动
  227. :return:
  228. '''
  229. self.__y -= self.speed
  230. # 越界处理
  231. if self.__y < 0:
  232. self.__y = 0
  233. def moveDown(self):
  234. '''
  235. 向下移动
  236. :return:
  237. '''
  238. self.__y += self.speed
  239. # 越界处理
  240. if self.__y > bgHeight - self.height:
  241. self.__y = bgHeight - self.height
  242. # 全局变量
  243. bgWidth = 0
  244. bgHeight = 0
  245. # 敌方飞机
  246. enemyList = []
  247. def start():
  248. global bgWidth, bgHeight,enemyPlane
  249. # 初始化
  250. pygame.init()
  251. # 加载背景图片
  252. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  253. # 获取背景图片的大小
  254. bgWidth = bgSurface.get_width()
  255. bgHeight = bgSurface.get_height()
  256. # 创建游戏窗口
  257. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  258. # 窗口标题
  259. pygame.display.set_caption( '飞机大战')
  260. # 加载图标图片
  261. iconSurface = pygame.image.load( 'img/app.ico')
  262. # 设置图标
  263. pygame.display.set_icon(iconSurface)
  264. # 创建飞机类
  265. plane = Plane( 200, 600, window)
  266. # 创建5个敌方飞机对象
  267. for index in range( 0, 5):
  268. enemyList.append(EnemyPlane(window))
  269. # 保证窗口不关闭
  270. while True:
  271. # 展示背景
  272. window.blit(bgSurface, ( 0, 0))
  273. # 展示我方飞机
  274. plane.display()
  275. # 展示敌方飞机
  276. for enemy in enemyList:
  277. enemy.display()
  278. # 刷新
  279. pygame.display.flip()
  280. # 处理窗口关闭
  281. eventList = pygame.event.get()
  282. for event in eventList:
  283. # 判断是否是退出类型
  284. if event.type == QUIT:
  285. # 退出游戏
  286. pygame.quit()
  287. # 退出程序
  288. sys.exit()
  289. elif event.type == KEYDOWN:
  290. if event.key == K_RETURN:
  291. # 发射子弹
  292. plane.fire()
  293. # 处理按压事件
  294. # 返回所有键对应的状态 0:没有按压 1:有按压
  295. states = pygame.key.get_pressed()
  296. # 有按压事件
  297. if 1 in states:
  298. # 是否是关心的a d s w键
  299. if states[K_a]: # 0 1
  300. plane.moveLeft()
  301. elif states[K_d]:
  302. plane.moveRight()
  303. elif states[K_w]:
  304. plane.moveUp()
  305. elif states[K_s]:
  306. plane.moveDown()
  307. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  308. # if event.key == K_a:
  309. # plane.moveLeft()
  310. # elif event.key == K_d:
  311. # plane.moveRight()
  312. # elif event.key == K_w:
  313. # plane.moveUp()
  314. # elif event.key == K_s:
  315. # plane.moveDown()
  316. if __name__ == '__main__':
  317. start()

20.游戏结束


  
  1. import pygame
  2. from pygame.locals import *
  3. import sys
  4. """
  5. 1.标题和图标
  6. 2.退出事件
  7. """
  8. """
  9. 飞机属性: x,y window
  10. 飞机行为:被展示行为display
  11. """
  12. import random
  13. """------------------ 敌方飞机类 ------------------"""
  14. class EnemyPlane:
  15. def __init__(self, window):
  16. # 被展示到的窗口
  17. self.__window = window
  18. # 自动移动
  19. self.__speed = 1
  20. # 重置
  21. self.__reset()
  22. def getX(self):
  23. return self.__x
  24. def getY(self):
  25. return self.__y
  26. def display(self):
  27. '''
  28. 被展示
  29. :return:
  30. '''
  31. # 展示我方飞机
  32. self.__window.blit(self.__surface, (self.__x, self.__y))
  33. def autoMove(self):
  34. '''
  35. 自动移动
  36. :return:
  37. '''
  38. self.__y += self.__speed
  39. # 如果越界 需要重置
  40. if self.__y>bgHeight-self.height:
  41. self.__reset()
  42. def reUse(self):
  43. '''
  44. 飞机被打死后者飞机越界 需要重新复用飞机
  45. :return:
  46. '''
  47. self.__reset()
  48. def __reset(self):
  49. '''
  50. 重置
  51. :return:
  52. '''
  53. # 飞机展示的Surface
  54. self.__surface = pygame.image.load( 'img/img-plane_{}.png'.format(random.randint( 1, 7)))
  55. # 控件宽度和高度
  56. self.width = self.__surface.get_width()
  57. self.height = self.__surface.get_height()
  58. # 坐标
  59. self.__x = random.randint( 0,bgWidth-self.width)
  60. self.__y = -random.randint( 3*self.height, 10*self.height)
  61. """------------------ 子弹类 ------------------"""
  62. class Bullet:
  63. def __init__(self, px, py, pw, window):
  64. # 需要展示的窗口
  65. self.__window = window
  66. # 展示的控件
  67. self.__surface = pygame.image.load( 'img/bullet_10.png')
  68. # 控件宽度和高度
  69. self.width = self.__surface.get_width()
  70. self.height = self.__surface.get_height()
  71. # bx = px + (pw - bw) / 2
  72. # by = py - bh / 2
  73. self.__x = px + (pw - self.width) / 2
  74. self.__y = py - self.height / 2
  75. # 自动运行速度
  76. self.__speed = 2
  77. def display(self):
  78. '''
  79. 展示控件
  80. :return:
  81. '''
  82. self.__window.blit(self.__surface, (self.__x, self.__y))
  83. # 自动移动
  84. self.autoMove()
  85. def autoMove(self):
  86. '''
  87. 自动移动
  88. :return:
  89. '''
  90. self.__y -= self.__speed
  91. def needDestroy(self):
  92. '''
  93. 是否需要被销毁
  94. :return:
  95. '''
  96. return self.__y <= -self.height
  97. def hasShootEnemey(self,enemy):
  98. '''
  99. 是否已经射击到敌方飞机
  100. :param enemy: EnemyPlane
  101. :return:
  102. '''
  103. # 子弹矩形
  104. bulletRect = pygame.Rect(self.__x,self.__y,self.width,self.height)
  105. # 敌方飞机矩形
  106. enemyRect = pygame.Rect(enemy.getX(),enemy.getY(),enemy.width,enemy.height)
  107. return bulletRect.colliderect(enemyRect)
  108. """------------------ 我方飞机类 ------------------"""
  109. class Plane:
  110. def __init__(self, x, y, window):
  111. # 坐标
  112. self.__x = x
  113. self.__y = y
  114. # 被展示到的窗口
  115. self.__window = window
  116. # 飞机展示的Surface
  117. self.__surface = pygame.image.load( 'img/hero2.png')
  118. # 控件宽度和高度
  119. self.width = self.__surface.get_width()
  120. self.height = self.__surface.get_height()
  121. # 运动速度
  122. self.speed = 1
  123. # 发射的子弹容器
  124. self.__bullets = []
  125. # 血量值
  126. self.blood = 5
  127. def hasCollision(self,enemy):
  128. '''
  129. 是否和敌方飞机发生了碰撞
  130. :param enemy:
  131. :return:
  132. '''
  133. # 我方飞机矩形
  134. myRect = pygame.Rect(self.__x,self.__y,self.width,self.height)
  135. # 敌方飞机矩形
  136. enemyRect = pygame.Rect(enemy.getX(),enemy.getY(),enemy.width,enemy.height)
  137. return myRect.colliderect(enemyRect)
  138. def display(self):
  139. '''
  140. 被展示
  141. :return:
  142. '''
  143. # 展示我方飞机
  144. self.__window.blit(self.__surface, (self.__x, self.__y))
  145. # 在循环遍历的时候做了移除元素的操作
  146. # 先销毁越界子弹
  147. # for bullet in self.__bullets:
  148. # # 如果子弹需要销毁
  149. # if bullet.needDestroy():
  150. # # 移除子弹对象
  151. # self.__bullets.remove(bullet)
  152. # # 内存销毁对象
  153. # del bullet
  154. # 检测我方飞机和敌方飞机是否发生了碰撞
  155. for enemy in enemyList:
  156. # 判断是否发生了碰撞
  157. if self.hasCollision(enemy):
  158. # 我方飞机掉血
  159. self.blood -= 1
  160. print( '血量',self.blood)
  161. # 敌方飞机重复使用
  162. enemy.reUse()
  163. # 子弹是否和敌方飞机发生了碰撞
  164. for index in range(len(self.__bullets) - 1, -1, -1):
  165. bullet = self.__bullets[index]
  166. for enemy in enemyList:
  167. # bullet和enemy是否发生了碰撞
  168. if bullet.hasShootEnemey(enemy):
  169. # 子弹销毁
  170. self.__bullets.remove(bullet)
  171. del bullet
  172. # 敌方飞机打掉
  173. # 敌方飞机复用
  174. enemy.reUse()
  175. # 退出里层循环
  176. break
  177. # 展示
  178. for index in range(len(self.__bullets) - 1, -1, -1):
  179. bullet = self.__bullets[index]
  180. if bullet.needDestroy():
  181. # 移除子弹对象
  182. self.__bullets.remove(bullet)
  183. # 内存销毁对象
  184. del bullet
  185. else:
  186. # 把子弹展示出来
  187. bullet.display()
  188. # # 把所有发射的子弹渲染出来
  189. # for bullet in self.__bullets:
  190. # # 把子弹展示出来
  191. # bullet.display()
  192. def fire(self):
  193. '''
  194. 发射子弹
  195. :return:
  196. '''
  197. # 子弹的x和y
  198. # bx = px + (pw - bw) / 2
  199. # by = py - bh / 2
  200. # 创建子弹对象
  201. bullet = Bullet(self.__x, self.__y, self.width, self.__window)
  202. # 子弹对象添加到子弹列表中
  203. self.__bullets.append(bullet)
  204. def moveLeft(self):
  205. '''
  206. 向左移动
  207. :return:
  208. '''
  209. self.__x -= self.speed
  210. # 越界处理
  211. if self.__x < 0:
  212. self.__x = 0
  213. def moveRight(self):
  214. '''
  215. 向右移动
  216. :return:
  217. '''
  218. self.__x += self.speed
  219. # 越界处理
  220. if self.__x > bgWidth - self.width:
  221. self.__x = bgWidth - self.width
  222. def moveUp(self):
  223. '''
  224. 向上移动
  225. :return:
  226. '''
  227. self.__y -= self.speed
  228. # 越界处理
  229. if self.__y < 0:
  230. self.__y = 0
  231. def moveDown(self):
  232. '''
  233. 向下移动
  234. :return:
  235. '''
  236. self.__y += self.speed
  237. # 越界处理
  238. if self.__y > bgHeight - self.height:
  239. self.__y = bgHeight - self.height
  240. # 全局变量
  241. bgWidth = 0
  242. bgHeight = 0
  243. # 敌方飞机
  244. enemyList = []
  245. def start():
  246. global bgWidth, bgHeight,enemyPlane
  247. # 初始化
  248. pygame.init()
  249. # 加载背景图片
  250. bgSurface = pygame.image.load( 'img/img_bg_level_1.jpg')
  251. # 获取背景图片的大小
  252. bgWidth = bgSurface.get_width()
  253. bgHeight = bgSurface.get_height()
  254. # 创建游戏窗口
  255. window = pygame.display.set_mode(size=(bgWidth, bgHeight))
  256. # 窗口标题
  257. pygame.display.set_caption( '飞机大战')
  258. # 加载图标图片
  259. iconSurface = pygame.image.load( 'img/app.ico')
  260. # 设置图标
  261. pygame.display.set_icon(iconSurface)
  262. # 创建飞机类
  263. plane = Plane( 200, 600, window)
  264. # 创建5个敌方飞机对象
  265. for index in range( 0, 5):
  266. enemyList.append(EnemyPlane(window))
  267. # 游戏结束文字
  268. font = pygame.font.Font( 'font/happy.ttf', 50)
  269. gameOverSurface = font.render( '游戏结束', True,( 255, 255, 255))
  270. # 保证窗口不关闭
  271. while True:
  272. # 展示背景
  273. window.blit(bgSurface, ( 0, 0))
  274. # 展示我方飞机
  275. plane.display()
  276. # 展示敌方飞机
  277. for enemy in enemyList:
  278. enemy.display()
  279. # 处理窗口关闭
  280. eventList = pygame.event.get()
  281. for event in eventList:
  282. # 判断是否是退出类型
  283. if event.type == QUIT:
  284. # 退出游戏
  285. pygame.quit()
  286. # 退出程序
  287. sys.exit()
  288. elif event.type == KEYDOWN:
  289. if event.key == K_RETURN:
  290. # 发射子弹
  291. plane.fire()
  292. # 检测是否停止
  293. if plane.blood<= 0:
  294. # 游戏停止
  295. window.blit(gameOverSurface,((bgWidth-gameOverSurface.get_width())/ 2,(bgHeight-gameOverSurface.get_height())/ 2))
  296. # 刷新
  297. pygame.display.flip()
  298. continue
  299. # 敌方飞机自动移动
  300. for enemy in enemyList:
  301. enemy.autoMove()
  302. # 刷新
  303. pygame.display.flip()
  304. # 处理窗口关闭
  305. eventList = pygame.event.get()
  306. for event in eventList:
  307. # 判断是否是退出类型
  308. if event.type == QUIT:
  309. # 退出游戏
  310. pygame.quit()
  311. # 退出程序
  312. sys.exit()
  313. elif event.type == KEYDOWN:
  314. if event.key == K_RETURN:
  315. # 发射子弹
  316. plane.fire()
  317. # 处理按压事件
  318. # 返回所有键对应的状态 0:没有按压 1:有按压
  319. states = pygame.key.get_pressed()
  320. # 有按压事件
  321. if 1 in states:
  322. # 是否是关心的a d s w键
  323. if states[K_a]: # 0 1
  324. plane.moveLeft()
  325. elif states[K_d]:
  326. plane.moveRight()
  327. elif states[K_w]:
  328. plane.moveUp()
  329. elif states[K_s]:
  330. plane.moveDown()
  331. # elif event.type == KEYDOWN: # 按下事件 一次按下只能执行一次
  332. # if event.key == K_a:
  333. # plane.moveLeft()
  334. # elif event.key == K_d:
  335. # plane.moveRight()
  336. # elif event.key == K_w:
  337. # plane.moveUp()
  338. # elif event.key == K_s:
  339. # plane.moveDown()
  340. if __name__ == '__main__':
  341. start()

 

代码下载:

链接: https://pan.baidu.com/s/1yScaUX3Aumos_iMrI4ZKjw 提取码: xwct 

 

注意:

打包时把所有的图片和文字路径改成绝对路径:

 

程序打包

PyInstaller

将Python应用打包成可执行文件

安装和使用

安装:

pip install pyinstaller

打包命令:

pyinstaller-F xx.py -w

最后说下-p这个参数

                -p:添加搜索路径

这个参数是告诉打包工具到哪去搜索依耐性文件,此次我们没有使用-p参数是因为工具默认为到python安装路径下的Lib 以及 Lib文件夹下的site-packages。

Lib文件夹下是python自带的一些库,site-packages为pip安装的库。此次的第三方库我都是通过pip安装的,所以不用加-p参数。如果使用了第三方库并且不再这两个路径下的需要加上:-p 库地址,告诉打包工具你使用的库在哪
 

3.更改exe文件的图标

自定义打包出来的exe文件图标,需要使用-i参数,同时需要准备一个ico格式的图片,贴上一个在线ico制作网站:

ico图片转换

 

pyinstaller -F --icon=1234.ico run.py

打包好的下载:

链接: https://pan.baidu.com/s/1EFsyTRQ6C6S82c0Tw33OYA 提取码: qkc6

注意: 如果打包后打不开文件,就把代码中用到的资源路径改成绝对路径后在打包


转载:https://blog.csdn.net/qq_15204179/article/details/104595193
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场