python-pygame实现键盘控制小球移动

import pygame,sys,time

pygame.init()
img=pygame.display.set_mode((512,512))#设置窗口
pygame.display.set_caption('image')#设置窗口名字
img.fill((111,111,111)
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,下面是实现自动随机生成一个迷宫,并能用键盘控制小球完成走迷宫游戏的Python代码。这里我们同样使用pygame和随机数模块random。 ```python import pygame import random # 定义迷宫的大小和单元格的大小 MAZE_WIDTH = 20 MAZE_HEIGHT = 15 CELL_SIZE = 30 # 定义迷宫的二维数组 maze = [[0 for y in range(MAZE_HEIGHT)] for x in range(MAZE_WIDTH)] # 定义迷宫的起点和终点 start_pos = (0, 0) end_pos = (MAZE_WIDTH - 1, MAZE_HEIGHT - 1) # 随机生成迷宫 def generate_maze(): # 初始化迷宫的基本结构 for x in range(MAZE_WIDTH): for y in range(MAZE_HEIGHT): if x == 0 or x == MAZE_WIDTH - 1 or y == 0 or y == MAZE_HEIGHT - 1: maze[x][y] = 0 else: maze[x][y] = 1 # 随机打通迷宫的墙壁 for x in range(1, MAZE_WIDTH - 1): for y in range(1, MAZE_HEIGHT - 1): if random.randint(0, 2) == 0: maze[x][y] = 0 # 设置起点和终点 maze[start_pos[0]][start_pos[1]] = 1 maze[end_pos[0]][end_pos[1]] = 1 # 绘制迷宫 def draw_maze(screen): for x in range(MAZE_WIDTH): for y in range(MAZE_HEIGHT): color = (255, 255, 255) if maze[x][y] == 0: color = (0, 0, 0) pygame.draw.rect(screen, color, (x * CELL_SIZE, y * CELL_SIZE, CELL_SIZE, CELL_SIZE)) # 绘制小球 def draw_ball(screen, pos): x, y = pos pygame.draw.circle(screen, (255, 0, 0), (x * CELL_SIZE + CELL_SIZE // 2, y * CELL_SIZE + CELL_SIZE // 2), CELL_SIZE // 2) # 判断小球是否能够移动 def can_move(pos, direction): x, y = pos if direction == pygame.K_UP: if y > 0 and maze[x][y - 1] == 1: return True elif direction == pygame.K_DOWN: if y < MAZE_HEIGHT - 1 and maze[x][y + 1] == 1: return True elif direction == pygame.K_LEFT: if x > 0 and maze[x - 1][y] == 1: return True elif direction == pygame.K_RIGHT: if x < MAZE_WIDTH - 1 and maze[x + 1][y] == 1: return True return False # 判断小球是否到达终点 def is_end(pos): return pos == end_pos # 主函数 def main(): pygame.init() screen = pygame.display.set_mode((MAZE_WIDTH * CELL_SIZE, MAZE_HEIGHT * CELL_SIZE)) pygame.display.set_caption("Maze Game") generate_maze() ball_pos = start_pos while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() return elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP and can_move(ball_pos, pygame.K_UP): ball_pos = (ball_pos[0], ball_pos[1] - 1) elif event.key == pygame.K_DOWN and can_move(ball_pos, pygame.K_DOWN): ball_pos = (ball_pos[0], ball_pos[1] + 1) elif event.key == pygame.K_LEFT and can_move(ball_pos, pygame.K_LEFT): ball_pos = (ball_pos[0] - 1, ball_pos[1]) elif event.key == pygame.K_RIGHT and can_move(ball_pos, pygame.K_RIGHT): ball_pos = (ball_pos[0] + 1, ball_pos[1]) screen.fill((255, 255, 255)) draw_maze(screen) draw_ball(screen, ball_pos) pygame.display.flip() if is_end(ball_pos): font = pygame.font.Font(None, 36) text = font.render("You Win!", True, (0, 255, 0)) text_rect = text.get_rect(centerx=screen.get_width() // 2, centery=screen.get_height() // 2) screen.blit(text, text_rect) pygame.display.flip() pygame.time.delay(2000) generate_maze() ball_pos = start_pos # 启动主函数 if __name__ == "__main__": main() ``` 运行代码后,你可以使用方向键来控制小球移动,完成走迷宫的游戏。希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值