20202321邬昱初《Python程序设计》实验四报告

课程:《Python程序设计》
班级:2023
姓名:邬昱初
学号:20202321
实验教师:王志强
实验日期:2021年6月28日至6月30日
必修/选修:公选课

一、实验内容

Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等

(一)、爬虫(对游戏 风暴英雄 的英雄信息进行爬取)
(二)、pygame坦克大战(本程序为在bilibili上跟随教程学习所得,代码是跟随教程视频手打出的,但有些地方由于知识有限于是将代码手动缩减了一些,所运行出的程序也相对原视频的程序更加简单)原视频:https://www.bilibili.com/video/BV1Vp4y1r7Gm?from=search&seid=14379185760654633406

二、实验过程
(一)、爬虫
(1).爬取网站 https://heroes.blizzard.cn/heroes/ 爬取内容 英雄信息


(2).代码的编写
1.地址与模拟浏览器
url = 'https://heroes.blizzard.cn/heroes'
headers =
{
'ser-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36'
}
2,寻找爬取的xpath地址
html=requests.get(url,headers=headers)
selector=etree.HTML(html.text)
hero_list=selector.xpath('//*[@id="container"]/div[2]/div[2]/div')
3.爬取内容选取
for hero in hero_list:
name=hero.xpath('a/h4/text()')[0]
chenghao=hero.xpath('a/div[2]/i/text()')[0]
item=[chenghao,name]
4.输出爬取内容
def writecsv(item):
with open('hero.csv','a',encoding='utf-8') as f:
writer=csv.writer(f)
try:
writer.writerow(item)
except:
print('write error!')
writecsv(item)
print('提取:',name)
(3).爬取结果

(二)、pygame坦克大战
(1).代码的编写
1.对游戏初始内容进行设置
COLOR_BLACK = pygame.Color(0,0,0)# 游戏窗口背景颜色
COLOR_RED = pygame.Color(255,0,0)# 游戏字体颜色
class MainGame():
# 游戏主窗口
window = None
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 500
# 我方坦克
TANK_P1 = None
# TANK_P1.stop = True
# TANK_P1.live = True
# 敌方坦克
EnemyTank_list = []
EnemyTank_count = 10

Bullet_list = []
Enemy_bullet_list = []
Explode_list = []
Wall_list = []

2.对游戏窗口进行设置
def startGame(self):
pygame.display.init()
MainGame.window = pygame.display.set_mode([MainGame.SCREEN_WIDTH, MainGame.SCREEN_HEIGHT])
self.createMyTank()
self.createEnemyTank()
self.createWalls()
pygame.display.set_caption('20202321坦克大战')
3.对游戏规则进行设置
while True:
MainGame.window.fill(COLOR_BLACK)
self.getEvent()
MainGame.window.blit(self.getTextSurface('剩余敌方坦克%d辆'%len(MainGame.EnemyTank_list)),(5,5))
self.blitWalls()
if MainGame.TANK_P1 and MainGame.TANK_P1.live:
MainGame.TANK_P1.displayTank()
else:
del MainGame.TANK_P1
MainGame.TANK_P1 = None
self.blitEnemyTank()
if MainGame.TANK_P1:
if MainGame.TANK_P1.stop == True:
MainGame.TANK_P1.move()
MainGame.TANK_P1.hitWalls()
MainGame.TANK_P1.hitEnemyTank()
self.blitBullet()
self.blitEnemyBullet()
self.displayExplodes()
time.sleep(0.02)
pygame.display.update()
4.界面内创造坦克

我方坦克

def createMyTank(self):
    MainGame.TANK_P1 = MyTank(400, 300)

敌方坦克

def createEnemyTank(self):
    top = 100
    for i in range(MainGame.EnemyTank_count):
        speed = random.randint(3, 6)
        left = random.randint(1, 7)
        eTank = EnemyTank(left * 100, top, speed)
        MainGame.EnemyTank_list.append(eTank)
