自制卡牌游戏Python

这次我会开一个系列,持续更新(也不排除放弃的可能)。

先进行基本的框架搭建

import pygame,sys
canvas=pygame.display.set_mode((1350,680))
pygame.display.set_caption('卡牌')

event=[]
def handleEvent():
    global events
    for e in pygame.event.get():
        if e.type==pygame.QUIT:
            pygame.quit()
            sys.exit()

while True:
    handleEvent()
    pygame.display.update()

接下来,我要创建一个地图,大体参照了最近游戏《邪恶冥刻》的形式:

 我总结了一下它的逻辑:

 既然它有这么多方面。那我们肯定会按其中一方面生成并在同时满足其他方面:这里我选择依照层级方面生成:

稍等,我实在是对我的代码水平不自信,咱们还是先做出显示函数吧。
def mapDisplay():
    while True:
        handleEvent()

        canvas.blit(mapImg[0],(0,0))

        canvas.blit(mapImg[1],(0,0))

        pygame.display.update()

mapDisplay()

现在可以开始一边编写算法一边完善显示了:

#记得引入random库并以r使用
def create_map(mapLength):
    global mapNotes
    notes_events=['card','printer','hospital','magic_stone','fight']

    mapNotes=[]
    for i in range(mapLength):
        if i==0:
            mapNotes.append(['card'])
        elif i==mapLength-1:
            mapNotes.append(['boss_fight'])
        else:
            tempL=[]
            for i in range(r.randint(1,4)):
                tempL.append(notes_events[r.randint(0,len(notes_events)-1)])
            mapNotes.append(tempL)
def mapDisplay():
    print(mapNotes)
    while True:
        handleEvent()

        canvas.blit(mapImg[0],(0,0))

        canvas.blit(mapImg[1],(0,0))

        pygame.display.update()

我们现在能生成符合要求的节点了,来开始生成连线吧!

先说一下逻辑:看看我们的示意图:

 是不是可以以一个 起点*终点 大小,1表示连接,0表示断开的矩阵表示?例如第二排到第三排

     1    2    3    4        第三至第四    1    1    1    1

1   1    0    0    0

2   0    1    1    1

我们发现了规律:每一行每一列至少但不限于1条连接,现在我们可以开始了:

#注意导入numpy库并以np调用
def create_map(mapLength):
    global mapNotes,mapWires
    #节点生成
    notes_events=['card','printer','hospital','magic_stone','fight']

    mapNotes=[]
    for i in range(mapLength):
        if i==0:
            mapNotes.append(['card'])
        elif i==mapLength-1:
            mapNotes.append(['boss_fight'])
        else:
            tempL=[]
            for i in range(r.randint(1,4)):
                tempL.append(notes_events[r.randint(0,len(notes_events)-1)])
            mapNotes.append(tempL)
    
    #连线生成
    mapWires=[]
    for i in range(mapLength-1):
        tempArray=np.zeros((len(mapNotes[i]),len(mapNotes[i+1])))

        for j in range(len(mapNotes[i])):
            tempArray[j,r.randint(0,len(mapNotes[i+1])-1)]=1
        for k in range(len(mapNotes[i+1])):
            tempArray[r.randint(0,len(mapNotes[i])-1),k]=1

        mapWires.append(tempArray)

def mapDisplay():
    print(mapNotes)
    for i in range(4):
        print(mapWires[i])
    while True:
        handleEvent()

        canvas.blit(mapImg[0],(0,0))

        canvas.blit(mapImg[1],(0,0))

        pygame.display.update()

这是我的其中一次运行结果:

[['card'], ['printer', 'hospital', 'card', 'card']
, ['magic_stone', 'hospital', 'printer'], ['hospital', 'hospital'], ['boss_fight']]
[[1. 1. 1. 1.]]
[[0. 1. 1.]    
 [0. 1. 1.]    
 [1. 0. 1.]    
 [1. 0. 0.]]   
[[1. 1.]       
 [0. 1.]       
 [1. 1.]]      
[[1.]
 [1.]]

我手绘了一下以展示:

 有点乱,但我满足了。让我们添加显示系统吧。

import pygame,sys,random as r,numpy as np
canvas=pygame.display.set_mode((1350,680))
pygame.display.set_caption('卡牌')

#加载图片
mapImg={0:pygame.image.load('images/map(layer0).png'),\
    1:pygame.image.load('images/map(layer1).png')}
