一个动物类的小游戏【类和对象】

//Critter Caretaker
//Simulates caring for a virtual pet

#include <iostream>

using namespace std;

class Critter
{
public:          
	Critter(int hunger = 0, int boredom = 0); 
	void Talk();
	void Eat(int food = 5);
	void Play(int fun = 5);

private:
	int m_Hunger;
	int m_Boredom;

	int GetMood() const;
	void PassTime(int time = 1);

};

Critter::Critter(int hunger, int boredom):
m_Hunger(hunger),
	m_Boredom(boredom)
{}

inline int Critter::GetMood() const 
{
	return (m_Hunger + m_Boredom);
}

void Critter::PassTime(int time)
{
	m_Hunger += time;
	m_Boredom += time;
	cout<<"m_Hunger"<<m_Hunger<<endl<<"m_Boredom"<<m_Boredom<<endl;
}

void Critter::Talk()
{
	cout << "I'm a critter and I feel ";

	int mood = GetMood();
	if (mood > 15)
	{
		cout << "mad.\n";
	}
	else if (mood > 10)
	{
		cout << "frustrated.\n";
	}
	else if (mood > 5)
	{
		cout << "okay.\n";
	}
	else
	{
		cout << "happy.\n";
	}

	PassTime();
}

void Critter::Eat(int food) 
{
	cout << "Brruppp.\n";

	m_Hunger -= food;
	if (m_Hunger < 0)
	{
		m_Hunger = 0;
	}

	PassTime();
}

void Critter::Play(int fun)
{
	cout << "Wheee!\n";

	m_Boredom -= fun;
	if (m_Boredom < 0)
	{
		m_Boredom = 0;
	}

	PassTime();
}

int main()
{
	Critter crit;

	int choice = 1;  //start the critter off talking
	while (choice != 0)
	{
		cout << "\nCritter Caretaker\n\n";
		cout << "0 - Quit\n";
		cout << "1 - Listen to your critter\n";
		cout << "2 - Feed your critter\n";
		cout << "3 - Play with your critter\n\n";

		cout << "Choice: ";
		cin >> choice;

		switch (choice)
		{
		case 0:	
			cout << "Good-bye.\n";
			break;
		case 1:	
			crit.Talk();
			break;
		case 2:	
			crit.Eat();
			break;
		case 3:	
			crit.Play();
			break;
		default:
			cout << "\nSorry, but " << choice << " isn't a valid choice.\n";
		}
	}

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,斗兽棋是一种中国传统棋类游戏,以下是一个简单的Python斗兽棋小游戏的代码示例: ```python import pygame import random pygame.init() # 游戏窗口大小 win_width = 600 win_height = 600 # 定义颜色 white = (255, 255, 255) black = (0, 0, 0) red = (255, 0, 0) # 定义动物类 class Animal(pygame.sprite.Sprite): def __init__(self, name, rank, x, y): super().__init__() self.name = name self.rank = rank self.image = pygame.Surface((50, 50)) self.image.fill(white) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y def move(self, x, y): self.rect.x = x self.rect.y = y # 创建游戏窗口 win = pygame.display.set_mode((win_width, win_height)) pygame.display.set_caption("Python斗兽棋小游戏") # 创建动物对象,并添加到精灵组 all_sprites = pygame.sprite.Group() animals = [] animals.append(Animal("鼠", 1, 0, 0)) animals.append(Animal("猫", 2, 50, 0)) animals.append(Animal("狼", 3, 100, 0)) animals.append(Animal("狗", 4, 150, 0)) animals.append(Animal("豹", 5, 200, 0)) animals.append(Animal("虎", 6, 250, 0)) animals.append(Animal("狮", 7, 300, 0)) animals.append(Animal("象", 8, 350, 0)) for animal in animals: all_sprites.add(animal) # 创建棋盘 board = pygame.Surface((500, 500)) board.fill((255, 228, 181)) pygame.draw.rect(board, black, (50, 50, 400, 100), 1) pygame.draw.rect(board, black, (50, 450, 400, 100), 1) pygame.draw.line(board, black, (200, 50), (200, 150), 1) pygame.draw.line(board, black, (400, 50), (400, 150), 1) pygame.draw.line(board, black, (200, 450), (200, 550), 1) pygame.draw.line(board, black, (400, 450), (400, 550), 1) win.blit(board, (50, 50)) # 游戏主循环 clock = pygame.time.Clock() turn = 1 # 玩家1先手 selected_animal = None while True: clock.tick(60) for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: x, y = event.pos # 判断点击的是否是动物 for animal in animals: if animal.rect.collidepoint(x, y): if animal.rank == 1 or animal.rank == 8: if turn == 1: selected_animal = animal elif turn == 1: selected_animal = animal else: pass # 判断点击的是否是棋盘 if 50 <= x <= 450 and 50 <= y <= 550: if selected_animal: # 计算动物要移动到的位置 row = (y - 50) // 50 col = (x - 50) // 50 target_x = 50 + col * 50 target_y = 50 + row * 50 # 判断目标位置是否有其他动物 target_animal = None for animal in animals: if animal != selected_animal and animal.rect.x == target_x and animal.rect.y == target_y: target_animal = animal break # 如果目标位置有其他动物,进行战斗 if target_animal: if selected_animal.rank == target_animal.rank: animals.remove(selected_animal) animals.remove(target_animal) all_sprites.remove(selected_animal) all_sprites.remove(target_animal) elif selected_animal.rank == 1 and target_animal.rank == 8: animals.remove(selected_animal) all_sprites.remove(selected_animal) elif selected_animal.rank == 8 and target_animal.rank == 1: animals.remove(target_animal) all_sprites.remove(target_animal) elif selected_animal.rank > target_animal.rank: animals.remove(target_animal) all_sprites.remove(target_animal) selected_animal.move(target_x, target_y) selected_animal = None turn = 1 - turn else: animals.remove(selected_animal) all_sprites.remove(selected_animal) selected_animal = None turn = 1 - turn else: selected_animal.move(target_x, target_y) selected_animal = None turn = 1 - turn # 绘制背景和动物 win.fill(white) win.blit(board, (50, 50)) all_sprites.draw(win) # 更新屏幕 pygame.display.update() ``` 这个斗兽棋小游戏中,玩家1为红方,玩家2为黑方,红方先手。玩家可以通过点击自己的动物,然后再点击棋盘上的空位将其移动。如果目标位置有其他动物,则进行战斗,战斗规则如下: - 鼠(1)可以吃象(8),象(8)不能吃鼠(1) - 动物可以吃比自己低阶的动物,但不能吃比自己高阶的动物 - 如果两只动物的阶数相同,则同归于尽,都从棋盘上消失 注意:以上代码仅为示例代码,还有很多优化和完善的地方,比如添加更多的动物种类、让动物移动时有动画效果等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值