用python代码写猴子推箱子摘香蕉动画
时间: 2024-02-18 12:32:44 浏览: 68
当然可以!以下是一个简单的猴子推箱子摘香蕉动画的示例代码:
```
import pygame
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
BROWN = (139,69,19)
# 设置屏幕大小,网格大小和边缘大小
SCREEN_WIDTH = 540
SCREEN_HEIGHT = 540
MARGIN = 10
CELL_WIDTH = (SCREEN_WIDTH - 3 * MARGIN) // 10
CELL_HEIGHT = (SCREEN_HEIGHT - 3 * MARGIN) // 10
# 初始化pygame并设置屏幕
pygame.init()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
# 加载图像资源
monkey_left = pygame.image.load('monkey_left.png')
monkey_right = pygame.image.load('monkey_right.png')
box = pygame.image.load('box.png')
banana = pygame.image.load('banana.png')
# 设置标题和图标
pygame.display.set_caption("Monkey Push Box Game")
icon = pygame.image.load('icon.png')
pygame.display.set_icon(icon)
# 网格和物品的坐标位置
grid = []
boxes = []
bananas = []
monkey_coords = [1, 1]
# 初始化网格和物品的位置
def init():
global grid, boxes, bananas, monkey_coords
grid = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 0, 0, 1, 1, 1, 0, 0, 1],
[1, 0, 0, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 0, 1, 1, 1, 2, 1],
[1, 0, 1, 0, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
boxes = [[3, 3], [3, 4], [3, 5], [4, 3], [4, 4], [4, 5]]
bananas = [[10, 8]]
monkey_coords = [5, 4]
# 画网格和物品
def draw():
global boxes, bananas, monkey_coords
screen.fill(WHITE)
for row in range(15):
for col in range(10):
if grid[row][col] == 1:
pygame.draw.rect(screen, BROWN, [(MARGIN + CELL_WIDTH) * col + MARGIN, (MARGIN + CELL_HEIGHT) * row + MARGIN, CELL_WIDTH, CELL_HEIGHT])
elif grid[row][col] == 2:
pygame.draw.rect(screen, GREEN, [(MARGIN + CELL_WIDTH) * col + MARGIN, (MARGIN + CELL_HEIGHT) * row + MARGIN, CELL_WIDTH, CELL_HEIGHT])
for box in boxes:
screen.blit(box_image, [(MARGIN + CELL_WIDTH) * box[1] + MARGIN, (MARGIN + CELL_HEIGHT) * box[0] + MARGIN])
for banana in bananas:
screen.blit(banana_image, [(MARGIN + CELL_WIDTH) * banana[1] + MARGIN, (MARGIN + CELL_HEIGHT) * banana[0] + MARGIN])
if monkey_coords[0] == monkey_old_coords[0]:
if monkey_coords[1] < monkey_old_coords[1]:
screen.blit(monkey_left, [(MARGIN + CELL_WIDTH) * monkey_coords[1] + MARGIN, (MARGIN + CELL_HEIGHT) * monkey_coords[0] + MARGIN])
else:
screen.blit(monkey_right, [(MARGIN + CELL_WIDTH) * monkey_coords[1] + MARGIN, (MARGIN + CELL_HEIGHT) * monkey_coords[0] + MARGIN])
elif monkey_coords[0] < monkey_old_coords[0]:
screen.blit(monkey_left, [(MARGIN + CELL_WIDTH) * monkey_coords[1] + MARGIN, (MARGIN + CELL_HEIGHT) * monkey_coords[0] + MARGIN])
else:
screen.blit(monkey_right, [(MARGIN + CELL_WIDTH) * monkey_coords[1] + MARGIN, (MARGIN + CELL_HEIGHT) * monkey_coords[0] + MARGIN])
pygame.display.flip()
# 猴子移动逻辑
def move_monkey(move_x, move_y):
global monkey_coords, boxes, bananas
new_x = monkey_coords[0] + move_x
new_y = monkey_coords[1] + move_y
if new_x < 0 or new_x > 14 or new_y < 0 or new_y > 9:
# 超出边界
return
if grid[new_x][new_y] == 1:
# 障碍物
return
for box in boxes:
if new_x == box[0] and new_y == box[1]:
new_box_x = box[0] + move_x
new_box_y = box[1] + move_y
if new_box_x < 0 or new_box_x > 14 or new_box_y < 0 or new_box_y > 9:
# 超出边界
return
if grid[new_box_x][new_box_y] == 1:
# 障碍物
return
for banana in bananas:
if new_box_x == banana[0] and new_box_y == banana[1]:
# 拿到香蕉了
bananas.remove(banana)
boxes.remove(box)
box.append(new_box_x)
box.append(new_box_y)
return
for box2 in boxes:
if new_box_x == box2[0] and new_box_y == box2[1]:
# 已经有箱子了
return
box[0] = new_box_x
box[1] = new_box_y
monkey_old_coords[0] = monkey_coords[0]
monkey_old_coords[1] = monkey_coords[1]
monkey_coords[0] = new_x
monkey_coords[1] = new_y
return
for banana in bananas:
if new_x == banana[0] and new_y == banana[1]:
# 拿到香蕉了
bananas.remove(banana)
return
monkey_old_coords[0] = monkey_coords[0]
monkey_old_coords[1] = monkey_coords[1]
monkey_coords[0] = new_x
monkey_coords[1] = new_y
# 主循环
box_image = pygame.transform.scale(box, (CELL_WIDTH, CELL_HEIGHT))
banana_image = pygame.transform.scale(banana, (CELL_WIDTH, CELL_HEIGHT))
init()
monkey_old_coords = monkey_coords
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
move_monkey(-1, 0)
elif event.key == pygame.K_DOWN:
move_monkey(1, 0)
elif event.key == pygame.K_LEFT:
move_monkey(0, -1)
elif event.key == pygame.K_RIGHT:
move_monkey(0, 1)
draw()
pygame.quit()
```
这是一个 15 行 x 10 列的网格,在网格上放置了猴子,香蕉和一些箱子,并使用 pygame 库将其渲染成一个游戏。你需要将上面的代码存为一个 .py 文件,然后在同一目录下放置图像资源 (monkey_left.png、monkey_right.png、box.png、banana.png、icon.png),该程序使用相对路径进行文件查找。请先确保安装了pygame库,然后在命令行中输入以下命令运行程序:
```
python monkey_push_box.py
```
这个程序使用箭头键来控制猴子的移动,你可以自己尝试玩玩看。
阅读全文