noteImg={'card':pygame.image.load('images/notes_events/card.png'),\
    'printer':pygame.image.load('images/notes_events/printer.png'),\
        'hospital':pygame.image.load('images/notes_events/hospital.png'),\
            'magic_stone':pygame.image.load('images/notes_events/magic_stone.png'),\
                'fight':pygame.image.load('images/notes_events/fight.png'),\
                    'boss_fight':pygame.image.load('images/notes_events/boss.png')}

#触发事件
event=[]
def handleEvent():
    global events
    for e in pygame.event.get():
        if e.type==pygame.QUIT:
            pygame.quit()
            sys.exit()

def create_map(mapLength):
    global mapNotes,mapWires
    #节点生成
    notes_events=['card','printer','hospital','magic_stone','fight']

    mapNotes=[]
    for i in range(mapLength):
        if i==0:
            mapNotes.append(['card'])
        elif i==mapLength-1:
            mapNotes.append(['boss_fight'])
        else:
            tempL=[]
            for i in range(r.randint(1,4)):
                tempL.append(notes_events[r.randint(0,len(notes_events)-1)])
            mapNotes.append(tempL)
    
    #连线生成
    mapWires=[]
    for i in range(mapLength-1):
        tempArray=np.zeros((len(mapNotes[i]),len(mapNotes[i+1])))

        for j in range(len(mapNotes[i])):
            tempArray[j,r.randint(0,len(mapNotes[i+1])-1)]=1
        for k in range(len(mapNotes[i+1])):
            tempArray[r.randint(0,len(mapNotes[i])-1),k]=1

        mapWires.append(tempArray)

noteDisplayPos={1:{0:316},2:{0:188,1:408},3:{0:150,1:280,2:400},4:{0:120,1:270,2:340,3:510}}
def mapDisplay():
    print(mapNotes)
    for i in range(4):
        print(mapWires[i])
    
    playerLayer=0
    while True:
        handleEvent()

        canvas.blit(mapImg[0],(0,0))
        for i in range(len(mapNotes)):
            for j in range(len(mapNotes[i])):
                canvas.blit(noteImg[mapNotes[i][j]],((i-playerLayer)*200+250,noteDisplayPos[len(mapNotes[i])][j]))
        
        for i in range(len(mapWires)):
            wires=mapWires[i]
            for a in range(wires.shape[0]):
                for b in range(wires.shape[1]):
                    if wires[a,b]==1:
                        pygame.draw.line(canvas,(10,10,10),\
                            ((i-playerLayer)*200+250+70,noteDisplayPos[len(mapNotes[i])][a]+32),\
                                ((i+1-playerLayer)*200+250-6,noteDisplayPos[len(mapNotes[i+1])][b]+32),4)
        canvas.blit(mapImg[1],(0,0))

        pygame.display.update()

create_map(7)
mapDisplay()

现在地图系统暂时告一段落,让我们去编写战斗吧。

(15条消息) images.rarrrrrrrrrrrrrrr-其他文档类资源-CSDN文库

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python 是一种非常强大的编程语言,适用于各种各样的应用开发,包括制作卡牌游戏。下面我会用 300 字为您介绍如何用 Python 制作一个简便的卡牌游戏。 首先,我们可以定义一个卡牌类。每张卡牌有自己的一些属性,比如名字、攻击力、生命值等等。我们可以使用类属性来表示这些属性。同时,我们还可以在卡牌类中定义一些方法,来表示一些卡牌的特殊能力。 接下来,我们可以创建一个玩家类,表示游戏中的玩家。每个玩家有自己的名字、生命值以及手牌等属性。我们可以在玩家类中定义一些方法,比如抽牌、出牌、计算伤害等等。 然后,我们可以创建一个游戏类,表示整个游戏的逻辑。游戏类可以包含两个玩家对象,以及一些游戏状态,比如当前回合数、当前行动玩家等等。游戏类中可以定义一些方法,比如开始游戏、进行回合等等。 最后,我们可以在一个主函数中实例化游戏对象,并调用开始游戏方法来启动整个游戏。在每个回合中,我们可以在控制台中输出当前游戏状态,让玩家进行相应的操作。比如选择一张手牌出牌、选择一个目标等等。然后根据玩家的选择,来执行相应的逻辑,比如计算伤害、移除卡牌等操作。 这样,我们就完成了一个简便的卡牌游戏制作。当然,为了更好地交互体验,我们也可以使用 Pygame 库来制作一个图形界面,提供更好的游戏体验。 总结起来,Python 提供了强大的编程能力,可以用来制作各种各样的应用,包括卡牌游戏。通过定义卡牌类、玩家类和游戏类,我们可以实现一个简便的卡牌游戏,并通过控制台或图形界面与玩家进行交互。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值