• Advertisement
Sign in to follow this  

Linking Images on a Grid

This topic is 2146 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

First off, please excuse me if my jargon is a little rough. I'm still learning the ropes to programming and haven't gotten completely used to accurately explaining things to professionals. That said, I'll do my best to describe the problem I'm currently facing.

To start with I'm using Python and pygame to program a Dr. Mario clone. A lot of what I read reccommended on starting with a Tetris like game as a beginner project, but I thought I'd try out Dr. Mario instead. My current problem is figuring out the best way to transition from one active piece to another while drawing the previous active piece to the game board.

The issue in particular is how I can load a specific graphic for linked pieces and non-linked pieces and how to distinguish them within an array. Currently, the array keeps track of the element, if any, that resides in each square on the grid. While I'm fairly confident I could get the program to load the desired element in each square, I have no clue how to load a single graphic that takes up two spots on the grid while simultaneously keeping them linked after they are no longer active.

Any pointers, tips, or resources (ideally aimed at beginners) would be appreciated. I've done preliminary research but sadly the only sources I could find were not well documented so I couldn't understand them (and I don't want to just copy/paste because then I won't learn any thing.) Below is the pseudo code for those that have never played Dr. Mario so that you can understand what I am trying to do and below that is the full code I have so far. Apologies in advance for not shortening it because I'm afraid I might leave out something vital. Thank you for taking the time to help me out!

Pseudo Code:
[spoiler]1. The player drops Mana from the top of the screen to elemental energy "Blocks".
- Mana comes in three forms, Light, Wind, and Earth.
- Mana comes in groups of two and may be combinations of any of the above.
- Mana may be rotated 90 degrees at a time Clockwise or Counter Clockwise.
- Players will see the next mana group to fall while controlling the active group.
- When a Mana Pair collides with a Block, other Mana, or the bottom of the board it becomes a part of the game board.
- After the board is done settling, the next Mana Pair is introduced and gameplay continues.

2. All of the action happens in an 8 x 16 Vertical Rectangle grid.

3. Blocks will be placed at random with in the bottom 12 rows of the Box.
- Number of blocks starts at 4 for level 1 difficulty, increases by 4 for each
level afterwards.
- Blocks are destroyed when four of the same color line up.

4. The user 'wins' when all of the blocks in the current level are destroyed and moves onto
the next difficulty level.
- There will be 20 stages in all making a max of 80 blocks.

5. If a pair of Mana collides with anything immediately upon being introduced, the user loses.

6. The user will be able to manually configure the rate of descent before the game begins
between SLOW, MID, and FAST.

---------------

Mana:
- Mana movement is controlled by the arrows, rotated with ctrl and alt.
- Has two elemental properties at random that may be the same.
- Four of a same color in a row clears all objects of that color.
- If a portion of the Group is destroyed, the rest will become single instances of
the coresponding mana and fall straight down.

Blocks:
- Blocks do not move and remain stationary.
- Blocks are cleared along with the Mana.[/spoiler]

Source Code:
[code]import pygame, random
pygame.init()

NUM_COLUMNS = 8
NUM_ROWS = 16

screen = pygame.display.set_mode((640, 480))
clock = pygame.time.Clock()

class Element(pygame.sprite.Sprite):
def __init__(self, element):
self.element = element
pygame.sprite.Sprite.__init__(self)
self.imageMaster = pygame.image.load("WMana.xcf")
self.generateElements()
self.image = self.imageMaster
self.rect = self.image.get_rect()
self.image.set_colorkey((255, 255, 255))
self.x = 0
self.y = 0

def generateElements(self):
#1 = W, 2 = G, 3 = Y
if self.element == 0:
self.element = random.randrange(1, 4)
if self.element == 1:
self.imageMaster = pygame.image.load("WMana.xcf")
elif self.element == 2:
self.imageMaster = pygame.image.load("GMana.xcf")
elif self.element == 3:
self.imageMaster = pygame.image.load("YMana.xcf")
self.imageMaster = self.imageMaster.convert()

def settle(self, occupiedGrid):
occupiedGrid[ xToGrid(self.x) ][ yToGrid(self.y) ] = self.element

def unSettle(self, occupiedGrid):
occupiedGrid[ xToGrid(self.x) ][ yToGrid(self.y) ] = None

def xToGrid(pixel_coordinate):
return ((pixel_coordinate - 10) - 240) / 20

def yToGrid(pixel_coordinate):
return ((pixel_coordinate - 10) - 100) / 20

class Mana(pygame.sprite.Sprite):
def __init__(self, pelement, selement):
self.pelement = pelement
self.selement = selement
pygame.sprite.Sprite.__init__(self)
self.imageMaster = pygame.image.load("testWW.xcf")
self.elements()
self.image = self.imageMaster
self.rect = self.image.get_rect()
self.image.set_colorkey((255, 255, 255))
self.x = 300
self.y = 100
self.dir = 0
self.wait = 0
self.drop = 6

def update(self):
self.checkKeys()
self.rotate()
self.rect.bottomleft = (self.x, self.y)
self.wait += 1
if self.wait >= self.drop:
self.wait = 0
self.y += 20
if self.rect.bottom >= 400:
self.y = 400
self.checkPos()

