用python编写一个斗兽棋的小游戏
时间: 2023-09-07 09:13:42 浏览: 47
好的,斗兽棋是一种中国传统棋类游戏,以下是一个简单的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)
- 动物可以吃比自己低阶的动物,但不能吃比自己高阶的动物
- 如果两只动物的阶数相同,则同归于尽,都从棋盘上消失
注意:以上代码仅为示例代码,还有很多优化和完善的地方,比如添加更多的动物种类、让动物移动时有动画效果等。