Python Forum
[PyGame] 2D BoardGame Pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] 2D BoardGame Pygame
#1
Hi all,

I'm trying to develop a 2D board game using PyGame. I'm new to using pygame though and there doesn't seem to be any easy tools for working with grids/2D boards.

Here is a VERY ROUGH prototype of what I want the board to look like.

[Image: xAKwPMq]

As you can see there are 4 main parts to the board. The main grid (black grid), the boxes on the bottom (green boxes) the special button (red box), and the status/score bar in the top left (blue boxes).

The green boxes are going to be the players hand where I will upload my own PGN's in the boxes. There will be 7 different images i.e. one for each cell. Would a spritesheet be best for this?
The main black grid I want to be infinitely big i.e. the player can drag it around. This means that the rest of the elements would have to stay in place i.e. be on a separate layer ontop of the grid.
Also unlike in the prototype all the cells will be the same size (so I guess having a global at the top would make sense). I am not sure what size I should make the window or the cells...but after reading this https://stackoverflow.com/questions/2095...resolution I think just keeping the window resolution at 800x600 and the cell size at 40x40 is a good starting point

The main game mechanic is a drag and drop function where the player drags the images from his hand onto the grid.

The first problem I'm having is how to generate the grid so it's a reactive surface i.e. not just drawn on lines but cells which can be dragged and dropped into.

To generate the grid I'm guessing I would have to use a 2D array but would each cell have to be its own surface? (this seems obviously wrong to me but I really don't know what else to do!) Is there a way to have it as one surface but split it up so I can reference the coordinates of each cell?

For the dragging and dropping I guess there would have to be a function which detects if the image surface coordinates are close to a particular cell and then snap it into place if it is.

Then there is the problem of how to create the grid as a background layer. I've read here https://stackoverflow.com/questions/3363...een-layers that one way to do this is to have different surfaces i.e. one surface for the background layer and one for the rest of the elements.

Then there is the problem of how to make the grid 'infinitely' big or at least appear infinitely big. I'm guessing this is some magic to do with blit and update but again some guidance with this would be helpful.

This is the very basic code I've copied from a tutorial which literally just creates a window and blits an image onto the centre of the screen. Unfortunately most tutorials don't cover the problems I'm having and I can only gather so much from scavenging on StackOverflow!

import pygame as pg
  
pg.init()
  
class Player:
    def __init__(self, screen_rect):
        self.image = pg.image.load('number1.jpg').convert() #create player.image attribute
        self.image.set_colorkey((255,0,255))                  
        self.rect = self.image.get_rect(center=screen_rect.center) #create player.rect attribute from the image and position it to screen center
          
    def draw(self, surf):
        surf.blit(self.image, self.rect)
  
screen = pg.display.set_mode((800,600))
screen_rect = screen.get_rect()
player = Player(screen_rect) #create player object, run __init__ (dunder init method)
done = False
while not done:
    for event in pg.event.get(): 
        if event.type == pg.QUIT:
            done = True
    player.draw(screen)
    pg.display.update()
Any help will be much appreciated Big Grin
Reply


Messages In This Thread
2D BoardGame Pygame - by Josh_Python890 - Aug-13-2019, 11:59 AM
RE: 2D BoardGame Pygame - by metulburr - Aug-13-2019, 12:32 PM
RE: 2D BoardGame Pygame - by Windspar - Aug-13-2019, 02:18 PM
RE: 2D BoardGame Pygame - by Windspar - Aug-13-2019, 06:25 PM
RE: 2D BoardGame Pygame - by SheeppOSU - Aug-14-2019, 02:32 AM
RE: 2D BoardGame Pygame - by Windspar - Aug-15-2019, 09:02 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020