5# coding: utf-8
# 游戏运行主程序
import sys
import pygame
import scene
import bullet
import food
import tanks
import home
from pygame.locals import *
print('Python学习交流群:125240963,群内每天分享干货,包括最新的python企业案例学习资料和零基础入门教程,欢迎各位小伙伴入群学习交流')
# 开始界面显示
def show_start_interface(screen, width, height):
tfont = pygame.font.Font('./font/simkai.ttf', width//4)
cfont = pygame.font.Font('./font/simkai.ttf', width//20)
title = tfont.render(u'坦克大战', True, (255, 0, 0))
content1 = cfont.render(u'按1键进入单人游戏', True, (0, 0, 255))
content2 = cfont.render(u'按2键进入双人人游戏', True, (0, 0, 255))
trect = title.get_rect()
trect.midtop = (width/2, height/4)
crect1 = content1.get_rect()
crect1.midtop = (width/2, height/1.8)
crect2 = content2.get_rect()
crect2.midtop = (width/2, height/1.6)
screen.blit(title, trect)
screen.blit(content1, crect1)
screen.blit(content2, crect2)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_1:
return 1
if event.key == pygame.K_2:
return 2
# 结束界面显示
def show_end_interface(screen, width, height, is_win):
bg_img = pygame.image.load("./images/others/background.png")
screen.blit(bg_img, (0, 0))
if is_win:
font = pygame.font.Font('./font/simkai.ttf', width//10)
content = font.render(u'恭喜通关!', True, (255, 0, 0))
rect = content.get_rect()
rect.midtop = (width/2, height/2)
screen.blit(content, rect)
else:
fail_img = pygame.image.load("./images/others/gameover.png")
rect = fail_img.get_rect()
rect.midtop = (width/2, height/2)
screen.blit(fail_img, rect)
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
# 关卡切换
def show_switch_stage(screen, width, height, stage):
bg_img = pygame.image.load("./images/others/background.png")
screen.blit(bg_img, (0, 0))
font = pygame.font.Font('./font/simkai.ttf', width//10)
content = font.render(u'第%d关' % stage, True, (0, 255, 0))
rect = content.get_rect()
rect.midtop = (width/2, height/2)
screen.blit(content, rect)
pygame.display.update()
delay_event = pygame.constants.USEREVENT
pygame.time.set_timer(delay_event, 1000)
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == delay_event:
return
# 主函数
def main():
# 初始化
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((630, 630))
pygame.display.set_caption("坦克大战")
# 加载图片
bg_img = pygame.image.load("./images/others/background.png")
# 加载音效
add_sound = pygame.mixer.Sound("./audios/add.wav")
add_sound.set_volume(1)
bang_sound = pygame.mixer.Sound("./audios/bang.wav")
bang_sound.set_volume(1)
blast_sound = pygame.mixer.Sound("./audios/blast.wav")
blast_sound.set_volume(1)
fire_sound = pygame.mixer.Sound("./audios/fire.wav")
fire_sound.set_volume(1)
Gunfire_sound = pygame.mixer.Sound("./audios/Gunfire.wav")
Gunfire_sound.set_volume(1)
hit_sound = pygame.mixer.Sound("./audios/hit.wav")
hit_sound.set_volume(1)
start_sound = pygame.mixer.Sound("./audios/start.wav")
start_sound.set_volume(1)
# 开始界面
num_player = show_start_interface(screen, 630, 630)
# 播放游戏开始的音乐
start_sound.play()
# 关卡
stage = 0
num_stage = 2
# 游戏是否结束
is_gameover = False
# 时钟
clock = pygame.time.Clock()
# 主循环
while not is_gameover:
# 关卡
stage += 1
if stage > num_stage:
break
show_switch_stage(screen, 630, 630, stage)
# 该关卡坦克总数量
enemytanks_total = min(stage * 18, 80)
# 场上存在的敌方坦克总数量
enemytanks_now = 0
# 场上可以存在的敌方坦克总数量
enemytanks_now_max = min(max(stage * 2, 4), 8)
# 精灵组
tanksGroup = pygame.sprite.Group()
mytanksGroup = pygame.sprite.Group()
enemytanksGroup = pygame.sprite.Group()
bulletsGroup = pygame.sprite.Group()
mybulletsGroup = pygame.sprite.Group()
enemybulletsGroup = pygame.sprite.Group()
myfoodsGroup = pygame.sprite.Group()
# 自定义事件
# -生成敌方坦克事件
genEnemyEvent = pygame.constants.USEREVENT
pygame.time.set_timer(genEnemyEvent, 100)
# -敌方坦克静止恢复事件
recoverEnemyEvent = pygame.constants.USEREVENT
pygame.time.set_timer(recoverEnemyEvent, 8000)
# -我方坦克无敌恢复事件
noprotectMytankEvent = pygame.constants.USEREVENT
pygame.time.set_timer(noprotectMytankEvent, 8000)
# 关卡地图
map_stage = scene.Map(stage)
# 我方坦克
tank_player1 = tanks.myTank(1)
tanksGroup.add(tank_player1)
mytanksGroup.add(tank_player1)
if num_player > 1:
tank_player2 = tanks.myTank(2)
tanksGroup.add(tank_player2)
mytanksGroup.add(tank_player2)
is_switch_tank = True
player1_moving = False
player2_moving = False
# 为了轮胎的动画效果
time = 0
# 敌方坦克
for i in range(0, 3):
if enemytanks_total > 0:
enemytank = tanks.enemyTank(i)
tanksGroup.add(enemytank)
enemytanksGroup.add(enemytank)
enemytanks_now += 1
enemytanks_total -= 1
# 大本营
myhome = home.Home()
# 出场特效
appearance_img = pygame.image.load("./images/others/appear.png").convert_alpha()
appearances = []
appearances.append(appearance_img.subsurface((0, 0), (48, 48)))
appearances.append(appearance_img.subsurface((48, 0), (48, 48)))
appearances.append(appearance_img.subsurface((96, 0), (48, 48)))
# 关卡主循环
while True:
if is_gameover is True:
break
if enemytanks_total < 1 and enemytanks_now < 1:
is_gameover = False
break
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == genEnemyEvent:
if enemytanks_total > 0:
if enemytanks_now < enemytanks_now_max:
enemytank = tanks.enemyTank()
if not pygame.sprite.spritecollide(enemytank, tanksGroup, False, None):
tanksGroup.add(enemytank)
enemytanksGroup.add(enemytank)
enemytanks_now += 1
enemytanks_total -= 1
if event.type == recoverEnemyEvent:
for each in enemytanksGroup:
each.can_move = True
if event.type == noprotectMytankEvent:
for each in mytanksGroup:
mytanksGroup.protected = False
# 检查用户键盘操作
key_pressed = pygame.key.get_pressed()
# 玩家一
# WSAD -> 上下左右
# 空格键射击
if key_pressed[pygame.K_w]:
tanksGroup.remove(tank_player1)
tank_player1.move_up(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)
tanksGroup.add(tank_player1)
player1_moving = True
elif key_pressed[pygame.K_s]:
tanksGroup.remove(tank_player1)
tank_player1.move_down(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)
tanksGroup.add(tank_player1)
player1_moving = True
elif key_pressed[pygame.K_a]:
tanksGroup.remove(tank_player1)
tank_player1.move_left(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)
tanksGroup.add(tank_player1)
player1_moving = True
elif key_pressed[pygame.K_d]:
tanksGroup.remove(tank_player1)
tank_player1.move_right(tanksGroup, map_stage.brickGroup, map_stage.ironGroup, myhome)
tanksGroup.add(tank_player1)
player1_moving = True
elif key_pressed[pygame.K_SPACE]:
if not tank_player1.bullet.being:
fire_sound.play()
tank_player1.shoot()
# 玩家二
# ↑↓←→ -> 上下左右
# 小键盘0键射击
if num_player > 1:
if key_pressed[pygame.K_UP
没有合适的资源?快使用搜索试试~ 我知道了~
Python 游戏源码 - 坦克大战版本2小游戏 Python源码
共75个文件
png:51个
pyc:10个
wav:7个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 176 浏览量
2023-10-19
15:08:18
上传
评论
收藏 7.5MB ZIP 举报
温馨提示
Python 游戏源码 - 坦克大战版本2小游戏 Python源码Python 游戏源码 - 坦克大战版本2小游戏 Python源码Python 游戏源码 - 坦克大战版本2小游戏 Python源码Python 游戏源码 - 坦克大战版本2小游戏 Python源码Python 游戏源码 - 坦克大战版本2小游戏 Python源码Python 游戏源码 - 坦克大战版本2小游戏 Python源码
资源推荐
资源详情
资源评论
收起资源包目录
游戏源码-08 坦克大战版本2.zip (75个子文件)
游戏源码-08 坦克大战版本2
坦克大战
font
simkai.ttf 11.24MB
audios
fire.wav 68KB
add.wav 128KB
Gunfire.wav 2KB
blast.wav 53KB
start.wav 863KB
hit.wav 790B
bang.wav 9KB
bullet.py 2KB
food.py 1KB
home.py 565B
images
enemyTank
enemy_2_1.png 16KB
enemy_1_1.png 16KB
enemy_4_3.png 16KB
enemy_1_2.png 16KB
enemy_2_0.png 16KB
enemy_3_2.png 16KB
enemy_2_2.png 16KB
enemy_3_0.png 16KB
enemy_2_3.png 16KB
enemy_3_3.png 16KB
enemy_3_1.png 16KB
enemy_1_0.png 16KB
enemy_4_2.png 16KB
enemy_1_3.png 16KB
enemy_4_0.png 16KB
enemy_4_1.png 16KB
others
boom_static.png 22KB
tip.png 3KB
logo.png 130KB
gameover.png 1KB
appear.png 15KB
mask.png 646B
background.png 1.15MB
protect.png 15KB
boom_dynamic.png 88KB
home
home_destroyed.png 17KB
home1.png 18KB
home2.png 22KB
myTank
tank_T2_0.png 16KB
tank_T2_1.png 16KB
tank_T1_2.png 16KB
tank_T2_2.png 16KB
tank_T1_0.png 16KB
tank_T1_1.png 16KB
scene
tree.png 1012B
river2.png 3KB
ice.png 2KB
brick.png 17KB
river1.png 3KB
iron.png 15KB
food
food_star.png 16KB
food_gun.png 16KB
food_clock.png 16KB
food_tank.png 16KB
food_iron.png 16KB
food_protect.png 16KB
food_boom.png 16KB
bullet
bullet_down.png 15KB
bullet_up.png 15KB
bullet_left.png 15KB
bullet_right.png 15KB
游戏运作主程序.py 18KB
__pycache__
home.cpython-37.pyc 923B
bullet.cpython-37.pyc 2KB
scene.cpython-36.pyc 6KB
home.cpython-36.pyc 910B
bullet.cpython-36.pyc 2KB
scene.cpython-37.pyc 5KB
food.cpython-37.pyc 1KB
tanks.cpython-37.pyc 9KB
tanks.cpython-36.pyc 10KB
food.cpython-36.pyc 1KB
tanks.py 14KB
scene.py 5KB
共 75 条
- 1
资源评论
douluo998
- 粉丝: 2003
- 资源: 5315
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功