def blitEnemyTank(self):
    for eTank in MainGame.EnemyTank_list:
        if eTank.live:
            eTank.displayTank()
            eTank.randMove()
            eTank.hitWalls()
            eTank.hitMyTank()
            eBullet = eTank.shot()
            if eBullet:
                MainGame.Enemy_bullet_list.append(eBullet)
        else:
            MainGame.EnemyTank_list.remove(eTank)

5.界面内创造墙壁
def createWalls(self):
for i in range(1, 7):
wall = Wall(120 * i, 240)
MainGame.Wall_list.append(wall)
def blitWalls(self):
for wall in MainGame.Wall_list:
if wall.live:
wall.displayWall()
else:
MainGame.Wall_list.remove(wall)
6.界面内创造子弹
def blitBullet(self):
for bullet in MainGame.Bullet_list:
if bullet.live == True:
bullet.displayBullet()
bullet.bulletMove()
bullet.hitEnemyTank()
bullet.hitWalls()
else:
MainGame.Bullet_list.remove(bullet)
def blitEnemyBullet(self):
for eBullet in MainGame.Enemy_bullet_list:
if eBullet.live:
eBullet.displayBullet()
eBullet.bulletMove()
eBullet.hitWalls()
if MainGame.TANK_P1 and MainGame.TANK_P1.live:
eBullet.hitMyTank()
else:
MainGame.Enemy_bullet_list.remove(eBullet)
7.玩家的操纵功能
def getEvent(self):
eventList = pygame.event.get()
for event in eventList:
if event.type == pygame.QUIT:
self.endGame()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE and not MainGame.TANK_P1:
self.createMyTank()
if MainGame.TANK_P1 and MainGame.TANK_P1.live:
if event.key == pygame.K_LEFT:
MainGame.TANK_P1.direction = 'L'
MainGame.TANK_P1.move()
MainGame.TANK_P1.stop = True
elif event.key == pygame.K_RIGHT:
MainGame.TANK_P1.direction = 'R'
MainGame.TANK_P1.move()
MainGame.TANK_P1.stop = True
elif event.key == pygame.K_UP:
MainGame.TANK_P1.direction = 'U'
MainGame.TANK_P1.move()
MainGame.TANK_P1.stop = True
elif event.key == pygame.K_DOWN:
MainGame.TANK_P1.direction = 'D'
MainGame.TANK_P1.move()
MainGame.TANK_P1.stop = True
elif event.key == pygame.K_SPACE:
print('坦克发射子弹')
if len(MainGame.Bullet_list) < 3:
m = Bullet(MainGame.TANK_P1)
MainGame.Bullet_list.append(m)
else:
print("子弹数量不足,无法发射")

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                if MainGame.TANK_P1 and MainGame.TANK_P1.live:
                    MainGame.TANK_P1.stop = False

8.特效
def displayExplodes(self):
for explode in MainGame.Explode_list:
if explode.live:
explode.displayExplode()
else:
MainGame.Explode_list.remove(explode)
9.文字与退出
def getTextSurface(self, text):
pygame.font.init()
font = pygame.font.SysFont('kaiti',18)
textSurface = font.render(text, True, COLOR_RED)
return textSurface
def endGame(self):
print("谢谢使用")
exit()
上述功能实现后,还需对坦克,墙壁,子弹在界面出现的具体情况进行设置完善,代码如下
10.坦克
class BaseItem(pygame.sprite.Sprite):
def init(self):
pygame.sprite.Sprite.init(self)
class Tank(BaseItem):
def init(self, left, top):
self.images = {
"U": pygame.image.load('img/p1tankU.gif'),
"D": pygame.image.load('img/p1tankD.gif'),
"L": pygame.image.load('img/p1tankL.gif'),
"R": pygame.image.load('img/p1tankR.gif')
}
self.direction = "U"
self.image = self.images[self.direction]

坦克所在区域

    self.rect = self.image.get_rect()
    self.rect.left = left
    self.rect.top = top
    self.speed = 5
    self.step = 20
    self.stop = False
    self.live = True
    self.oldLeft = self.rect.left
    self.oldTop = self.rect.top

坦克移动

