机器人
积分排行榜
Scratch
积分排行榜
Python
在线答题
积分排行榜
活跃排行榜
打字练习
Microbit
C++
在线答题
积分排行榜
活跃排行榜
网盘
积分
未登录
未登录
登录学习
作者:
贾明雨
更新时间:
2025-03-22 11:58
浏览:
39次
点赞:
0次
热度:
20
import pygame import random # 初始化Pygame pygame.init() # 游戏常量 SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 BLOCK_SIZE = 40 PLAYER_SIZE = 30 GRAVITY = 0.8 JUMP_FORCE = -15 # 颜色定义 WHITE = (255, 255, 255) BLUE = (0, 120, 255) GREEN = (34, 139, 34) BROWN = (139, 69, 19) GRAY = (128, 128, 128) BLACK = (0, 0, 0) # 初始化屏幕 screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption("PyCraft") # 方块类型 blocks = { "air": {"color": WHITE, "solid": False}, "grass": {"color": GREEN, "solid": True}, "dirt": {"color": BROWN, "solid": True}, "stone": {"color": GRAY, "solid": True} } class Player: def __init__(self): self.x = SCREEN_WIDTH // 2 self.y = SCREEN_HEIGHT // 2 self.velocity = 0 self.on_ground = False def move(self, dx, dy): self.x += dx self.y += dy def jump(self): if self.on_ground: self.velocity = JUMP_FORCE self.on_ground = False class World: def __init__(self, width, height): self.width = width self.height = height self.grid = [[None for _ in range(width)] for _ in range(height)] self.generate_world() def generate_world(self): # 简单地形生成 surface_level = self.height // 2 for x in range(self.width): height = surface_level + random.randint(-3, 3) for y in range(self.height): if y > height + 2: self.grid[y][x] = "stone" elif y > height: self.grid[y][x] = "dirt" elif y == height: self.grid[y][x] = "grass" else: self.grid[y][x] = "air" def draw(self): for y in range(self.height): for x in range(self.width): block = self.grid[y][x] if block != "air": rect = pygame.Rect(x*BLOCK_SIZE, y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) pygame.draw.rect(screen, blocks[block]["color"], rect) pygame.draw.rect(screen, BLACK, rect, 1) # 方块边框 def main(): clock = pygame.time.Clock() player = Player() world = World(SCREEN_WIDTH//BLOCK_SIZE, SCREEN_HEIGHT//BLOCK_SIZE) running = True while running: dt = clock.tick(60) / 1000 # 事件处理 for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 鼠标事件 if event.type == pygame.MOUSEBUTTONDOWN: x, y = pygame.mouse.get_pos() grid_x = x // BLOCK_SIZE grid_y = y // BLOCK_SIZE if event.button == 1: # 左键破坏 if world.grid[grid_y][grid_x] != "air": world.grid[grid_y][grid_x] = "air" elif event.button == 3: # 右键放置 if world.grid[grid_y][grid_x] == "air": world.grid[grid_y][grid_x] = "grass" # 键盘输入 keys = pygame.key.get_pressed() move_speed = 5 dx = 0 if keys[pygame.K_a] or keys[pygame.K_LEFT]: dx -= move_speed if keys[pygame.K_d] or keys[pygame.K_RIGHT]: dx += move_speed if keys[pygame.K_SPACE]: player.jump() # 物理更新 player.velocity += GRAVITY player.y += player.velocity # 碰撞检测 player_rect = pygame.Rect(player.x, player.y, PLAYER_SIZE, PLAYER_SIZE) player.on_ground = False # 简单碰撞检测 grid_x = int(player.x // BLOCK_SIZE) grid_y = int(player.y // BLOCK_SIZE) for y in range(max(0, grid_y-2), min(world.height, grid_y+3)): for x in range(max(0, grid_x-2), min(world.width, grid_x+3)): if blocks[world.grid[y][x]]["solid"]: block_rect = pygame.Rect(x*BLOCK_SIZE, y*BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) if player_rect.colliderect(block_rect): # 简单垂直碰撞处理 if player.velocity > 0: player.y = block_rect.top - PLAYER_SIZE player.velocity = 0 player.on_ground = True elif player.velocity < 0: player.y = block_rect.bottom player.velocity = 0 # 水平移动 player.x += dx player.x = max(0, min(SCREEN_WIDTH - PLAYER_SIZE, player.x)) # 绘制 screen.fill(WHITE) world.draw() pygame.draw.rect(screen, BLUE, (player.x, player.y, PLAYER_SIZE, PLAYER_SIZE)) pygame.display.flip() pygame.quit() if __name__ == "__main__": main()
点赞成功
分享作品
×