######initializing global variables########
movecount = 0
map1 = Map.Map()
charImages = ImagePaths.CharImagePaths(movecount)
last_key_pressed = "DOWN"
#######initialize pygame#######
pygame.init()
#D - Display configuration
resolution = (800,600)
screen = pygame.display.set_mode(resolution)
######main game loop#######
def main():
#E - Entities (things moving about on screen)
pygame.display.set_caption("THE GAME")
background = pygame.Surface(screen.get_size())
background.fill((50,255,50)) # what color the background is in RGB
screen.blit(background, (0, 0))
framecount = 0
last_key_pressed = "DOWN"
# Assign sprite classes and other classes to variables
info = HUDs.HUD()
info.center = (100, 50)
char = CharacterSprites.Char(movecount, charImages, map1)
wiz = EnemySprites.Wiz(screen)
wiz1 = EnemySprites.Wiz(screen)
wiz2 = EnemySprites.Wiz(screen)
gold = ItemSprites.Gold()
gold1 = ItemSprites.Gold()
gold2 = ItemSprites.Gold()
cache = ImageCache.ImageCache()
map1.load('Map/map.txt')
#A - Action (broken into ALTER steps)
#A Assign values to key variables
goodSprites = pygame.sprite.Group(char)
itemSprites = pygame.sprite.Group(gold, gold1, gold2)
badSprites = pygame.sprite.Group(wiz, wiz1, wiz2)
infoSprites = pygame.sprite.Group(info)
clock = pygame.time.Clock() #initializing the clock
keepGoing = True #initialally we want to Keep Going
# Main caption
pygame.display.set_caption("THE GAME")
#L - Set up main loop
while keepGoing:
#T - Timer to set frame rate
clock.tick(30) #setting frame rate number = fps
#UPDATE = pygame.USEREVENT
#pygame.time.set_timer(UPDATE, int(1000.0/30))
#for animation######
framecount += 1
framecount = framecount % 4 #lower the number to speed up animation
#The aboove code is for animation
#pygame.mouse.set_visible(True)
#E - Event handling
#saving the old position, before we change it, just incase we hit a wall.
char.old_x = char.rect.centerx
char.old_y = char.rect.centery
map1.old_vp_x = map1.vpCoordinate_x
map1.old_vp_y = map1.vpCoordinate_y
# The above is for wall collisions
for event in pygame.event.get():
if event.type == pygame.QUIT:
keepGoing = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
if char.rect.centerx == char.start_x:
map1.advanceVelocity_x += -char.speed
map1.vpCoordinate_x += map1.advanceVelocity_x
last_key_pressed = "LEFT"
elif event.key == pygame.K_RIGHT:
if char.rect.centerx == char.start_x:
map1.advanceVelocity_x += char.speed
map1.vpCoordinate_x += map1.advanceVelocity_x
last_key_pressed = "RIGHT"
elif event.key == pygame.K_UP:
if char.rect.centery == char.start_y:
map1.advanceVelocity_y += -char.speed
map1.vpCoordinate_y += map1.advanceVelocity_y
last_key_pressed = "UP"
elif event.key == pygame.K_DOWN:
if char.rect.centery == char.start_y:
map1.advanceVelocity_y += char.speed
map1.vpCoordinate_y += map1.advanceVelocity_y
last_key_pressed = "DOWN"
elif event.key == pygame.K_ESCAPE:
pygame.event.post(pygame.event.Event(pygame.QUIT, {}))
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
map1.advanceVelocity_x += char.speed
#char.rect.centerx += char.speed
elif event.key == pygame.K_RIGHT:
map1.advanceVelocity_x += -char.speed
#char.rect.centerx -= char.speed
elif event.key == pygame.K_UP:
map1.advanceVelocity_y += char.speed
#char.rect.centery += char.speed
elif event.key == pygame.K_DOWN:
map1.advanceVelocity_y += -char.speed
#char.rect.centery -= char.speed
#map1.vpCoordinate_x += map1.advanceVelocity_x
#map1.vpCoordinate_y += map1.advanceVelocity_y
if map1.vpCoordinate_x < map1.minHorzScrollBounds:
map1.vpCoordinate_x = map1.minHorzScrollBounds
print "minHorz"
if map1.vpCoordinate_x > map1.maxHorzScrollBounds:
map1.vpCoordinate_x = map1.maxHorzScrollBounds
print "maxHorz"
if map1.vpCoordinate_y < map1.minVertScrollBounds:
map1.vpCoordinate_y = map1.minVertScrollBounds
print "minVert"
if map1.vpCoordinate_y > map1.maxVertScrollBounds:
map1.vpCoordinate_y = map1.maxVertScrollBounds
print "maxVert"
if last_key_pressed == "LEFT":
if map1.vpCoordinate_x == map1.minHorzScrollBounds:
char.rect.centerx -= char.speed
if map1.vpCoordinate_x == map1.maxHorzScrollBounds:
if char.rect.centerx > char.start_x:
char.rect.centerx -= char.speed
if last_key_pressed == "RIGHT":
if map1.vpCoordinate_x == map1.maxHorzScrollBounds:
char.rect.centerx += char.speed
if map1.vpCoordinate_x == map1.minHorzScrollBounds:
if char.rect.centerx < char.start_x:
char.rect.centerx += char.speed
if last_key_pressed == "UP":
if map1.vpCoordinate_y == map1.minVertScrollBounds:
char.rect.centery -= char.speed
if map1.vpCoordinate_y == map1.maxVertScrollBounds:
if char.start_y < char.rect.centery:
char.rect.centery -= char.speed
if last_key_pressed == "DOWN":
if map1.vpCoordinate_y == map1.maxVertScrollBounds:
char.rect.centery += char.speed
if map1.vpCoordinate_y == map1.minVertScrollBounds:
if char.start_y > char.rect.centery:
char.rect.centery += char.speed
#######directional controls
#This is for the character animation
#When we switch directions we switch which pictures are animated
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT or event.key == pygame.K_DOWN or event.key == pygame.K_UP:
if framecount == 0:
if last_key_pressed == "UP":
char.movecount += 1
char.movecount = char.movecount % len(charImages.character_array_back)
char.image = pygame.image.load(charImages.character_array_back[char.movecount])
elif last_key_pressed == "DOWN":
char.movecount += 1
char.movecount = char.movecount % len(charImages.character_array_front)
char.image = pygame.image.load(charImages.character_array_front[char.movecount])
elif last_key_pressed == "LEFT":
char.movecount += 1
char.movecount = char.movecount % len(charImages.character_array_left)
char.image = pygame.image.load(charImages.character_array_left[char.movecount])
elif last_key_pressed == "RIGHT":
char.movecount += 1
char.movecount = char.movecount % len(charImages.character_array_right)
char.image = pygame.image.load(charImages.character_array_right[char.movecount])
######Check for sprites colliding with other sprites######
hitItem = pygame.sprite.spritecollide(char, itemSprites, False)
if hitItem:
for theItem in hitItem:
theItem.reset() #resets location
info.gold += 1 #adds one to gold
hitBad = pygame.sprite.spritecollide(char, badSprites, False)
if hitBad:
for theBad in hitBad:
char.reset()#resets location
info.life -= 1# ads one to kills
if info.life <= 0:
keepGoing = False
#R - Refresh display "blit" means printing an image
###Clear old sprite groups
goodSprites.clear(screen, background)
itemSprites.clear(screen, background)
badSprites.clear(screen, background)
infoSprites.clear(screen, background)
###Update the sprite group positions, etc.
goodSprites.update(last_key_pressed)
itemSprites.update()
badSprites.update()
infoSprites.update()
###Draw new sprite groups
map1.startXTile = int(math.floor(float(map1.vpCoordinate_x) / map1.tilewidth))
map1.startYTile = int(math.floor(float(map1.vpCoordinate_y) / map1.tileheight))
#print "tile width = " + str(map1.tilewidth)
#print "tile height = " + str(map1.tileheight)
map1.draw(screen)
goodSprites.draw(screen)
itemSprites.draw(screen)
badSprites.draw(screen)
infoSprites.draw(screen)
#print "length of map1.tiles:"+str(len(map1.tiles))
#print "length of map1.tiles[0]:"+str(len(map1.tiles[0]))
#print "length of map1.images:"+str(len(map1.images))
#print "map1.startXTile = " + str(map1.startXTile)
#print "map1.startYTile = " + str(map1.startYTile)
#print "map1.numXTiles = " + str(map1.numXTiles)
#print "map1.numYTiles = " + str(map1.numYTiles)
if (char.rect.centerx != char.old_x) or (char.rect.centery != char.old_y):
print "Character Position: (" + str(char.rect.centerx) + ", " + str(char.rect.centery) + ")"
####Switch the screen to the new information
pygame.display.flip()
# End main game loop
if __name__ == "__main__":
main()
import pygame, math, ImageCache
class Map:
def __init__(self):
self.tiles = []
self.images = []
self.collision = []
self.tilewidth = 0
self.tileheight = 0
self.shift_wall_x = 0
self.shift_wall_y = 0
def load(self, map_file):
cache = ImageCache.ImageCache()
self.images.append(cache.getImage('Images/Scenery/ocean.png'))
self.images.append(cache.getImage('Images/Scenery/pathway.png'))
self.images.append(cache.getImage('Images/Scenery/brick.png'))
self.tilewidth = self.images[0].get_width()
self.tileheight = self.images[0].get_height()
for line in open(map_file, 'r'):
characters = line.strip().split(',')
if len(characters) > 0:
self.tiles.append([])
self.collision.append([])
# Run through all the tile characters
for character in characters:
if character == 'OCN':
# [-1] means: the last element of
# I'm pushing 0 here, because 'red' is the 0th element in self.images
self.tiles[-1].append(0)
self.collision[-1].append(True)
elif character == 'PTH':
self.tiles[-1].append(1)
self.collision[-1].append(False)
elif character == 'BRK':
self.tiles[-1].append(2)
self.collision[-1].append(True)
else:
# Right now this makes each row one tile shorter, because
# we're not storing any data for this tile.
print 'Wrong tile: [' + character + ']'
self.vpRenderOffset = (0,0)
self.vpStatsOffset_x = (80,540)
self.vpStatsOffset_y = (80, 555)
self.vpCoordinate_x = 0
self.vpCoordinate_y = 0
self.vpDimensions = (800, 600)
self.minHorzScrollBounds = 0
self.maxHorzScrollBounds = 400
self.minVertScrollBounds = 0
self.maxVertScrollBounds = 400
self.advanceVelocity_x = 0
self.advanceVelocity_y = 0
self.numXTiles = int(math.ceil(float(self.vpDimensions[0]) / self.tilewidth)) + 1
self.numYTiles = int(math.ceil(float(self.vpDimensions[1]) / self.tileheight)) + 1
self.tiledBG = pygame.Surface((self.numXTiles * self.tilewidth, self.numYTiles * self.tileheight)).convert()
self.wallsBG = pygame.Surface((self.numXTiles * self.tilewidth, self.numYTiles * self.tileheight)).convert()
def draw(self,screen):
for x in range(self.startXTile, self.startXTile + self.numXTiles):
for y in range(self.startYTile, self.startYTile + self.numYTiles):
#print map1.images[map1.tiles[y][x]]
self.tiledBG.blit(self.images[self.tiles[y][x]], ((x - self.startXTile) * self.tilewidth, (y - self.startYTile) * self.tileheight))
screen.blit(self.tiledBG, self.vpRenderOffset, (self.vpCoordinate_x - (self.startXTile * self.tilewidth), (self.vpCoordinate_y - (self.startYTile * self.tileheight))) + self.vpDimensions)
import pygame, math
from Images import ImagePaths
from Map import Map, ImageCache
class Char(pygame.sprite.Sprite):
def __init__(self, movecount, charImages, map1):
pygame.sprite.Sprite.__init__(self)
self.map1 = map1
self.movecount = movecount
self.image = charImages.character_front_image
self.image = self.image.convert()
self.rect = self.image.get_rect()
# Transparent
self.colorkey = self.image.get_at((0, 0))
self.image.set_colorkey(self.colorkey)
# end transparent
self.rect.centerx = 400
self.rect.centery = 300
self.speed = 7
self.old_x = self.rect.centerx
self.old_y = self.rect.centery
self.start_x = self.rect.centerx
self.start_y = self.rect.centery
def reset(self):
self.rect.centerx = 400
self.rect.centery = 300
def update(self, last_key_pressed):
# Put the player in the new spot
self.rect.center = (self.rect.centerx, self.rect.centery)
self.last_key_pressed = last_key_pressed
cal_x = 0
cal_y = 0
if self.last_key_pressed == "LEFT":
cal_x = -10
cal_y = 0
if self.last_key_pressed == "RIGHT":
cal_x = 5
cal_y = 0
if self.last_key_pressed == "UP":
cal_x = 0
cal_y = -11
if self.last_key_pressed == "DOWN":
cal_x = 0
cal_y = 10
else:
pass
#######%$$*^%^&*%^$#%^#&%$*&%
#Did we hit a wall???????????
#######$#$%^#^%#$^%#^%$#%$#%$
if self.map1.old_vp_x != self.map1.vpCoordinate_x or self.map1.old_vp_y != self.map1.vpCoordinate_y:
print "vpCoordinates = (" + str(self.map1.vpCoordinate_x) + ", " + str(self.map1.vpCoordinate_y) +")"
if self.last_key_pressed == "UP" or self.last_key_pressed == "DOWN":
self.map1.shift_wall_y = int(math.ceil((self.map1.vpCoordinate_y)/40))
if self.last_key_pressed == "RIGHT" or self.last_key_pressed == "LEFT":
self.map1.shift_wall_x = int(math.ceil((self.map1.vpCoordinate_x)/40))
print "map1.shift_wall = (" + str(self.map1.shift_wall_x) + ", " + str(self.map1.shift_wall_y) + ")"
if self.map1.collision[self.map1.shift_wall_y + int(math.ceil((self.rect.centery)/40))][self.map1.shift_wall_x + int(math.ceil((self.rect.centerx)/40))] == True:
print "shift_wall = (" + str(self.map1.shift_wall_x) + ", " + str(self.map1.shift_wall_x) + ")"
print "[y][x] = [" + str(int(math.floor((self.rect.centery+cal_y)/40))) + "][" + str(int(math.ceil((self.rect.centerx+cal_x)/40))) + "]"
self.rect.center = (self.old_x, self.old_y)
else:
#Didn't hit a wall
self.old_x = self.rect.centerx
self.old_y = self.rect.centery