def move(self):
    # 先记录移动之前的坐标
    self.oldLeft = self.rect.left
    self.oldTop = self.rect.top
    if self.direction == "L":
        if self.rect.left > 0:
            self.rect.left -= self.speed
    elif self.direction == "R":
        if self.rect.left + self.rect.width < MainGame.SCREEN_WIDTH:
            self.rect.left += self.speed
    elif self.direction == "U":
        if self.rect.top > 0:
            self.rect.top -= self.speed
    else:
        if self.rect.top + self.rect.height < MainGame.SCREEN_HEIGHT:
            self.rect.top += self.speed

坦克还原

def stay(self):
    self.rect.left = self.oldLeft
    self.rect.top = self.oldTop

坦克碰撞到墙壁

def hitWalls(self):
    for wall in MainGame.Wall_list:
        if pygame.sprite.collide_rect(wall, self):
            self.stay()

坦克射击

def shot(self):
    return Bullet(self)

坦克展示

def displayTank(self):
    self.image = self.images[self.direction]
    MainGame.window.blit(self.image,self.rect)

我方坦克

class MyTank(Tank):
def init(self, left, top):
super(MyTank, self).init(left, top)
def hitEnemyTank(self):
for eTank in MainGame.EnemyTank_list:
if pygame.sprite.collide_rect(eTank, self):
self.stay()

敌方坦克

class EnemyTank(Tank):
def init(self, left, top, speed):
super(EnemyTank, self).init(left,top)
self.images = {
"U": pygame.image.load('img/enemy1U.gif'),
"D": pygame.image.load('img/enemy1D.gif'),
"L": pygame.image.load('img/enemy1L.gif'),
"R": pygame.image.load('img/enemy1R.gif')
}
self.direction = self.randDirection()
self.image = self.images[self.direction]
self.rect = self.image.get_rect()
self.rect.left = left
self.rect.top = top
self.speed = speed
self.stop = False
self.step = 10
self.live = True
# rect
def randDirection(self):
num = random.randint(1, 4)
if num == 1:
return 'U'
elif num == 2:
return 'D'
elif num == 3:
return 'L'
elif num == 4:
return 'R'
def displayEnemtTank(self):
super().displayTank()

敌方坦克随机移动

def randMove(self):
    if self.step <= 0:
        self.direction = self.randDirection()
        self.step = 20
    else:
        self.move()
        self.step -= 1
def shot(self):
    num = random.randint(1, 1000)
    if num <= 20:
        return Bullet(self)
def hitMyTank(self):
    if MainGame.TANK_P1 and MainGame.TANK_P1.live:
        if pygame.sprite.collide_rect(self, MainGame.TANK_P1):
            self.stay()

11.子弹
class Bullet(BaseItem):
def init(self, tank):
self.speed = 5
self.live = True
self.image = pygame.image.load('img/enemymissile.gif')
self.direction = tank.direction

子弹位置

    self.rect = self.image.get_rect()
    if self.direction == 'U':
        self.rect.left = tank.rect.left + tank.rect.width/2 - self.rect.width/2
        self.rect.top = tank.rect.top - self.rect.height
    elif self.direction == 'D':
        self.rect.left = tank.rect.left + tank.rect.width / 2 - self.rect.width / 2
        self.rect.top = tank.rect.top + tank.rect.height
    elif self.direction == 'L':
        self.rect.left = tank.rect.left - self.rect.width / 2 - self.rect.width / 2
        self.rect.top = tank.rect.top + tank.rect.width / 2 - self.rect.width / 2
    elif self.direction == 'R':
        self.rect.left = tank.rect.left + tank.rect.width
        self.rect.top = tank.rect.top + tank.rect.width/2 - self.rect.width / 2
def bulletMove(self):
    if self.direction == 'U':
        if self.rect.top > 0:
            self.rect.top -= self.speed
        else:
            self.live = False
    elif self.direction == 'D':
        if self.rect.top < MainGame.SCREEN_HEIGHT - self.rect.height:
            self.rect.top += self.speed
        else:
            self.live = False
    elif self.direction == 'L':
        if self.rect.left > 0:
            self.rect.left -= self.speed
        else:
            self.live = False
    elif self.direction == 'R':
        if self.rect.left < MainGame.SCREEN_WIDTH - self.rect.width:
            self.rect.left += self.speed
        else:
            self.live = False
