diff --git "a/Aircraft Wars/hm_01_pygame\345\205\245\351\227\250.py" "b/Aircraft Wars/hm_01_pygame\345\205\245\351\227\250.py" new file mode 100644 index 0000000..3fb4a70 --- /dev/null +++ "b/Aircraft Wars/hm_01_pygame\345\205\245\351\227\250.py" @@ -0,0 +1,8 @@ +import pygame + +pygame.init() + +# 编写游戏的代码 +print("游戏的代码...") + +pygame.quit() \ No newline at end of file diff --git "a/Aircraft Wars/hm_02_\344\275\277\347\224\250Rect\346\217\217\350\277\260\350\213\261\351\233\204.py" "b/Aircraft Wars/hm_02_\344\275\277\347\224\250Rect\346\217\217\350\277\260\350\213\261\351\233\204.py" new file mode 100644 index 0000000..0481688 --- /dev/null +++ "b/Aircraft Wars/hm_02_\344\275\277\347\224\250Rect\346\217\217\350\277\260\350\213\261\351\233\204.py" @@ -0,0 +1,7 @@ +import pygame + +hero_rect = pygame.Rect(100, 500, 120, 125) + +print("英雄的原点 %d %d" % (hero_rect.x, hero_rect.y)) +print("英雄的尺寸 %d %d" % (hero_rect.width, hero_rect.height)) +print("%d %d" % hero_rect.size) diff --git "a/Aircraft Wars/hm_03_\345\210\233\345\273\272\346\270\270\346\210\217\347\252\227\345\217\243.py" "b/Aircraft Wars/hm_03_\345\210\233\345\273\272\346\270\270\346\210\217\347\252\227\345\217\243.py" new file mode 100644 index 0000000..5fd3cfe --- /dev/null +++ "b/Aircraft Wars/hm_03_\345\210\233\345\273\272\346\270\270\346\210\217\347\252\227\345\217\243.py" @@ -0,0 +1,11 @@ +import pygame + +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +while True: + pass + +pygame.quit() diff --git "a/Aircraft Wars/hm_04_\347\273\230\345\210\266\345\233\276\345\203\217.py" "b/Aircraft Wars/hm_04_\347\273\230\345\210\266\345\233\276\345\203\217.py" new file mode 100644 index 0000000..fefa7f3 --- /dev/null +++ "b/Aircraft Wars/hm_04_\347\273\230\345\210\266\345\233\276\345\203\217.py" @@ -0,0 +1,19 @@ +import pygame + +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +# 绘制背景图像 +# 1> 加载图像数据 +bg = pygame.image.load("./images/background.png") +# 2> blit 绘制图像 +screen.blit(bg, (0, 0)) +# 3> update 更新屏幕显示 +pygame.display.update() + +while True: + pass + +pygame.quit() diff --git "a/Aircraft Wars/hm_05_\347\273\230\345\210\266\350\213\261\351\233\204\345\233\276\345\203\217.py" "b/Aircraft Wars/hm_05_\347\273\230\345\210\266\350\213\261\351\233\204\345\233\276\345\203\217.py" new file mode 100644 index 0000000..404e2a5 --- /dev/null +++ "b/Aircraft Wars/hm_05_\347\273\230\345\210\266\350\213\261\351\233\204\345\233\276\345\203\217.py" @@ -0,0 +1,24 @@ +import pygame + +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +# 绘制背景图像 +# 1> 加载图像数据 +bg = pygame.image.load("./images/background.png") +# 2> blit 绘制图像 +screen.blit(bg, (0, 0)) +# 3> update 更新屏幕显示 +pygame.display.update() + +# 绘制英雄的飞机 +hero = pygame.image.load("./images/me1.png") +screen.blit(hero, (150, 300)) +pygame.display.update() + +while True: + pass + +pygame.quit() diff --git "a/Aircraft Wars/hm_06_update\346\226\271\346\263\225.py" "b/Aircraft Wars/hm_06_update\346\226\271\346\263\225.py" new file mode 100644 index 0000000..75c763a --- /dev/null +++ "b/Aircraft Wars/hm_06_update\346\226\271\346\263\225.py" @@ -0,0 +1,23 @@ +import pygame + +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +# 绘制背景图像 +bg = pygame.image.load("./images/background.png") +screen.blit(bg, (0, 0)) +# pygame.display.update() + +# 绘制英雄的飞机 +hero = pygame.image.load("./images/me1.png") +screen.blit(hero, (150, 300)) + +# 可以在所有绘制工作完成之后,统一调用update方法 +pygame.display.update() + +while True: + pass + +pygame.quit() diff --git "a/Aircraft Wars/hm_07_\350\256\244\350\257\206\346\270\270\346\210\217\345\276\252\347\216\257.py" "b/Aircraft Wars/hm_07_\350\256\244\350\257\206\346\270\270\346\210\217\345\276\252\347\216\257.py" new file mode 100644 index 0000000..f6205da --- /dev/null +++ "b/Aircraft Wars/hm_07_\350\256\244\350\257\206\346\270\270\346\210\217\345\276\252\347\216\257.py" @@ -0,0 +1,38 @@ +import pygame + +# 游戏的初始化 +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +# 绘制背景图像 +bg = pygame.image.load("./images/background.png") +screen.blit(bg, (0, 0)) +# pygame.display.update() + +# 绘制英雄的飞机 +hero = pygame.image.load("./images/me1.png") +screen.blit(hero, (150, 300)) + +# 可以在所有绘制工作完成之后,统一调用update方法 +pygame.display.update() + +# 创建时钟对象 +clock = pygame.time.Clock() + +# 游戏循环 -> 意味着游戏的正式开始! +i = 0 + +while True: + + # 可以指定循环体内部的代码执行的频率 + clock.tick(1) + + print(i) + + i += 1 + + pass + +pygame.quit() diff --git "a/Aircraft Wars/hm_08_\346\233\264\346\226\260\350\213\261\351\233\204\344\275\215\347\275\256.py" "b/Aircraft Wars/hm_08_\346\233\264\346\226\260\350\213\261\351\233\204\344\275\215\347\275\256.py" new file mode 100644 index 0000000..d7b6219 --- /dev/null +++ "b/Aircraft Wars/hm_08_\346\233\264\346\226\260\350\213\261\351\233\204\344\275\215\347\275\256.py" @@ -0,0 +1,43 @@ +import pygame + +# 游戏的初始化 +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +# 绘制背景图像 +bg = pygame.image.load("./images/background.png") +screen.blit(bg, (0, 0)) +# pygame.display.update() + +# 绘制英雄的飞机 +hero = pygame.image.load("./images/me1.png") +screen.blit(hero, (150, 300)) + +# 可以在所有绘制工作完成之后,统一调用update方法 +pygame.display.update() + +# 创建时钟对象 +clock = pygame.time.Clock() + +# 1. 定义rect记录飞机的初始位置 +hero_rect = pygame.Rect(150, 300, 102, 126) + +# 游戏循环 -> 意味着游戏的正式开始! +while True: + + # 可以指定循环体内部的代码执行的频率 + clock.tick(60) + + # 2. 修改飞机的位置 + hero_rect.y -= 1 + + # 3. 调用blit方法绘制图 + screen.blit(bg, (0, 0)) + screen.blit(hero, hero_rect) + + # 4. 调用update方法更新显示 + pygame.display.update() + +pygame.quit() diff --git "a/Aircraft Wars/hm_09_\350\213\261\351\233\204\345\276\252\347\216\257\351\243\236\350\241\214.py" "b/Aircraft Wars/hm_09_\350\213\261\351\233\204\345\276\252\347\216\257\351\243\236\350\241\214.py" new file mode 100644 index 0000000..42e9183 --- /dev/null +++ "b/Aircraft Wars/hm_09_\350\213\261\351\233\204\345\276\252\347\216\257\351\243\236\350\241\214.py" @@ -0,0 +1,47 @@ +import pygame + +# 游戏的初始化 +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +# 绘制背景图像 +bg = pygame.image.load("./images/background.png") +screen.blit(bg, (0, 0)) +# pygame.display.update() + +# 绘制英雄的飞机 +hero = pygame.image.load("./images/me1.png") +screen.blit(hero, (150, 300)) + +# 可以在所有绘制工作完成之后,统一调用update方法 +pygame.display.update() + +# 创建时钟对象 +clock = pygame.time.Clock() + +# 1. 定义rect记录飞机的初始位置 +hero_rect = pygame.Rect(150, 300, 102, 126) + +# 游戏循环 -> 意味着游戏的正式开始! +while True: + + # 可以指定循环体内部的代码执行的频率 + clock.tick(60) + + # 2. 修改飞机的位置 + hero_rect.y -= 1 + + # 判断飞机的位置 + if hero_rect.y <= -102: + hero_rect.y = 700 + + # 3. 调用blit方法绘制图 + screen.blit(bg, (0, 0)) + screen.blit(hero, hero_rect) + + # 4. 调用update方法更新显示 + pygame.display.update() + +pygame.quit() diff --git "a/Aircraft Wars/hm_10_\344\272\213\344\273\266\347\233\221\345\220\254.py" "b/Aircraft Wars/hm_10_\344\272\213\344\273\266\347\233\221\345\220\254.py" new file mode 100644 index 0000000..c73b075 --- /dev/null +++ "b/Aircraft Wars/hm_10_\344\272\213\344\273\266\347\233\221\345\220\254.py" @@ -0,0 +1,52 @@ +import pygame + +# 游戏的初始化 +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +# 绘制背景图像 +bg = pygame.image.load("./images/background.png") +screen.blit(bg, (0, 0)) +# pygame.display.update() + +# 绘制英雄的飞机 +hero = pygame.image.load("./images/me1.png") +screen.blit(hero, (150, 300)) + +# 可以在所有绘制工作完成之后,统一调用update方法 +pygame.display.update() + +# 创建时钟对象 +clock = pygame.time.Clock() + +# 1. 定义rect记录飞机的初始位置 +hero_rect = pygame.Rect(150, 300, 102, 126) + +# 游戏循环 -> 意味着游戏的正式开始! +while True: + + # 可以指定循环体内部的代码执行的频率 + clock.tick(60) + + # 捕获事件 + event_list = pygame.event.get() + if len(event_list) > 0: + print(event_list) + + # 2. 修改飞机的位置 + hero_rect.y -= 1 + + # 判断飞机的位置 + if hero_rect.y <= -102: + hero_rect.y = 700 + + # 3. 调用blit方法绘制图 + screen.blit(bg, (0, 0)) + screen.blit(hero, hero_rect) + + # 4. 调用update方法更新显示 + pygame.display.update() + +pygame.quit() diff --git "a/Aircraft Wars/hm_11_\347\233\221\345\220\254\351\200\200\345\207\272\344\272\213\344\273\266.py" "b/Aircraft Wars/hm_11_\347\233\221\345\220\254\351\200\200\345\207\272\344\272\213\344\273\266.py" new file mode 100644 index 0000000..727e4ad --- /dev/null +++ "b/Aircraft Wars/hm_11_\347\233\221\345\220\254\351\200\200\345\207\272\344\272\213\344\273\266.py" @@ -0,0 +1,60 @@ +import pygame + +# 游戏的初始化 +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +# 绘制背景图像 +bg = pygame.image.load("./images/background.png") +screen.blit(bg, (0, 0)) +# pygame.display.update() + +# 绘制英雄的飞机 +hero = pygame.image.load("./images/me1.png") +screen.blit(hero, (150, 300)) + +# 可以在所有绘制工作完成之后,统一调用update方法 +pygame.display.update() + +# 创建时钟对象 +clock = pygame.time.Clock() + +# 1. 定义rect记录飞机的初始位置 +hero_rect = pygame.Rect(150, 300, 102, 126) + +# 游戏循环 -> 意味着游戏的正式开始! +while True: + + # 可以指定循环体内部的代码执行的频率 + clock.tick(60) + + # 监听事件 + for event in pygame.event.get(): + + # 判断事件类型是否是退出事件 + if event.type == pygame.QUIT: + print("游戏退出...") + + # quit 卸载所有的模块 + pygame.quit() + + # exit() 直接终止当前正在执行的程序 + exit() + + # 2. 修改飞机的位置 + hero_rect.y -= 1 + + # 判断飞机的位置 + if hero_rect.y <= -102: + hero_rect.y = 700 + + # 3. 调用blit方法绘制图 + screen.blit(bg, (0, 0)) + screen.blit(hero, hero_rect) + + # 4. 调用update方法更新显示 + pygame.display.update() + +pygame.quit() diff --git "a/Aircraft Wars/hm_12_\346\274\224\347\273\203\347\262\276\347\201\265.py" "b/Aircraft Wars/hm_12_\346\274\224\347\273\203\347\262\276\347\201\265.py" new file mode 100644 index 0000000..4a96d27 --- /dev/null +++ "b/Aircraft Wars/hm_12_\346\274\224\347\273\203\347\262\276\347\201\265.py" @@ -0,0 +1,78 @@ +import pygame +from plane_sprites import * + + +# 游戏的初始化 +pygame.init() + +# 创建游戏的窗口 +screen = pygame.display.set_mode((480, 700)) + +# 绘制背景图像 +bg = pygame.image.load("./images/background.png") +screen.blit(bg, (0, 0)) +# pygame.display.update() + +# 绘制英雄的飞机 +hero = pygame.image.load("./images/me1.png") +screen.blit(hero, (150, 300)) + +# 可以在所有绘制工作完成之后,统一调用update方法 +pygame.display.update() + +# 创建时钟对象 +clock = pygame.time.Clock() + +# 1. 定义rect记录飞机的初始位置 +hero_rect = pygame.Rect(150, 300, 102, 126) + + +# 创建敌机的精灵 +enemy = GameSprite("./images/enemy1.png") +enemy1 = GameSprite("./images/enemy1.png", 2) +# 创建敌机的精灵组 +enemy_group = pygame.sprite.Group(enemy, enemy1) + + +# 游戏循环 -> 意味着游戏的正式开始! +while True: + + # 可以指定循环体内部的代码执行的频率 + clock.tick(60) + + # 监听事件 + for event in pygame.event.get(): + + # 判断事件类型是否是退出事件 + if event.type == pygame.QUIT: + print("游戏退出...") + + # quit 卸载所有的模块 + pygame.quit() + + # exit() 直接终止当前正在执行的程序 + exit() + + # 2. 修改飞机的位置 + hero_rect.y -= 1 + + # 判断飞机的位置 + if hero_rect.y <= -102: + hero_rect.y = 700 + + # 3. 调用blit方法绘制图 + screen.blit(bg, (0, 0)) + screen.blit(hero, hero_rect) + + # 让精灵组调用两个方法 + # update - 让组中的所有精灵更新位置 + enemy_group.update() + + # draw - 在screen上绘制所有的精灵 + enemy_group.draw(screen) + + + # 4. 调用update方法更新显示 + pygame.display.update() + +pygame.quit() diff --git a/Aircraft Wars/images/again.png b/Aircraft Wars/images/again.png new file mode 100755 index 0000000..4d9c80e Binary files /dev/null and b/Aircraft Wars/images/again.png differ diff --git a/Aircraft Wars/images/background.png b/Aircraft Wars/images/background.png new file mode 100755 index 0000000..8f1c51c Binary files /dev/null and b/Aircraft Wars/images/background.png differ diff --git a/Aircraft Wars/images/bomb.png b/Aircraft Wars/images/bomb.png new file mode 100755 index 0000000..d7f44fa Binary files /dev/null and b/Aircraft Wars/images/bomb.png differ diff --git a/Aircraft Wars/images/bomb_supply.png b/Aircraft Wars/images/bomb_supply.png new file mode 100755 index 0000000..763220c Binary files /dev/null and b/Aircraft Wars/images/bomb_supply.png differ diff --git a/Aircraft Wars/images/bullet1.png b/Aircraft Wars/images/bullet1.png new file mode 100755 index 0000000..c3d289e Binary files /dev/null and b/Aircraft Wars/images/bullet1.png differ diff --git a/Aircraft Wars/images/bullet2.png b/Aircraft Wars/images/bullet2.png new file mode 100755 index 0000000..1481916 Binary files /dev/null and b/Aircraft Wars/images/bullet2.png differ diff --git a/Aircraft Wars/images/bullet_supply.png b/Aircraft Wars/images/bullet_supply.png new file mode 100755 index 0000000..f04be50 Binary files /dev/null and b/Aircraft Wars/images/bullet_supply.png differ diff --git a/Aircraft Wars/images/enemy1.png b/Aircraft Wars/images/enemy1.png new file mode 100755 index 0000000..b58494c Binary files /dev/null and b/Aircraft Wars/images/enemy1.png differ diff --git a/Aircraft Wars/images/enemy1_down1.png b/Aircraft Wars/images/enemy1_down1.png new file mode 100755 index 0000000..eb143a4 Binary files /dev/null and b/Aircraft Wars/images/enemy1_down1.png differ diff --git a/Aircraft Wars/images/enemy1_down2.png b/Aircraft Wars/images/enemy1_down2.png new file mode 100755 index 0000000..d4e02d8 Binary files /dev/null and b/Aircraft Wars/images/enemy1_down2.png differ diff --git a/Aircraft Wars/images/enemy1_down3.png b/Aircraft Wars/images/enemy1_down3.png new file mode 100755 index 0000000..5a4d5bc Binary files /dev/null and b/Aircraft Wars/images/enemy1_down3.png differ diff --git a/Aircraft Wars/images/enemy1_down4.png b/Aircraft Wars/images/enemy1_down4.png new file mode 100755 index 0000000..8cf3b5c Binary files /dev/null and b/Aircraft Wars/images/enemy1_down4.png differ diff --git a/Aircraft Wars/images/enemy2.png b/Aircraft Wars/images/enemy2.png new file mode 100755 index 0000000..71abebe Binary files /dev/null and b/Aircraft Wars/images/enemy2.png differ diff --git a/Aircraft Wars/images/enemy2_down1.png b/Aircraft Wars/images/enemy2_down1.png new file mode 100755 index 0000000..6128d78 Binary files /dev/null and b/Aircraft Wars/images/enemy2_down1.png differ diff --git a/Aircraft Wars/images/enemy2_down2.png b/Aircraft Wars/images/enemy2_down2.png new file mode 100755 index 0000000..07a837e Binary files /dev/null and b/Aircraft Wars/images/enemy2_down2.png differ diff --git a/Aircraft Wars/images/enemy2_down3.png b/Aircraft Wars/images/enemy2_down3.png new file mode 100755 index 0000000..618ca36 Binary files /dev/null and b/Aircraft Wars/images/enemy2_down3.png differ diff --git a/Aircraft Wars/images/enemy2_down4.png b/Aircraft Wars/images/enemy2_down4.png new file mode 100755 index 0000000..1d04321 Binary files /dev/null and b/Aircraft Wars/images/enemy2_down4.png differ diff --git a/Aircraft Wars/images/enemy2_hit.png b/Aircraft Wars/images/enemy2_hit.png new file mode 100755 index 0000000..a119255 Binary files /dev/null and b/Aircraft Wars/images/enemy2_hit.png differ diff --git a/Aircraft Wars/images/enemy3_down1.png b/Aircraft Wars/images/enemy3_down1.png new file mode 100755 index 0000000..96e95b6 Binary files /dev/null and b/Aircraft Wars/images/enemy3_down1.png differ diff --git a/Aircraft Wars/images/enemy3_down2.png b/Aircraft Wars/images/enemy3_down2.png new file mode 100755 index 0000000..a448cfd Binary files /dev/null and b/Aircraft Wars/images/enemy3_down2.png differ diff --git a/Aircraft Wars/images/enemy3_down3.png b/Aircraft Wars/images/enemy3_down3.png new file mode 100755 index 0000000..2c134a3 Binary files /dev/null and b/Aircraft Wars/images/enemy3_down3.png differ diff --git a/Aircraft Wars/images/enemy3_down4.png b/Aircraft Wars/images/enemy3_down4.png new file mode 100755 index 0000000..6ba88bf Binary files /dev/null and b/Aircraft Wars/images/enemy3_down4.png differ diff --git a/Aircraft Wars/images/enemy3_down5.png b/Aircraft Wars/images/enemy3_down5.png new file mode 100755 index 0000000..dbb9634 Binary files /dev/null and b/Aircraft Wars/images/enemy3_down5.png differ diff --git a/Aircraft Wars/images/enemy3_down6.png b/Aircraft Wars/images/enemy3_down6.png new file mode 100755 index 0000000..044f747 Binary files /dev/null and b/Aircraft Wars/images/enemy3_down6.png differ diff --git a/Aircraft Wars/images/enemy3_hit.png b/Aircraft Wars/images/enemy3_hit.png new file mode 100755 index 0000000..72cefd4 Binary files /dev/null and b/Aircraft Wars/images/enemy3_hit.png differ diff --git a/Aircraft Wars/images/enemy3_n1.png b/Aircraft Wars/images/enemy3_n1.png new file mode 100755 index 0000000..e5c9157 Binary files /dev/null and b/Aircraft Wars/images/enemy3_n1.png differ diff --git a/Aircraft Wars/images/enemy3_n2.png b/Aircraft Wars/images/enemy3_n2.png new file mode 100755 index 0000000..bddab2f Binary files /dev/null and b/Aircraft Wars/images/enemy3_n2.png differ diff --git a/Aircraft Wars/images/gameover.png b/Aircraft Wars/images/gameover.png new file mode 100755 index 0000000..483da0d Binary files /dev/null and b/Aircraft Wars/images/gameover.png differ diff --git a/Aircraft Wars/images/life.png b/Aircraft Wars/images/life.png new file mode 100755 index 0000000..b8fd20a Binary files /dev/null and b/Aircraft Wars/images/life.png differ diff --git a/Aircraft Wars/images/me1.png b/Aircraft Wars/images/me1.png new file mode 100755 index 0000000..6eb3c28 Binary files /dev/null and b/Aircraft Wars/images/me1.png differ diff --git a/Aircraft Wars/images/me2.png b/Aircraft Wars/images/me2.png new file mode 100755 index 0000000..cf3129d Binary files /dev/null and b/Aircraft Wars/images/me2.png differ diff --git a/Aircraft Wars/images/me_destroy_1.png b/Aircraft Wars/images/me_destroy_1.png new file mode 100755 index 0000000..cfec1f0 Binary files /dev/null and b/Aircraft Wars/images/me_destroy_1.png differ diff --git a/Aircraft Wars/images/me_destroy_2.png b/Aircraft Wars/images/me_destroy_2.png new file mode 100755 index 0000000..d24a960 Binary files /dev/null and b/Aircraft Wars/images/me_destroy_2.png differ diff --git a/Aircraft Wars/images/me_destroy_3.png b/Aircraft Wars/images/me_destroy_3.png new file mode 100755 index 0000000..575a654 Binary files /dev/null and b/Aircraft Wars/images/me_destroy_3.png differ diff --git a/Aircraft Wars/images/me_destroy_4.png b/Aircraft Wars/images/me_destroy_4.png new file mode 100755 index 0000000..0a2b204 Binary files /dev/null and b/Aircraft Wars/images/me_destroy_4.png differ diff --git a/Aircraft Wars/images/pause_nor.png b/Aircraft Wars/images/pause_nor.png new file mode 100755 index 0000000..8617e5d Binary files /dev/null and b/Aircraft Wars/images/pause_nor.png differ diff --git a/Aircraft Wars/images/pause_pressed.png b/Aircraft Wars/images/pause_pressed.png new file mode 100755 index 0000000..6b8bc1a Binary files /dev/null and b/Aircraft Wars/images/pause_pressed.png differ diff --git a/Aircraft Wars/images/resume_nor.png b/Aircraft Wars/images/resume_nor.png new file mode 100755 index 0000000..9295d0c Binary files /dev/null and b/Aircraft Wars/images/resume_nor.png differ diff --git a/Aircraft Wars/images/resume_pressed.png b/Aircraft Wars/images/resume_pressed.png new file mode 100755 index 0000000..09a8bdf Binary files /dev/null and b/Aircraft Wars/images/resume_pressed.png differ diff --git a/Aircraft Wars/plane_main.py b/Aircraft Wars/plane_main.py new file mode 100644 index 0000000..8575ff3 --- /dev/null +++ b/Aircraft Wars/plane_main.py @@ -0,0 +1,127 @@ +import pygame +from plane_sprites import * + + +class PlaneGame(object): + """飞机大战主游戏""" + + def __init__(self): + print("游戏初始化") + + # 1. 创建游戏的窗口 + self.screen = pygame.display.set_mode(SCREEN_RECT.size) + # 2. 创建游戏的时钟 + self.clock = pygame.time.Clock() + # 3. 调用私有方法,精灵和精灵组的创建 + self.__create_sprites() + + # 4. 设置定时器事件 - 创建敌机 1s + pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000) + pygame.time.set_timer(HERO_FIRE_EVENT, 500) + + def __create_sprites(self): + + # 创建背景精灵和精灵组 + bg1 = Background() + bg2 = Background(True) + + self.back_group = pygame.sprite.Group(bg1, bg2) + + # 创建敌机的精灵组 + self.enemy_group = pygame.sprite.Group() + + # 创建英雄的精灵和精灵组 + self.hero = Hero() + self.hero_group = pygame.sprite.Group(self.hero) + + + def start_game(self): + print("游戏开始...") + + while True: + # 1. 设置刷新帧率 + self.clock.tick(FRAME_PRE_SEC) + # 2. 事件监听 + self.__event_handler() + # 3. 碰撞检测 + self.__check_collide() + # 4. 更新/绘制精灵组 + self.__update_sprites() + # 5. 更新显示 + pygame.display.update() + + def __event_handler(self): + + for event in pygame.event.get(): + + # 判断是否退出游戏 + if event.type == pygame.QUIT: + PlaneGame.__game_over() + elif event.type == CREATE_ENEMY_EVENT: + # print("敌机出场...") + # 创建敌机精灵 + enemy = Enemy() + + # 将敌机精灵添加到敌机精灵组 + self.enemy_group.add(enemy) + elif event.type == HERO_FIRE_EVENT: + self.hero.fire() + # elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT: + # print("向右移动...") + + # 使用键盘提供的方法获取键盘按键 + keys_pressed = pygame.key.get_pressed() + # 判断元组中对应的按键索引值 1 + if keys_pressed[pygame.K_RIGHT]: + self.hero.speed = 2 + elif keys_pressed[pygame.K_LEFT]: + self.hero.speed = -2 + else: + self.hero.speed = 0 + + + def __check_collide(self): + + # 1. 子弹摧毁敌机 + pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True) + + # 2. 敌机撞毁英雄 + enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True) + + # 判断列表是否有内容 + if len(enemies) > 0: + + # 让英雄牺牲 + self.hero.kill() + + # 结束游戏 + PlaneGame.__game_over() + + def __update_sprites(self): + + self.back_group.update() + self.back_group.draw(self.screen) + + self.enemy_group.update() + self.enemy_group.draw(self.screen) + + self.hero_group.update() + self.hero_group.draw(self.screen) + + self.hero.bullets.update() + self.hero.bullets.draw(self.screen) + + @staticmethod + def __game_over(): + print("游戏结束") + + pygame.quit() + exit() + +if __name__ == '__main__': + + # 创建游戏对象 + game = PlaneGame() + + # 启动游戏 + game.start_game() \ No newline at end of file diff --git a/Aircraft Wars/plane_sprites.py b/Aircraft Wars/plane_sprites.py new file mode 100644 index 0000000..7f23392 --- /dev/null +++ b/Aircraft Wars/plane_sprites.py @@ -0,0 +1,146 @@ +import pygame +import random + +# 屏幕大小的常量 +SCREEN_RECT = pygame.Rect(0, 0, 480, 700) +# 刷新的帧率 +FRAME_PRE_SEC = 60 +# 创建敌机的定时器常量 +CREATE_ENEMY_EVENT = pygame.USEREVENT +# 英雄发射子弹事件 +HERO_FIRE_EVENT = pygame.USEREVENT + 1 + + +class GameSprite(pygame.sprite.Sprite): + """飞机大战游戏精灵""" + + def __init__(self, image_name, speed = 1): + + # 调用父类的初始化方法 + super().__init__() + + #定义对象的属性 + self.image = pygame.image.load(image_name) + self.rect = self.image.get_rect() + self.speed = speed + + def update(self): + + # 在屏幕的垂直方向上移动 + self.rect.y += self.speed + + +class Background(GameSprite): + """游戏背景精灵""" + + def __init__(self, is_alt = False): + + # 1. 调用父类的方法实现 + super().__init__("./images/background.png") + + # 2. 判断是否是交替图像,如果是,需要设置初始位置 + if is_alt: + self.rect.y = -self.rect.height + + def update(self): + + # 1. 调用父类的方法实现 + super().update() + + # 2. 判断是否移出屏幕,如果移出屏幕,将图像设置到屏幕的上方 + if self.rect.y >= SCREEN_RECT.height: + self.rect.y = -self.rect.height + + +class Enemy(GameSprite): + """敌机精灵""" + + def __init__(self): + + # 1. 调用父类方法,创建敌机精灵,同时指定敌机图片 + super().__init__("./images/enemy1.png") + + # 2. 指定敌机的初试随机速度 1 ~ 3 + self.speed = random.randint(1, 3) + + # 3. 指定敌机的初试随机位置 + self.rect.bottom = 0 + + max_x = SCREEN_RECT.w - self.rect.width + self.rect.x = random.randint(0, max_x) + + def update(self): + + # 1. 调用父类方法,保持垂直方向飞行 + super().update() + + # 2. 判断是否飞出屏幕,如果是,需要从精灵组删除敌机 + if self.rect.y >= SCREEN_RECT.height: + # print("飞出屏幕,需要从精灵组删除...") + # kill方法可以将精灵从所有精灵组中移出,精灵就会被销毁 + self.kill() + + def __del__(self): + # print("敌机挂了 %s" % self.rect) + pass + + +class Hero(GameSprite): + """英雄精灵""" + + def __init__(self): + + # 1. 调用父类方法,设置images&speed + super().__init__("./images/me1.png", 0) + + # 2. 设置英雄的初始位置 + self.rect.centerx = SCREEN_RECT.centerx + self.rect.bottom = SCREEN_RECT.bottom - 120 + + # 3. 创建子弹的精灵组 + self.bullets = pygame.sprite.Group() + + def update(self): + + # 英雄在水平方向移动 + self.rect.x += self.speed + + # 控制英雄不能离开屏幕 + if self.rect.x < 0: + self.rect.x = 0 + elif self.rect.right > SCREEN_RECT.right: + self.rect.right = SCREEN_RECT.right + + def fire(self): + print("发射子弹...") + + for i in (0, 1, 2): + # 1. 创建子弹的精灵 + bullet = Bullet() + + # 2. 设置精灵的位置 + bullet.rect.bottom = self.rect.y - i * 20 + bullet.rect.centerx = self.rect.centerx + + # 3. 将精灵添加到精灵组 + self.bullets.add(bullet) + +class Bullet(GameSprite): + """子弹精灵""" + + def __init__(self): + + # 调用父类方法,设置子弹图片,设置初始速度 + super().__init__("./images/bullet1.png", -2) + + def update(self): + + # 调用父类方法,让子弹沿垂直方向飞行 + super().update() + + # 判断子弹是否飞出屏幕 + if self.rect.bottom < 0: + self.kill() + + def __del__(self): + print("子弹被销毁...")