def elements(self):
#1 = W, 2 = G, 3 = Y
if self.pelement.element == 1:
if self.selement.element == 1:
self.imageMaster = pygame.image.load("testWW.xcf")
elif self.selement.element == 2:
self.imageMaster = pygame.image.load("testWG.xcf")
elif self.selement.element == 3:
self.imageMaster = pygame.image.load("testWY.xcf")
elif self.pelement.element == 2:
if self.selement.element == 1:
self.imageMaster = pygame.image.load("testGW.xcf")
elif self.selement.element == 2:
self.imageMaster = pygame.image.load("testGG.xcf")
elif self.selement.element == 3:
self.imageMaster = pygame.image.load("testGY.xcf")
elif self.pelement.element == 3:
if self.selement.element == 1:
self.imageMaster = pygame.image.load("testYW.xcf")
elif self.selement.element == 2:
self.imageMaster = pygame.image.load("testYG.xcf")
elif self.selement.element == 3:
self.imageMaster = pygame.image.load("testYY.xcf")
self.imageMaster = self.imageMaster.convert()

def checkKeys(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.x -= 20
if self.rect.left <= 240:
self.x = 240
if keys[pygame.K_RIGHT]:
if self.rect.right <= 380:
self.x += 20
if keys[pygame.K_DOWN]:
self.y += 20
self.wait = 0
if self.rect.bottom >= 400:
self.y = 400

def rotate(self):
oldBL = self.rect.bottomleft
self.image = pygame.transform.rotate(self.imageMaster, self.dir)
self.rect = self.image.get_rect()
self.rect.bottomleft = oldBL

def checkPos(self):
if self.dir == 0 or self.dir == 360:
self.pelement.x = (self.x + 10)
self.pelement.y = (self.y + 10)
self.selement.x = (self.x + 30)
self.selement.y = (self.y + 10)
elif self.dir == 90:
self.pelement.x = (self.x + 10)
self.pelement.y = (self.y + 10)
self.selement.x = (self.x + 10)
self.selement.y = (self.y - 10)
elif self.dir == 180:
self.pelement.x = (self.x + 30)
self.pelement.y = (self.y + 10)
self.selement.x = (self.x + 10)
self.selement.y = (self.y + 10)
elif self.dir == 270:
self.pelement.x = (self.x + 10)
self.pelement.y = (self.y - 10)
self.selement.x = (self.x + 10)
self.selement.y = (self.y + 10)

def settle(self, occupiedGrid):
self.pelement.settle(occupiedGrid)
self.selement.settle(occupiedGrid)

class Backdrop(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("Border.xcf")
self.image = self.image.convert()
self.rect = self.image.get_rect()

def update(self):
self.rect.center = (320, 240)

class Game():
def __init__(self):
pygame.display.set_caption("Mana Clash!")

self.background = pygame.Surface(screen.get_size())
self.background.fill((100, 100, 100))
screen.blit(self.background, (0, 0))

self.pelement = Element(0)
self.selement = Element(0)
self.mana = Mana(self.pelement, self.selement)
self.backdrop = Backdrop()

self.allSprites = pygame.sprite.OrderedUpdates(self.backdrop, self.mana)

self.occupiedGrid = []
for x in range(0, NUM_COLUMNS):
self.occupiedGrid.append([])
for y in range(0, NUM_ROWS):
self.occupiedGrid[x].append(0)

def debug(self):
for x in range(0, NUM_COLUMNS):
for y in range(0, NUM_ROWS):
print self.occupiedGrid
break

def mainLoop(self):
keepGoing = True
while keepGoing:
clock.tick(5)
pygame.mouse.set_visible(False)
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RCTRL:
self.mana.dir += 90
if self.mana.dir > 360:
self.mana.dir = 90
elif event.key == pygame.K_RALT:
self.mana.dir -= 90
if self.mana.dir < 0:
self.mana.dir = 270

if event.key == pygame.K_d:
self.debug()
if event.key == pygame.K_s:
self.mana.settle(self.occupiedGrid)

self.allSprites.clear (screen, self.background)
self.allSprites.update()
self.allSprites.draw(screen)

pygame.display.flip()

pygame.mouse.set_visible(True)

def main():
game = Game()
game.mainLoop()

if __name__ == "__main__":
main()[/code]

Share this post


Link to post
Share on other sites
Advertisement
Sorry to bump this, but I'm still trying to work through this issue.

Do you think that perhaps if I passed the entire Object onto the grid rather than the number that corresponds to the particular element, that might work? That way I could use the Direction of the sprite to tell if two objects next to each other are linked. Of course, doing that means I would have to seperate the sprites into two halves to properly load an image onto the grid. Unfortuantely, I don't know how I would continue to register the two seperate sprites as one entity other than perhaps making it a sprite group in which case I would probably have to scrap everything I have and redesign the program for a fourth time from the ground up and I'm hesitant to do that when I'm not even sure that my idea will work properly.

I know programming isn't supposed to be an easy process, but I can't help but feel that I'm doing things terribly innefficiently and taking unnecessary steps to accomplish something very simple. More so, I'm worried that there isn't a lot of information out there to bridge the gap from beginner to intermediate when it comes to game programming in Python. I can find tons of beginner examples that I know and understand but don't come close to performing the tasks I need, but every thing above that seems to be using advanced techniques that are performing tasks far above my current skill level. I'm hesitant to ask, but perhaps should I try a different programming language since I seem to have gotten stuck in the mud with Python? C# seems to have much better community support and documentation and is widely used. I chose Python because I liked the syntax and the logic was very easy to follow compared to my previous forays into programming. If any one that has started with Python as their first step into serious programming can give me a little insight on how they managed to overcome the hurdle in making the jump from beginner to intermediate I would appreciate it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement