110
社区成员




课程:《Python程序设计》
班级: 2324
姓名: 董琰祥
学号:20232411
实验教师:王志强
实验日期:2022年4月17日
必修/选修: 公选课
Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。
课代表和各小组负责人收集作业(源代码、视频、综合实践报告)
例如:编写从社交网络爬取数据,实现可视化舆情监控或者情感分析。
例如:利用公开数据集,开展图像分类、恶意软件检测等
例如:利用Python库,基于OCR技术实现自动化提取图片中数据,并填入excel中。
例如:爬取天气数据,实现自动化微信提醒
例如:利用爬虫,实现自动化下载网站视频、文件等。
例如:编写小游戏:坦克大战、贪吃蛇、扫雷等等
注:在Windows/Linux系统上使用VIM、PDB、IDLE、Pycharm等工具编程实现
完整代码:
运行视频:
飞机大战这个小游戏的核心就在于大量飞机、子弹间的碰撞检测,而pygame在此方面卓有建树,于是我下载并导入了它。随机函数库的导入则是为了方便后续敌人的生成,sys函数库导入则是为了退出游戏。导入pygame后就利用它完成窗口的创建,以及一些常量的初始化。
# -*- coding = utf-8 -*-
import pygame
from pygame.locals import *
from sys import exit
from random import randint
SCREEN_WIDTH = 480 #窗口大小
SCREEN_HEIGHT = 640
......
score = 0 #分数初始化
......
FRAME_RATE = 60 #最高帧率
ANIMATE_CYCLE = 30 #动画频率
ticks = 0 #时频计数
ticks_enemy = 0
clock = pygame.time.Clock() #帧率对象生成
offset = {pygame.K_LEFT:0, pygame.K_RIGHT:0, pygame.K_UP:0, pygame.K_DOWN:0} #按键初始化
fps_enemy1 = 30 #三种敌人的生成频率
fps_enemy2 = 300
fps_enemy3 = 1799
......
pygame.init() #窗口生成
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption('planes\'war')
借助网上收集的图片资源进行玩家,子弹,敌人的类的编写。(图片资源在导入图像部分再详说)
class Bullet1(pygame.sprite.Sprite): #玩家子弹类
def __init__(self, bullet1_surface, bullet1_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = bullet1_surface
self.rect = self.image.get_rect()
self.rect.topleft = bullet1_init_pos
self.speed = 8
def update(self):
self.rect.top -= self.speed
if self.rect.bottom < 0:
self.kill()
class Bullet2(pygame.sprite.Sprite): #敌人子弹类
def __init__(self, bullet2_surface, bullet2_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = bullet2_surface
self.rect = self.image.get_rect()
self.rect.topleft = bullet2_init_pos
self.speed = 8
def update(self):
self.rect.top += self.speed
if self.rect.top > SCREEN_HEIGHT:
self.kill()
class Hero(pygame.sprite.Sprite): #玩家类
def __init__(self, hero_surface, hero_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = hero_surface
self.rect = self.image.get_rect()
self.rect.topleft = hero_init_pos
self.speed = 6
self.is_hit = 0
self.bullets_1 = pygame.sprite.Group()
def single_shoot(self, bullet1_surface): #发射子弹
bullet_1 = Bullet1(bullet1_surface, self.rect.midtop)
self.bullets_1.add(bullet_1)
def move(self, offset): #移动
x = self.rect.left + offset[pygame.K_RIGHT] - offset[pygame.K_LEFT]
y = self.rect.top + offset[pygame.K_DOWN] - offset[pygame.K_UP]
if x < 0:
self.rect.left = 0
elif x > SCREEN_WIDTH - self.rect.width:
self.rect.left = SCREEN_WIDTH - self.rect.width
else:
self.rect.left = x
if y < 0:
self.rect.top = 0
elif y > SCREEN_HEIGHT - self.rect.height:
self.rect.top = SCREEN_HEIGHT - self.rect.height
else:
self.rect.top = y
class Enemy1(pygame.sprite.Sprite): #第一类敌人
def __init__(self, enemy1_surface, enemy1_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = enemy1_surface
self.rect = self.image.get_rect()
self.rect.topleft = enemy1_init_pos
self.speed = 2
self.down_index = 0
def update(self):
self.rect.top += self.speed
if self.rect.top > SCREEN_HEIGHT:
self.kill()
class Enemy2(pygame.sprite.Sprite): #第二类敌人
def __init__(self, enemy2_surface, enemy2_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = enemy2_surface
self.rect = self.image.get_rect()
self.rect.topleft = enemy2_init_pos
self.rect.midbottom = [self.rect.left+self.rect.width/2,self.rect.top+self.rect.height]
self.speed = 2
self.is_hit = 0
self.down_index = 0
self.bullets = pygame.sprite.Group()
def update(self):
self.rect.top += self.speed
if self.rect.top > SCREEN_HEIGHT:
self.kill()
def single_shoot(self, bullet2_surface):
bullet = Bullet2(bullet2_surface, self.rect.midbottom)
self.bullets.add(bullet)
class Enemy3(pygame.sprite.Sprite): #第三类敌人
def __init__(self, enemy3_surface, enemy3_init_pos):
pygame.sprite.Sprite.__init__(self)
self.image = enemy3_surface
self.rect = self.image.get_rect()
self.rect.topleft = enemy3_init_pos
self.is_hit = 0
self.down_index = 0
......
hero_down_index = 1 #玩家坠落动画标识初始化
图像数据都是从网上找的,源自源自Kill-Console的技术博客Pygame制作微信打飞机游戏PC版。图片资源以附件形式上传。
background = pygame.image.load('resources/image/background.png')
gameover = pygame.image.load('resources/image/gameover.png')
shoot_img = pygame.image.load('resources/image/shoot.png')
hero_surface = []
hero_surface.append(shoot_img.subsurface(pygame.Rect(0, 99, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 360, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 234, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 624, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 498, 102, 126)))
hero_surface.append(shoot_img.subsurface(pygame.Rect(432, 624, 102, 126)))
hero_pos = [200, 500]
bullet1_surface = shoot_img.subsurface(pygame.Rect(1004, 987, 9, 21))
bullet2_surface = shoot_img.subsurface(pygame.Rect(69, 78, 9, 21))
enemy1_surface = shoot_img.subsurface(pygame.Rect(534, 612, 57, 43))
enemy1_down_surface = []
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 347, 57, 43)))
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(873, 697, 57, 43)))
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 296, 57, 43)))
enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(930, 697, 57, 43)))
enemy2_surface = shoot_img.subsurface(pygame.Rect(0, 0, 69, 99))
enemy2_down_surface = []
enemy2_down_surface.append(shoot_img.subsurface(pygame.Rect(534, 655, 69, 95)))
enemy2_down_surface.append(shoot_img.subsurface(pygame.Rect(603, 655, 69, 95)))
enemy2_down_surface.append(shoot_img.subsurface(pygame.Rect(672, 653, 69, 95)))
enemy2_down_surface.append(shoot_img.subsurface(pygame.Rect(741, 653, 69, 95)))
enemy3_surface = shoot_img.subsurface(pygame.Rect(335, 750, 169, 258))
enemy3_down_surface = []
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(0, 486, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(0, 225, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(839, 748, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(165, 486, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(673, 748, 165, 261)))
enemy3_down_surface.append(shoot_img.subsurface(pygame.Rect(0, 747, 165, 261)))
enemy3_pos = [150, 0]
hero = Hero(hero_surface[0], hero_pos)
enemy1_group = pygame.sprite.Group()
enemy1_down_group = pygame.sprite.Group()
enemy2_group = pygame.sprite.Group()
enemy2_down_group = pygame.sprite.Group()
enemy3_group = pygame.sprite.Group()
enemy3_down_group = pygame.sprite.Group()
这是游戏的精髓,pygame的价值也体现在这里:通过精灵组实现同类对象的统一管理
我会在这具体介绍我的设计:对于玩家,每三分之一秒发射一枚子弹,共五滴血;
第一类敌人作为小兵,不发射子弹,一滴血,0.5秒生成一个,50分
第二类敌人作为中坚,发射子弹(子弹1.5秒发射一个,其他参数与玩家子弹相同),三滴血,5秒生成一个,500分
第三类敌人作为boss,不发射子弹但会改变其他敌人生成速率(第一类敌人0.05秒生成一个,第二类敌人2秒生成一个),十滴血,30秒生成一个,5000分
此外,只有用子弹击败敌人才得分
玩家能直接撞毁前两类敌人,但会被第三类敌人撞毁
while True:
clock.tick(FRAME_RATE)
screen.blit(background, (0, 0))
if ticks >= ANIMATE_CYCLE:
ticks = 0
if hero.is_hit >= 5: #玩家坠落判定
hero.kill()
if ticks%(ANIMATE_CYCLE//2) == 0:
hero_down_index += 1
hero.image = hero_surface[hero_down_index]
if hero_down_index == 5:
break
else:
hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]
if ticks % 20 == 0:
hero.single_shoot(bullet1_surface)
hero.bullets_1.update()
hero.bullets_1.draw(screen)
if ticks_enemy % fps_enemy1 == 0: #敌人生成
enemy1 = Enemy1(enemy1_surface, [randint(0, SCREEN_WIDTH - enemy1_surface.get_width()), -enemy1_surface.get_height()])
enemy1_group.add(enemy1)
enemy1_group.update()
enemy1_group.draw(screen)
if ticks_enemy % fps_enemy2 == 0:
enemy2 = Enemy2(enemy2_surface, [randint(0, SCREEN_WIDTH - enemy1_surface.get_width()), -enemy1_surface.get_height()])
enemy2_group.add(enemy2)
enemy2_group.update()
enemy2_group.draw(screen)
for enemy2 in enemy2_group:
if ticks_enemy % 90 == 0:
enemy2.single_shoot(bullet2_surface)
enemy2.bullets.update()
enemy2.bullets.draw(screen)
if ticks_enemy // fps_enemy3 == 1:
enemy3 = Enemy3(enemy3_surface, enemy3_pos)
enemy3_group.add(enemy3)
fps_enemy1 = 15
fps_enemy2 = 120
ticks_enemy = 0
enemy3_group.update()
enemy3_group.draw(screen)
if enemy1_down_group.add(pygame.sprite.groupcollide(enemy1_group, hero.bullets_1, True, True)): #玩家子弹与敌人的碰撞检测
score += 50
pygame.sprite.groupcollide(enemy2.bullets, hero.bullets_1, True, True) #玩家子弹与敌人子弹相遇抵消
for enemy2 in enemy2_group:
if enemy2.is_hit < 2:
if pygame.sprite.groupcollide(enemy2_group, hero.bullets_1, False, True):
enemy2.is_hit += 1
else:
enemy2_down_group.add(pygame.sprite.groupcollide(enemy2_group, hero.bullets_1, True, True))
score += 500
for enemy3 in enemy3_group:
if enemy3.is_hit < 9:
if pygame.sprite.groupcollide(enemy3_group, hero.bullets_1, False, True):
enemy3.is_hit += 1
else:
enemy3_down_group.add(pygame.sprite.groupcollide(enemy3_group, hero.bullets_1, True, True))
fps_enemy1 = 30
fps_enemy3 = 300
score += 5000
for enemy1_down in enemy1_down_group: #敌人坠落动画判定
screen.blit(enemy1_down_surface[enemy1_down.down_index], enemy1_down.rect)
if ticks % (ANIMATE_CYCLE//2) == 0:
if enemy1_down.down_index < 3:
enemy1_down.down_index += 1
else:
enemy1_down_group.remove(enemy1_down)
for enemy2_down in enemy2_down_group:
screen.blit(enemy2_down_surface[enemy2_down.down_index], enemy2_down.rect)
if ticks % (ANIMATE_CYCLE//2) == 0:
if enemy2_down.down_index < 3:
enemy2_down.down_index += 1
else:
enemy2_down_group.remove(enemy2_down)
for enemy3_down in enemy3_down_group:
screen.blit(enemy3_down_surface[enemy3_down.down_index], enemy3_down.rect)
if ticks % (ANIMATE_CYCLE//2) == 0:
if enemy3_down.down_index < 5:
enemy3_down.down_index += 1
else:
enemy3_down_group.remove(enemy3_down)
enemy1_down_list = pygame.sprite.spritecollide(hero, enemy1_group, True) #敌人,玩家碰撞检测
if len(enemy1_down_list) > 0:
enemy1_down_group.add(enemy1_down_list)
hero.is_hit += 1
enemy2_down_list = pygame.sprite.spritecollide(hero, enemy2_group, True)
if len(enemy2_down_list) > 0:
enemy2_down_group.add(enemy2_down_list)
hero.is_hit += 1
if pygame.sprite.spritecollide(hero, enemy3_group, False):
hero.is_hit += 5
if pygame.sprite.spritecollide(hero, enemy2.bullets, True): #玩家被击中的判定
hero.is_hit += 1
主要是对退出游戏的判定和对玩家移动的补充
for event in pygame.event.get(): #退出游戏
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN: #移动判定
if event.key in offset:
offset[event.key] = hero.speed
elif event.type == pygame.KEYUP:
if event.key in offset:
offset[event.key] = 0
hero.move(offset)
分数呈现和结束画面
font = pygame.font.SysFont("", 48) #分数呈现
text = font.render("Score: " + str(score), True, (255, 0, 0))
text_rect = text.get_rect()
text_rect.centerx = screen.get_rect().centerx
text_rect.centery = screen.get_rect().centery - 50
screen.blit(gameover, (0, 0)) #结束画面
screen.blit(text, text_rect)
while True: #退出游戏
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
问题一:游戏结束后无法正常退出游戏:点叉号无反应
解决方案:原退出判定只在游戏中起效,为了解决问题,就在游戏结束后再次进行退出判定
问题二:设置对象移动方向时总是与设想的不一致
解决方案:通过对pygame的深入学习,我发现pygame的坐标轴与我们日常常见的坐标轴不同,y坐标是相反的,因此将涉及y坐标的代码都重新审查以解决问题
本次实验对我来说颇具挑战性,完成这个游戏的过程也是十分曲折。但真正完成了这个实验后,我对python又有了更深入的了解,也进一步体会到了python的魅力。完成实验的过程虽然曲折,但这是必不可少的,只有真正在实践中,我才能真正成长。事实上,这个游戏还很简陋,有诸多不足,甚至可能还有我没发觉的bug。但这是我亲手写的,是我的成果,我对此感到骄傲。同时,我也会继续努力,完善我的游戏,让它真正出彩。
很荣幸也很幸运我选了王志强老师的“python程序与设计”,在此之前我接触地更多的是C语言,python只有偶尔我上网查询ctf资料时,会在插件中遇到。对当时的我来说python显得很神秘,也很强大,我也因此产生了兴趣。同时,由于我本身对ctf的学习要求,需要接触学习python,于是我开始了一定程度的自学。当时,我学习的时候不得要领,东一榔头西一棒槌,最后连什么是面向对象的语言都没搞清。也因此,这学期我选修了这门课程,系统学习了python后,我才彻底地感受到了它的简洁但又功能多样,令我难以自拔。所以说,我很高兴能选修这门课,领略python的魅力。“人生苦短,我用python。”