def displayBullet(self):
    MainGame.window.blit(self.image, self.rect)

我方子弹碰撞敌方坦克

def hitEnemyTank(self):
    for eTank in MainGame.EnemyTank_list:
        if pygame.sprite.collide_rect(eTank, self):
            # 产生一个爆炸效果
            explode = Explode(eTank)
            # 将爆炸效果加入到爆炸效果列表
            MainGame.Explode_list.append(explode)
            self.live = False
            eTank.live = False

敌方子弹与我方坦克的碰撞

def hitMyTank(self):
    if pygame.sprite.collide_rect(self, MainGame.TANK_P1):
        explode = Explode(MainGame.TANK_P1)
        MainGame.Explode_list.append(explode)
        self.live = False
        MainGame.TANK_P1.live = False

子弹与墙壁的碰撞

def hitWalls(self):
    for wall in MainGame.Wall_list:
        if pygame.sprite.collide_rect(wall, self):
            self.live = False
            wall.hp -= 1
            if wall.hp <= 0:
                wall.live = False

12.墙壁
class Wall():
def init(self, left, top):
self.image = pygame.image.load('img/steels.gif')
self.rect = self.image.get_rect()
self.rect.left = left
self.rect.top = top
self.live = True

墙壁的生命值

    self.hp = 3
def displayWall(self):
    MainGame.window.blit(self.image, self.rect)

13.爆炸效果
class Explode():
def init(self, tank):
self.rect = tank.rect
self.step = 0
self.images = [
pygame.image.load('img/blast0.gif'),
pygame.image.load('img/blast1.gif'),
pygame.image.load('img/blast2.gif'),
pygame.image.load('img/blast3.gif'),
pygame.image.load('img/blast4.gif')
]
self.image = self.images[self.step]
self.live = True

def displayExplode(self):
    if self.step < len(self.images):
        MainGame.window.blit(self.image, self.rect)
        self.image = self.images[self.step]
        self.step += 1
    else:
        self.live = False
        self.step = 0

(2).实验的结果


三、问题与源代码
爬虫选择的多个网站难以爬取,对图片的爬取不够熟练,爬取内容显示过少等;
坦克大战中坦克图片的寻找,坦克碰撞体积的设置没有成功,子弹的发射与移动速度难以调整,墙壁的随机生成等。
爬虫源代码:https://gitee.com/wu-yuchu/pythonProject/blob/master/爬虫.py
坦克大战代码:https://gitee.com/wu-yuchu/pythonProject/blob/master/坦克大战.py
四、总结
实验:
这次实验的要求因为可选择性强,所以难度也呈现可调控性,而我选择了相对简单的爬虫(上课学过)和pygame(视频网站有学习教程)进行实验,过程中,我依然发现了自己的不足,对爬虫的掌握较差,于是请教了同学,而在跟打代码的时候依然有些地方出了错,于是只能缩减程序,但也通过这次实验,我对这两方面的知识理解与印象也更加深刻,也明白了学无止境、向他人学习也是很好的方式的道理。
课程:
本学期我对python从入门到开始了解,中间有不少人的帮忙,包括老师、同学、学长,在这期间学习了许多关于python的基础知识,也了解了许多深入一些的内容,如爬虫、python对游戏功能的实现等内容。作为大一新生,因为这学期同时学习了c语言与python两门语言,难免会有比较,相较于c的结构严谨、代码繁杂,python的灵活简单更加使我影响深刻,好上手、应用面广、高效而便捷,在实际的体验中我开始真正地明白“人生苦短,我用python”所言非虚。
在今后的编程学习中,我明白了语言同时学习往往能相互理解,甚至共有的知识点只需稍加改变就能相互使用,也能更好明白面向对象和和面向过程的差别。
最后,感谢老师在这一学期的指导,下一学期的数据结构课程,也请您多多指点。

posted on 2021-06-30 16:41  MaruForever  阅读(98)  评论(0编辑  收藏  举报

导航