Hello, all. I've taken your advice on board and found some success with it, but I've had a new problem. I created a game where one could move a rectangle around by pressing up, down, left, or right. The floor is "solid," and I successfully programmed the game to stop the rectangle from moving through the floor. However, I've run into a problem again: the movement works fine for the first few button presses or so, but after that, a glitch happens where the rectangle will refuse to move at all. Here's the code, if anyone can take a look at it:
import pygame
# Define some colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
pygame.init()
#Set the width and height of the screen [width,height]
size = (700,500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Jumping Square!")
#Loop until the user clicks the close button
done = False
#Used to manage how fast the screen updates
clock = pygame.time.Clock()
def generatetiles(x,y): #Generates a tile grid, implemented by a matrix
countx = 0
county = 0
indices = []
while countx < x:
indices.append([])
countx += 1
county = 0 #reset y coordinate count each time
while county < y:
indices[countx - 1].append(0) #0 means an unnoccupied tile. Initialize to an empty grid.
county += 1
return indices
Tiles = generatetiles(70,50)
class stationaryobject: #Hitbox for anything that stays still
def __init__(self,x1,x2,y1,y2): #x1/x2 and y1/y2 specify corners of hitbox
countx = x1
county = y1
while countx <= x2: #Since x2 is counted up before it's used, we use <=
county = y1
countx += 1
while county < y2 + 1:
Tiles[countx-1][county] = 1
county += 1
class moveableobject:
def __init__(self,x1,x2,y1,y2):
self.occupiedtiles = {}
countx = x1
county = y1
while countx <= x2:
self.occupiedtiles[countx]=[]
countx += 1
county = y1
while county < y2:
self.occupiedtiles[countx-1].append(county)
Tiles[countx-1][county] = 1
county += 1
#define methods to scan the top, bottom, left and right sides of the object's hitbox to return data for movement.
def scanleftside(self):
firstxcoordinate = self.occupiedtiles.keys()[0]
return {self.occupiedtiles.keys()[0]:self.occupiedtiles[firstxcoordinate]}
def scanrightside(self):
lastxcoordinate = self.occupiedtiles.keys()[-1]
return {self.occupiedtiles.keys()[-1]:self.occupiedtiles[lastxcoordinate]}
def scantop(self):
top = {}
for xcoordinate in self.occupiedtiles:
top[xcoordinate] = self.occupiedtiles[xcoordinate][0]
return top
def scanbottom(self):
bottom = {}
for xcoordinate in self.occupiedtiles:
bottom[xcoordinate] = self.occupiedtiles[xcoordinate][-1]
return bottom
def moveleft(self):
leftside = self.scanleftside()
xcoordinate = leftside.keys()[0]
blockedflag = 0
for ycoordinate in leftside[xcoordinate]:
if Tiles[xcoordinate-1][ycoordinate] == 1: blockedflag = 1
if blockedflag == 0:
rightmostxcoordinate = self.occupiedtiles.keys()[-1]
leftmostxcoordinate = self.occupiedtiles.keys()[0]
del self.occupiedtiles[rightmostxcoordinate]
for ycoordinate in Tiles[rightmostxcoordinate]: ycoordinate = 0
self.occupiedtiles[leftmostxcoordinate-1] = self.occupiedtiles[leftmostxcoordinate]
def moveright(self):
rightside = self.scanrightside()
xcoordinate = rightside.keys()[0]
blockedflag = 0
for ycoordinate in rightside[xcoordinate]: #Check coordinates to the right of the current right side to see if they're occupied
if Tiles[xcoordinate+1][ycoordinate] == 1: blockedflag = 1
if blockedflag == 0:
rightmostxcoordinate = self.occupiedtiles.keys()[-1]
leftmostxcoordinate = self.occupiedtiles.keys()[0]
del self.occupiedtiles[leftmostxcoordinate]
for ycoordinate in Tiles[leftmostxcoordinate]: ycoordinate = 0
self.occupiedtiles[rightmostxcoordinate+1] = self.occupiedtiles[rightmostxcoordinate]
def moveup(self):
top = self.scantop()
ycoordinate = top.keys()[0]
blockedflag = 0
for xcoordinate in top:
if Tiles[xcoordinate][ycoordinate-1] == 1: blockedflag = 1
if blockedflag == 0:
for xcoordinate in self.occupiedtiles:
del self.occupiedtiles[xcoordinate][-1]
Tiles[xcoordinate][-1] = 0
self.occupiedtiles[xcoordinate].append(ycoordinate-1)
self.occupiedtiles[xcoordinate] = sorted(self.occupiedtiles[xcoordinate])
def movedown(self):
bottom = self.scanbottom()
ycoordinate = bottom[bottom.keys()[0]]
blockedflag = 0
for xcoordinate in bottom:
if Tiles[xcoordinate][ycoordinate+1] == 1: blockedflag = 1
if blockedflag == 0:
for xcoordinate in self.occupiedtiles:
del self.occupiedtiles[xcoordinate][0]
Tiles[xcoordinate][0] = 0
self.occupiedtiles[xcoordinate].append(ycoordinate+1)
self.occupiedtiles[xcoordinate] = sorted(self.occupiedtiles[xcoordinate])
player = moveableobject(1,2,3,6)
square = stationaryobject(34,39,44,49)
floor = stationaryobject(0,69,48,49)
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT: player.moveleft()
if event.key == pygame.K_RIGHT: player.moveright()
if event.key == pygame.K_UP: player.moveup()
if event.key == pygame.K_DOWN: player.movedown()
# --- Game logic should go here
# --- Drawing code should go here
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(BLACK)
pygame.draw.line(screen,GREEN,[0,490],[700,490],1)
pygame.draw.rect(screen,GREEN,[350,441,50,50],1)
pygame.draw.rect(screen,WHITE,[player.occupiedtiles.keys()[0]*10,player.occupiedtiles[player.occupiedtiles.keys()[0]][0]*10,20,40],1)
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
# If you forget this line, the program will 'hang'
# on exit if running from IDLE.
pygame.quit()
I've tried to solve this problem by setting the movement methods to print the value of "blockedflag" each time the user presses a button, to make sure that the rectangle was not being blocked unnecessarily. No such luck: "blockedflag" always returned 0, unless the square was pressing against a hard surface (which means that the blockedflag value is working the way it should). I also tried printing the global tile matrix ("Tiles", defined on line 36) each time the user moved, to make sure that the values stored in that matrix were being deleted correctly (they were). So what gives? Can anyone point to a flaw in this code that my dumb eyes seem to have missed?