【python游戏】这年头塔除了拆还能干什么?这款好玩上瘾的塔防游戏,了解一下嘛

前言

嗨喽~大家好呀,这里是魔王呐 ❤ ~!

炮塔防御(Tower Defence),简称塔防,一种游戏类型。

炮塔防御游戏起源于最早的一款Desktop Tower Defence游戏。

塔防受众很广,游戏模式简单而可玩性强,且乐趣无穷,是智力和策略的比拼。

现在除了单纯的建造炮塔防御怪物,更产生了像《植物大战僵尸》这样的新型衍生类塔防游戏。

关于植物大战僵尸python制作源码也可以看看我之前的一篇文章:

【python游戏制作】僵尸来袭 ~ 快来一起创造植物叭~

而今天制作的塔防游戏,完整的项目源码 点击此处跳转文末名片免费获取

制作准备

图片素材(仅部分)

图片素材还有很多就不展示了哈,完整的素材包+源码自己 点击此处跳转文末名片免费获取

字体素材

音乐素材

敲代码

代码比较多的哈,如果文章懒得看,可以直接找我拿完整的项目自己慢慢研究也可。

完整代码 点击此处跳转文末名片免费获取噢~

1)游戏运行主程序
import pygame
from interface import END
from interface import START
from interface import GAMING
from interface import CHOICE
WIDTH = 800
HEIGHT = 600


'''主函数'''
def main():
	pygame.init()
	pygame.mixer.init()
	pygame.mixer.music.load('resource/audios/1.mp3')
	pygame.mixer.music.play(-1, 0.0)
	pygame.mixer.music.set_volume(0.25)
	screen = pygame.display.set_mode((WIDTH, HEIGHT))
	pygame.display.set_caption("塔防游戏")
	clock = pygame.time.Clock()
	# 调用游戏开始界面
	start_interface = START.START(WIDTH, HEIGHT)
	is_play = start_interface.update(screen)
	if not is_play:
		return
	# 调用游戏界面
	while True:
		choice_interface = CHOICE.CHOICE(WIDTH, HEIGHT)
		map_choice, difficulty_choice = choice_interface.update(screen)
		game_interface = GAMING.GAMING(WIDTH, HEIGHT)
		game_interface.start(screen, map_path='./maps/%s.map' % map_choice, difficulty_path='./difficulty/%s.json' % difficulty_choice)
		end_interface = END.END(WIDTH, HEIGHT)
		end_interface.update(screen)


'''run'''
if __name__ == '__main__':
	main()
2)游戏开始界面
import sys
import pygame


'''游戏开始界面'''
class StartInterface(pygame.sprite.Sprite):
	def __init__(self, WIDTH, HEIGHT):
		pygame.sprite.Sprite.__init__(self)
		self.imgs = ['./resource/imgs/start/start_interface.png']
		self.image = pygame.image.load(self.imgs[0]).convert()
		self.rect = self.image.get_rect()
		self.rect.center = WIDTH/2, HEIGHT/2
	'''just pass'''
	def update(self):
		pass


'''开始游戏按钮'''
class PlayButton(pygame.sprite.Sprite):
	def __init__(self, position=(220, 415)):
		pygame.sprite.Sprite.__init__(self)
		self.imgs = ['./resource/imgs/start/play_black.png', './resource/imgs/start/play_red.png']
		self.img_1 = pygame.image.load(self.imgs[0]).convert()
		self.img_2 = pygame.image.load(self.imgs[1]).convert()
		self.image = self.img_1
		self.rect = self.image.get_rect()
		self.rect.center = position
	'''不断地更新检测鼠标是否在按钮上'''
	def update(self):
		mouse_pos = pygame.mouse.get_pos()
		if self.rect.collidepoint(mouse_pos):
			self.image = self.img_2
		else:
			self.image = self.img_1


'''结束游戏按钮'''
class QuitButton(pygame.sprite.Sprite):
	def __init__(self, position=(580, 415)):
		pygame.sprite.Sprite.__init__(self)
		self.imgs = ['./resource/imgs/start/quit_black.png', './resource/imgs/start/quit_red.png']
		self.img_1 = pygame.image.load(self.imgs[0]).convert()
		self.img_2 = pygame.image.load(self.imgs[1]).convert()
		self.image = self.img_1
		self.rect = self.image.get_rect()
		self.rect.center = position
	'''不断地更新检测鼠标是否在按钮上'''
	def update(self):
		mouse_pos = pygame.mouse.get_pos()
		if self.rect.collidepoint(mouse_pos):
			self.image = self.img_2
		else:
			self.image = self.img_1


'''游戏开始类'''
class START():
	def __init__(self, WIDTH, HEIGHT):
		self.SI = StartInterface(WIDTH, HEIGHT)
		self.PB = PlayButton()
		self.QB = QuitButton()
		self.components = pygame.sprite.LayeredUpdates(self.SI, self.PB, self.QB)
	'''外部调用'''
	def update(self, screen):
		clock = pygame.time.Clock()
		while True:
			clock.tick(60)
			self.components.update()
			self.components.draw(screen)
			pygame.display.flip()
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					sys.exit(0)
					pygame.quit()
				elif event.type == pygame.MOUSEBUTTONDOWN:
					if event.button == 1:
						mouse_pos = pygame.mouse.get_pos()
						if self.PB.rect.collidepoint(mouse_pos):
							return True
						elif self.QB.rect.collidepoint(mouse_pos):
							return False

完整代码 点击此处跳转文末名片免费获取噢~

3)游戏进行中界面
import sys
import json
import math
import random
import pygame
sys.path.append('..')
from sprites import Enemy
from sprites import Turret
from interface import PAUSE
from pygame.locals import *
from collections import namedtuple


# 按钮类: 位置、文本、点击触发的事件
Button = namedtuple('Button', ['rect', 'text', 'onClick'])
# 定义一些颜色
info_color = (120, 20, 50)
red = (255, 0, 0)
green = (0, 255, 0)
black = (0, 0, 0)
white = (255, 255, 255)
grey = (127, 127, 127)
button_color1 = (0, 200, 0)
button_color2 = (0, 100, 0)


'''游戏进行中界面'''
class GAMING():
	def __init__(self, WIDTH=800, HEIGHT=600):
		self.WIDTH = WIDTH
		self.HEIGHT = HEIGHT
		# 游戏地图大小
		map_w = WIDTH
		map_h = 500
		# 按钮大小和位置
		button_w = 60
		button_h = 60
		button_y = 520
		# 间隙
		gap = 20
		# 按钮放在工具栏, 工具栏两端各有一个信息显示框
		toolbar_w = gap * 7 + button_w * 6
		info_w = (WIDTH - toolbar_w) // 2
		info_h = HEIGHT - map_h
		toolbar_h = HEIGHT - map_h
		# 界面布置
		self.map_rect = pygame.Rect(0, 0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值