Pygame开发求助:点击菜单方块后无法在工作区点击位置生成方块
Fixing Block Placement in Your ArchiConnect World Game
It sounds like your current code handles the "pick up" part (making the block follow the mouse when selecting from the menu) but misses the logic to actually place the block when you click in the workspace area. Let’s fix that by adding state tracking for the dragged block and implementing placement logic.
Key Issues to Address
- Track whether a block is currently being dragged
- Detect clicks in the workspace area to drop the dragged block
- Create a new block instance at the click position once dropped
Corrected Code Example
Here’s how to modify your code to add these features (adjust coordinates and block details to match your original setup):
import pygame from pygame.locals import * pygame.init() # Window setup window = pygame.display.set_mode([1000, 800]) pygame.display.set_caption("ArchiConnect World") # Define workspace and menu areas (adjust as needed) workspace_area = pygame.Rect(0, 0, 1000, 700) # Top 700px for building menu_area = pygame.Rect(0, 700, 1000, 100) # Bottom 100px for block selection # Example block assets (replace with your actual block images) block_types = { "grass": pygame.Surface((50, 50)), "stone": pygame.Surface((50, 50)) } block_types["grass"].fill((34, 139, 34)) # Green grass block block_types["stone"].fill((128, 128, 128)) # Gray stone block # Menu block positions menu_blocks = [ {"type": "grass", "rect": pygame.Rect(50, 725, 50, 50)}, {"type": "stone", "rect": pygame.Rect(150, 725, 50, 50)} ] # State variables for dragging dragging_block = None # Tracks which block is being dragged dragging_offset = (-25, -25) # Centers block on mouse cursor # List to store placed blocks in the workspace placed_blocks = [] running = True while running: window.fill((255, 255, 255)) # Clear screen each frame # Event handling for event in pygame.event.get(): if event.type == QUIT: running = False # Handle mouse clicks if event.type == MOUSEBUTTONDOWN: mouse_pos = pygame.mouse.get_pos() # Check if clicking in the menu to pick up a block if menu_area.collidepoint(mouse_pos): for block in menu_blocks: if block["rect"].collidepoint(mouse_pos): dragging_block = block["type"] break # Check if clicking in workspace to place a block elif workspace_area.collidepoint(mouse_pos) and dragging_block is not None: # Add new block to workspace at click position (centered) placed_blocks.append({ "type": dragging_block, "rect": pygame.Rect( mouse_pos[0] + dragging_offset[0], mouse_pos[1] + dragging_offset[1], 50, 50 ) }) dragging_block = None # Stop dragging # Update dragged block position on mouse movement if event.type == MOUSEMOTION and dragging_block is not None: dragged_rect = pygame.Rect( event.pos[0] + dragging_offset[0], event.pos[1] + dragging_offset[1], 50, 50 ) # Draw placed workspace blocks for block in placed_blocks: window.blit(block_types[block["type"]], block["rect"]) # Draw menu area and blocks pygame.draw.rect(window, (200, 200, 200), menu_area) for block in menu_blocks: window.blit(block_types[block["type"]], block["rect"]) # Draw dragged block if active if dragging_block is not None: window.blit(block_types[dragging_block], dragged_rect) pygame.display.flip() pygame.quit()
Key Changes Explained
- State Tracking: Added
dragging_blockto track which block is being moved, so we know when to handle placement. - Menu Click Logic: When you click a block in the menu, we set the dragging state and prepare the block to follow the mouse.
- Workspace Placement: When you click in the workspace while dragging, we add a new block to the
placed_blockslist at the click position and end the dragging state. - Mouse Movement Sync: Updates the dragged block’s position every time the mouse moves, keeping it centered on the cursor.
This setup lets you pick blocks from the menu, drag them around, and place them in the workspace with a single click. Tweak the coordinates, block sizes, and colors to match your original project!
内容的提问来源于stack exchange,提问作者Maria
相关产品推荐
相关产品推荐

