基于Python的坦克大戰(zhàn)小游戲

一。背景介紹

    90后小伙伴應(yīng)該對(duì)坦克大戰(zhàn)這款游戲很熟悉吧!!這款經(jīng)典游戲,筆者小時(shí)候玩過無(wú)數(shù)次,大多數(shù)時(shí)候都是和小伙伴們一起開玩,相信對(duì)很多人來說,這款游戲還是記憶深處的珍藏品。當(dāng)然,現(xiàn)在大家更熱衷于英雄聯(lián)盟、王者農(nóng)藥這樣的游戲了。今天就和大家一起分享下,用python的pygame庫(kù)制作的坦克大戰(zhàn)游戲。

1.png

二。游戲介紹

    打開游戲后是熟悉的主界面,關(guān)卡為兩關(guān),場(chǎng)景有石墻、鋼墻和樹林,其中紅色土石墻(子彈可以打通),白色鋼板(子彈打不通),樹林坦克進(jìn)去可以隱藏(子彈不隱藏),大本營(yíng)由土墻包圍,內(nèi)部有老鷹的圖騰,敵方坦克有三種,分別為 普通坦克、移速慢血多、移速快血少,打紅色坦克會(huì)產(chǎn)生食物  不同的食物有不同的效果(子彈加速、大本營(yíng)加固成鋼板、坦克生命+1等)。我方坦克和敵方坦克出生會(huì)有簡(jiǎn)單特效。

2.png

當(dāng)?shù)胤教箍巳克劳鰰r(shí)  出現(xiàn) Congratulations  字樣,當(dāng)我放坦克生命數(shù)為0或者大本營(yíng)被擊毀時(shí)  出現(xiàn)  Game Over  字樣。

三。游戲文件

游戲代碼部分由bullet.py、food.py、home.py、scene.py、tanks.py、main.py這六個(gè)文件組成,分別代表子彈、獎(jiǎng)勵(lì)物品、基地、場(chǎng)景、坦克及主文件。整個(gè)游戲主要基于Pygame庫(kù)進(jìn)行開發(fā),各模塊均用函數(shù)進(jìn)行封裝,以增強(qiáng)復(fù)用性,主文件的部分代碼如下所示:

#!/usr/bin/env python

# -*- coding:utf-8 -*-

import sys

import os

import pygame

import scene

import bullet

import food

import tanks

import home

from pygame.locals import *

# 開始界面顯示

def show_start_interface(screen, width, height):

    tfont = pygame.font.Font('./font/times.ttf', width//4)

    cfont = pygame.font.Font('./font/times.ttf', width//20)

    title = tfont.render(u'Tank War', True, (255, 0, 0))

    content1 = cfont.render(u'Press 1 for one player', True, (0, 0, 255))

    content2 = cfont.render(u'Press 2 for two players', 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

# 結(jié)束界面顯示

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/times.ttf', width//10)

        content = font.render(u'Congratulations!', 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()

除了代碼文件外,游戲文件還包含音樂、字體、圖片等文件,最終游戲的效果也非常接近小時(shí)候的味道~~

感興趣的小伙伴可以關(guān)注“320科技工作室”的微信公眾號(hào),找管理員獲取源文件~~

登錄后免費(fèi)查看全文
立即登錄
App下載
技術(shù)鄰APP
工程師必備
  • 項(xiàng)目客服
  • 培訓(xùn)客服
  • 平臺(tái)客服

TOP

16
8
2