鱼C论坛

 找回密码
 立即注册
查看: 1546|回复: 5

自作游戏,修改37讲的鱼与乌龟游戏

[复制链接]
发表于 2020-11-2 10:07:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
我想以鱼与乌龟游戏为基础,做一个类似与吃鸡的游戏.  本人是第一次自己写代码,有很多错误的地方望大神指导下.

首先我现在的思路是,在一个100*100的范围里面 生产 (普通士兵,射手,伏地魔,医疗兵,) 他们每个人都有自己的特性, 比如说医疗兵没移动一次可以增加回复1点体力,伏地魔移动速度非常慢,但是不被其他士兵发现,而且如果与其他士兵的位置重叠后可以杀死其他士兵. 射手就是当与其他士兵的距离为2-4的时候可以杀死其,如果为1的话将被其他士兵杀死.  地图每一个回合会缩小2-4, 当士兵在圈以外的地方的时候 血量会-1.

import random as r

def map1(x,y):
    x = 100
    y = 100
    return map1(x - r.randint(2,4),y - r.randint(2,4))


class Soldiers:
    def __init__(self):
        self.power = 5
        self.x = r.randint(0,100)
        self.y = r.randint(0,100)

    def move(self):
        new_x = self.x + r.choice([1,3,-1,-3])
        new_y = self.y + r.choice([1,3,-1,-3])

        if new_x < map1(x):
            self.x = 0 - (new_x - 0)
            self.power -= 1

        elif new_x > map1(x):
            self.x = map1(x) - (new_x - map1(x))
            self.power -= 1

        else:
            self.x = new_x


        if new_y < map1(y):
            self.y = 0 - (new_y - 0)
            self.power -= 1

        elif new_y > map1(y):
            self.y = map1(y) - (new_y - map1(y))
            self.power -= 1

        else:
            self.y =  new_y

        return (self.x, self.y)

        

class Shooter(Soldiers):
    def __init__(self):
        self.power = 2
        super().__init__()


    def move(delf):
        new_x = self.x + r.choice([2,-2])
        new_y = self.y + r.choice([2,-2])
        

class Lord_Voldemort(Soldiers):
    def __init__(self):
        self.power = 5
        super().__init__()

    def move(self):
        new_x = self.x + r.choice([1,-1])
        new_y = self.y + r.choice([1,-1])


class Medic(Soldiers):
    def __init__(self):
        self.power = 3
        if self.power > 5:
            self.power = 5
        super().__init__()
        
    def move(self):
        new_x = self.x + r.choice([2,-2])
        new_y = self.y + r.choice([2,-2])

        if new_x < map1(x):
            self.x = 0 - (new_x - 0)
            self.power -= 1

        elif new_x > map1(x):
            self.x = map1(x) - (new_x - map1(x))
            self.power -= 1

        else:
            self.x = new_x


        if new_y < map1(y):
            self.y = 0 - (new_y - 0)
            self.power -= 1

        elif new_y > map1(y):
            self.y = map1(y) - (new_y - map1(y))
            self.power -= 1

        else:
            self.y =  new_y
            
        self.power += 1
        return (self.x, self.y)




soldier = Soldiers()

shooter = Shooter()

lord_voldemort = Lord_Voldemort()

medic = Medic()


while True:
    if not soldier.power:
        print('普通士兵被淘汰了')

    if not shooter.power:
        print('射手被淘汰了')

    if not lord_voldemort.power:
        print('伏地魔被毒死了')

    if not medic.power:
        print('医疗兵被淘汰了')

    soldier_pos = soldier.move()
    shooter_pos = shooter.move()
    lord_voldemort_pos = lord_voldemort.move()
    medic_pos = medic.move()

    if soldier_pos == shooter_pos:
        print('射手被普通士兵击杀!')
        shooter.power = 0
    if lord_voldemort_pos == soldier_pos:
        print('普通士兵被伏地魔偷袭了!')
        soldier.power = 0

    if lord_voldemort_pos == medic_pos:
        print('医疗兵被伏地魔偷袭了!')
        medic.power = 0

    if lord_voldemort_pos == shooter_pos:
        print('射手被伏地魔偷袭了!')
        shooter.power = 0
        
   

   




   

目前代码写到了这里. 有许多的error ,比如说if new_x < map1(x) 中的map1(x) 无法被识别.

还有就是有没有好的方法去实现 射手就是当与其他士兵的距离为2-4的时候可以杀死其的代码呢?
当只剩下一个幸存者的时候可以显示出来.
或者还有什么好的建议都可以!!

请大神指导一下小白!!!!


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-11-2 14:49:22 | 显示全部楼层
if new_x < map1(x) 中的map1(x) 无法被识别.=========那是因为你定义的函数有2个参数,而你只传入了1个。。。
射手就是当与其他士兵的距离为2-4的时候可以杀死其的代码呢?========这个加判断就行啊,判断射手的坐标减去其他士兵的坐标,再abs()函数取绝对值,在2-4就可以杀死其他士兵,在2以内则被杀死。

当只剩下一个幸存者的时候可以显示出来.========这个你可以用列表操作的,把所有角色加入列表,死了就remove掉,剩余1的时候显示就行了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-2 19:04:42 | 显示全部楼层
谢谢大神!!!! 不好意思,请问如何传入2个参数呢?
本人小白!!不要介意!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-2 19:05:15 | 显示全部楼层
笨鸟学飞 发表于 2020-11-2 14:49
if new_x < map1(x) 中的map1(x) 无法被识别.=========那是因为你定义的函数有2个参数,而你只传入了1个。 ...


谢谢大神!!!! 不好意思,请问如何传入2个参数呢?
本人小白!!不要介意!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-11-2 21:10:49 | 显示全部楼层
  1. def map1(x,y):  #你自己定义的函数需要两个参数。而且这个函数应该是有问题的函数
  2.     x = 100
  3.     y = 100
  4.     return map1(x - r.randint(2,4),y - r.randint(2,4))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-11-3 18:06:52 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-11 01:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表