Monday, October 28, 2013

Bejeweled Style Matches

I am working on a school project to create a basic, educational game. I started with the amazing code written by cactusbin and revised by Gareth Ress found for the basics of a bejeweled style game.



The game objective is to match letters of DNA to make correct pairs while including two blank squares with the letter to clear it. There are three different colors (red, blue, yellow) and five different letters (a, c, g, t, u).




Right now, thanks again to Gareth, the game will clear matches of four based on color regardless of letter. What I have been banging my head against the keyboard trying to figure out is how to change the code so the ONLY way matches are cleared is if they include two letters that are a part of a valid pair. I have the code set up so at the first level, 20 random letters spawn at the start of the game and to win the level, a set number of pairs need to be cleared. Unlike Bejeweled, squares can be swapped with the square next to it regardless of if it makes a valid match.



For example, if a user moves four blank squares of the same color next to each other, they will not clear. However, if they move two (or more) blank red squares next to a red A square and a red T square, that will be a valid match and will clear. If they get a red A, red T, and red U, they will clear but be an invalid "pair" and give the user a strike. If they get 5 blank reds and a red T, they will not clear, but if they manage to get a red A somewhere in the line of 5 blanks and T, the whole set will clear.



I hope that makes it clear what we are trying to do. I have made some changes trying to get it to work but it either ends up clearing ALL blank squares regardless of them being grouped next to each other or I made it so only blank squares clear and if a letter is part of the four, it will not clear. Running out of time to get this Alpha build finished with working gameplay, so any help you can provide would be awesome!



I had the suggestion of looping through the "match" variable and see if it fits requirements for a score, but I am not sure that would work. There are a handful of different correct ways to score (BlueA, BlueT, 2 blue blanks) (YellowA, YellowT, 2 yellow blanks) (RedA, RedT, 2 red blanks) are just three of the different possible valid "matches". Any ideas on how to do this?



The full code as it stands right now (the vertical cursor switching is not working so don't press "F" or you will get an error...another problem I am trying to fix).



import pygame, random, time, sys from pygame.locals import * import itertools import os WHITE = (255, 255, 255) BLACK = (0, 0, 0) SHAPEWIDTH = 64 # Width of each shape (pixels). SHAPEHEIGHT = 64 # Height of each shape (pixels). PUZZLECOLUMNS = 10 # Number of columns on the board. PUZZLEROWS = 11 # Number of rows on the board. MARGIN = 118 # Margin around the board (pixels). WINDOWWIDTH = PUZZLECOLUMNS * SHAPEWIDTH + 2 * MARGIN + 485 WINDOWHEIGHT = PUZZLEROWS * SHAPEHEIGHT + 2 * MARGIN - 150 FONTSIZE = 60 TEXTOFFSET = MARGIN + 950 # Map from number of matches to points scored. MINIMUMMATCH = 4 EXTRALENGTHPOINTS = .1 RANDOMPOINTS = .3 DELAYPENALTYSECONDS = 1 DELAYPENALTYPOINTS = 0 FPS = 30 EXPLOSIONSPEED = 15 # In frames per second. SPINSPEED = 15 REFILLSPEED = 10 # In cells per second. VERTICAL = False class Cell(object): """ A cell on the board, with properties: `image` -- a `Surface` object containing the sprite to draw here. `offset` -- vertical offset in pixels for drawing this cell. """ def init(self, image): self.offset = 0.0 self.image = image def tick(self, dt): self.offset = max(0.0, self.offset - dt * REFILLSPEED) class Board(object): """ A rectangular board of cells, with properties: `w` -- width in cells. `h` -- height in cells. `size` -- total number of cells. `board` -- list of cells. `matches` -- list of matches, each being a list of exploding cells. `refill` -- list of cells that are moving up to refill the board. `score` -- score due to chain reactions. """ def init(self, width, height): self.explosion = [pygame.image.load('images/explosion{}.png'.format(i)) for i in range(1, 7)] self.spin = [pygame.image.load('images/powerframe{}.png'.format(i)) for i in range (1, 12)] self.imagecolor = {} self.shapes = [] self.rareshapes = [] colors = 'red blue yellow' letters = 'acgtu' for c in colors.split(): im = pygame.image.load('images/{}.png'.format(c)) self.shapes.append(im) self.imagecolor[im] = c for l in letters: im = pygame.image.load('rareimages/{}{}.png'.format(c, l)) self.rareshapes.append(im) self.imagecolor[im] = l self.background = pygame.image.load("images/bg.png") self.blank = pygame.image.load("images/blank.png") self.x = pygame.image.load("images/x.png") self.w = width self.h = height self.size = width * height self.board = [Cell(self.blank) for in range(self.size)] self.matches = [] self.refill = [] self.score = 0.0 self.spintime = 15 def randomize(self): """ Replace the entire board with fresh shapes. """ rareshapecount = 0 for i in range(self.size): if rareshapecount
Full Post

No comments:

Post a Comment