用户:VivioPfh查看:0 回复:0 评论:0 创建时间:2020-11-14T14:25:56
【作品展示】

【作品介绍】
击飞virus!!!
【作品源代码】
'''
创意作品说明 : 请实现按下空格键,飞机发射喵的效果(一排三颗)
小提示:看 ?处注释提示
本次作品不用录制视频,不用提交,感兴趣的同学尝试完成即可~
'''
from loadImg import *
from initData import *
import sys
import pygame
pygame.init()
# 设置窗口大小
screen = pygame.display.set_mode([480, 670])
# 设置窗口标题
pygame.display.set_caption('消灭病毒')
# 设置游戏初始积分为0
gameScore = 0
# 设置游戏初始状态为'运行'
gameState = '运行'
# 病毒列表
virusList = []
# 子弹列表
bulletList = []
# 自定义【添加病毒事件】
ADDVIRUS = 24
# 开启【添加病毒事件】的定时器
pygame.time.set_timer(ADDVIRUS, 1000)
# 创建函数,绘制界面
def draw():
# 绘制背景
screen.blit(bgImg, [0, 0])
# 绘制病毒
for virus in virusList:
screen.blit(virus['img'], [virus['x'], virus['y']])
# 绘制飞机
screen.blit(plane['img'], [plane['x'], plane['y']])
# 绘制子弹
for bullet in bulletList:
screen.blit(bullet['img'], [bullet['x'], bullet['y']])
# 创建函数,控制移动
def move():
# 病毒移动
for virus in virusList:
virus['y'] += virus['step']
# 子弹移动 -----------
# 遍历子弹列表
for bullet in bulletList:
# 所有子弹的y坐标都要减小
bullet['y'] -= bullet['step']
# 如果是左边的子弹,就x坐标减小(因为左边子弹是往左上方走)
if bullet['place'] == '左':
bullet['x'] -= 1
# 如果是右边的子弹,就x坐标增大(因为右边子弹是往右上方走)
elif bullet['place'] == '右':
bullet['x'] += 1
# 创建函数,检测碰撞
def checkHit(v, p):
# 碰撞范围上下左右边缘
top = p['y'] - v['height']
bottom = p['y'] + p['height']
left = p['x'] - v['width']
right = p['x'] + p['width']
# 如果发生碰撞,就返回'撞到'
if v['x'] >= left and v['x'] <= right:
if v['y'] >= top and v['y'] <= bottom:
return '撞到'
# 创建函数,检测所有物体,如果有碰撞,就做出响应
def check():
# 声明全局变量
global gameScore, gameState
# 如果是运行状态,才判断相撞
if gameState == '运行':
# 检测【病毒与飞机】相撞
# 遍历病毒列表,挨个获取到每一个病毒
for virus in virusList:
# 如果该病毒与飞机发生碰撞
if checkHit(virus, plane) == '撞到':
# 关闭【添加病毒事件】的定时器
pygame.time.set_timer(ADDVIRUS, 0)
# 打印GameOver和当前积分
print('GameOver')
if print('GameOver'):
quit()
print('总得分' + str(gameScore))
# 改变游戏状态为'结束'
gameState = '结束'
# 检测【病毒与子弹】相撞
# 遍历病毒列表,挨个获取到每一个病毒
for virus in virusList:
# 遍历子弹列表,挨个获取到每一个子弹
for bullet in bulletList:
# 如果该病毒与该子弹发生碰撞
if checkHit(virus, bullet) == '撞到':
# 从列表中删除该病毒和子弹
virusList.remove(virus)
bulletList.remove(bullet)
# 积分+10
gameScore += 10
# 创建函数,删除已移出窗口的病毒,子弹
def delete():
# 声明全局变量
global virusList, bulletList
# 删除移出窗口的病毒
# 创建新列表
newVirusList = []
# 遍历病毒列表,挨个获取到每一个病毒
for virus in virusList:
# 判断如果这个病毒在窗口内,并且状态为True,就添加到新列表中
if virus['y'] < 670:
newVirusList.append(virus)
# 将新列表赋值回给病毒列表
virusList = newVirusList
# 删除移出窗口的子弹
# 创建新列表
newBulletList = []
# 遍历子弹列表,挨个获取到每一个子弹
for bullet in bulletList:
# 判断如果这个子弹在窗口内,并且状态为True,就添加到新列表中
if bullet['y'] > - bullet['height']:
newBulletList.append(bullet)
# 将新列表赋值回给子弹列表
bulletList = newBulletList
# 创建函数,事件监听
def eventListen():
for event in pygame.event.get():
# 如果监听到【退出事件】,就退出程序
if event.type == pygame.QUIT:
sys.exit()
# 如果监听到【添加病毒事件】,就往列表中添加新病毒
elif event.type == ADDVIRUS:
virusList.append(getVirus())
# 如果监听到【按下键盘事件】
elif event.type == pygame.KEYDOWN:
# 如果是【左移键】并且 飞机不超过左边界,就让飞机左移
if event.key == pygame.K_LEFT:
if plane['x'] > 0:
plane['x'] -= plane['step']
# 如果是【右移键】并且 飞机不超过右边界,就让飞机右移
elif event.key == pygame.K_RIGHT:
if plane['x'] < 480 - plane['width']:
plane['x'] += plane['step']
# 如果是【空格键】,就往列表中添加左、中、右三颗新子弹 ------
elif event.key == pygame.K_SPACE:
b1 = getBullet()
b1['place'] = '左'
b2 = getBullet()
b2['place'] = '中'
b3 = getBullet()
b3['place'] = '右'
bulletList.append(b1)
bulletList.append(b2)
bulletList.append(b3)
# 主循环
while True:
# 使用函数,事件监听
eventListen()
# 使用函数,绘制界面
draw()
# 使用函数,控制移动
move()
# 使用函数,删除已移出窗口的病毒,子弹
delete()
# 使用函数,检测所有物体,如果有碰撞,就做出响应
check()
# 更新窗口
pygame.display.update()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn