利用python3的pygame模塊基本實(shí)現(xiàn)塔防游戲的基本功能,包括血量和分?jǐn)?shù)顯示,bgm,防御塔建造,防御塔攻擊范圍內(nèi)的敵軍,暫停和加速功能。由于實(shí)在沒(méi)有素材,用的都是自己截圖P的,所以美化不好。但基本保證功能,其中有一個(gè)BUG,但不影響游戲效果。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專(zhuān)注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、重慶小程序開(kāi)發(fā)公司、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶(hù)創(chuàng)新互聯(lián)還提供了漢川免費(fèi)建站歡迎大家使用!1.運(yùn)行主類(lèi)
"""主程序""" from pygame.locals import * from TowerDefend.enemy import * from TowerDefend.towerposSet import * from TowerDefend.tower import * import pygame def run(): """運(yùn)行函數(shù)""" pygame.init() size = width, height = 1200, 600 screen = pygame.display.set_mode(size) background_img = pygame.image.load(r'image/background.png').convert_alpha() background_img = pygame.transform.scale(background_img, (width, height)) # 創(chuàng)建分?jǐn)?shù)和血量 health_count = 5 score_count = 0 score = pygame.font.Font('font/score_health.ttf', 30) health = pygame.font.Font('font/score_health.ttf', 30) # 創(chuàng)建背景音樂(lè) bg_music = pygame.mixer.music bg_music.load('media/bg.mp3') bg_music.set_volume(2) # 創(chuàng)建敵軍類(lèi) enemies = pygame.sprite.Group() ENEMY_NUM = 5 position = [[258, 600], [258, 670], [258, 740], [258, 810], [258, 880]] for i in range(ENEMY_NUM): enemies.add(Enemy(position[i])) # 創(chuàng)建炮塔 towers = pygame.sprite.Group() # 加載暫停鍵 pause_img = pygame.image.load('image/pause.png').convert_alpha() pause_rect = pause_img.get_rect() pause_rect.left, pause_rect.top = 1145, 0 # 加載血量和金幣顯示 health_money_img = pygame.image.load('image/health_money.png').convert_alpha() health_money_rect = health_money_img.get_rect() health_money_rect.left, health_money_rect.top = 0, 0 # 加載加速鍵 speed_img = pygame.image.load('image/speed.png').convert_alpha() speed_rect = speed_img.get_rect() speed_rect.left, speed_rect.top = 1090, 0 # 設(shè)置炮塔位置 towers_pos = pygame.sprite.Group() position_list = [[225, 495], [264, 428], [312, 428], [362, 428], [410, 428], [460, 428], [508, 428], [561, 428], [561, 373], [377, 373]] for i in range(len(position_list)): towers_pos.add(Position(position_list[i])) # 設(shè)置循環(huán)條件 running = True clock = pygame.time.Clock() paused = False # 播放音樂(lè) if not bg_music.get_busy(): bg_music.play(-1) while running: clock.tick(100) for event in pygame.event.get(): if event.type == QUIT: running = False if event.type == MOUSEBUTTONDOWN: if event.button == 1: if speed_rect.collidepoint(event.pos): for each in enemies: each.accelerate *= 2 if pause_rect.collidepoint(event.pos): paused = not paused for each in towers_pos: if each.rect.collidepoint(event.pos): tower = Tower([each.rect.left, each.rect.top]) towers.add(tower) towers_pos.remove(each) if not paused: for enemy in enemies: if enemy.active: enemy.move() else: if enemy.rect.top <= 180: health_count -= 1 if enemy.rect.top > 180: score_count += 20 enemies.remove(enemy) # 繪制界面設(shè)置 screen.blit(background_img, (0, 0)) screen.blit(health.render(str(health_count), True, (255, 255, 255)), (60, 0.3)) screen.blit(score.render(str(score_count), True, (255, 255, 255)), (130, 0.5)) screen.blit(health_money_img, health_money_rect) screen.blit(pause_img, pause_rect) screen.blit(speed_img, speed_rect) # 繪制炮塔 for each in towers: each.draw(screen, enemies) each.hit(enemies) # 繪制炮塔位置 towers_pos.draw(screen) # 敵軍若存活則繪制其和血量 for enemy in enemies: if enemy.active: screen.blit(enemy.img, enemy.rect) enemy.drawhealth(screen) pygame.display.flip() pygame.quit() if __name__ == "__main__": run() 2.炮塔類(lèi) """TOWER""" import pygame import math class Tower(pygame.sprite.Sprite): """tower""" def __init__(self, pos): pygame.sprite.Sprite.__init__(self) self.img0 = pygame.image.load('image/tower0.png') self.img1 = pygame.image.load('image/tower1.png') self.img2 = pygame.image.load('image/tower2.png') self.rect = self.img0.get_rect() self.rect.left, self.rect.top = pos self.count = 1 def draw(self, screen, enemies): """繪制""" if self.count > 90: self.count = 1 if 1 <= self.count < 30: screen.blit(self.img0, self.rect) elif 30 <= self.count < 60: screen.blit(self.img1, self.rect) else: screen.blit(self.img2, self.rect) for enemy in enemies: distance = math.sqrt( math.pow((self.rect.left - enemy.rect.left), 2) + math.pow((self.rect.top - enemy.rect.top), 2)) if distance < 50 and enemy.active is True: self.count += 1 def hit(self, enemies): """攻擊""" for enemy in enemies: distance = math.sqrt( math.pow((self.rect.left - enemy.rect.left), 2) + math.pow((self.rect.top - enemy.rect.top), 2)) if distance < 50: enemy.health -= 1 if enemy.health == 0: enemy.active = False
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。
名稱(chēng)欄目:利用python3的pygame模塊實(shí)現(xiàn)塔防游戲-創(chuàng)新互聯(lián)
分享URL:http://jinyejixie.com/article16/dehcgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷(xiāo)推廣、網(wǎng)站導(dǎo)航、網(wǎng)站設(shè)計(jì)公司、Google、微信小程序、定制開(kāi)發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容