Skip to content

Commit

Permalink
Cleaning, melee, controls and pickups
Browse files Browse the repository at this point in the history
  • Loading branch information
sgoodspeed committed Apr 23, 2012
1 parent 5e49505 commit b403b7e
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 54 deletions.
6 changes: 4 additions & 2 deletions core/collisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ def collisionCheck(player,enemyGroup, ammoGroup, level):

for ammo in groupcollide(ammoGroup, player, True, False):
player.sprite.ammo += AMMO_AMOUNT

#print player.sprite.melee

for melee, enemies in groupcollide(player.sprite.meleeGroup, enemyGroup, False,False).items():
for enemy in enemies:
melee.hurt(enemy, level)
23 changes: 20 additions & 3 deletions core/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ def __init__(self, tileImage, isSolid, x, y, tileType):
self.rect.topleft = (x,y)
self.tileType = tileType

class DirtyTile(Tile):
def __init__(self, tileImage, cleanImage, x, y, tileType):
Tile.__init__(self, tileImage, True, x, y, tileType)
self.cleanImage = cleanImage
self.cleaned = False

def clean(self):
if not self.cleaned:
self.cleaned = True
self.image = self.cleanImage
draw.rect(self.image, (0,0,255), self.image.get_rect())
print "blah"



## Tilesheet class
class TileSheet(object):
Expand Down Expand Up @@ -78,9 +92,12 @@ def render(self, data):
tileImage = self.tilemap.get(cell)
if tileImage:
isSolid = cell in TILE_SOLIDS # If the cell is in the solid list then it should be solid
if cell == "o": # o = door
doorLoc = x*self.w, y*self.h
tile = Tile(tileImage, isSolid, x*self.w, y*self.h, cell)
if cell in DIRTY_TILES:
tile = DirtyTile(tileImage, tileImage, x*self.w, y*self.h, cell) # We need to assign a secondary image here where it says tileImage the second time that will display when it's cleaned
else:
if cell == "o": # o = door
doorLoc = x*self.w, y*self.h
tile = Tile(tileImage, isSolid, x*self.w, y*self.h, cell)
if isSolid:
solidTileGroup.add(tile)
tileGroup.add(tile) # Create a Tile object with the correct image
Expand Down
14 changes: 11 additions & 3 deletions core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@
"%": (385,289),
".": (192,96),
"&": (32,32),
"o": (0,0)
"o": (0,0),
",": (69,69)
}
TILE_SOLIDS = [ ".",
"&",
"%"
"%",
","
]

DIRTY_TILES = [","]

### PLAYER
PLAYER_SIZE = PLY_W, PLY_H = T_W*.75, T_H*.75
PLAYER_START = PLAYER_START_X, PLAYER_START_Y = 100, 504
Expand All @@ -32,11 +36,15 @@
RAT_HEALTH = 30


### PROJECTILES
### ATTACKS
BULLET_SIZE = PLY_W*2, PLY_H
BULLET_SPEED = 400
BULLET_DAMAGE = 10

MELEE_DAMAGE = 15
MELEE_SIZE = (100,10)


### PICKUPS
AMMO_SIZE = (T_W*.4, T_H*.4)
PICKUP_THROW_SPEED = 50
Expand Down
18 changes: 9 additions & 9 deletions data/levels/0.lvl
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
~~~~~~~~~o~..~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~~.~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~..~~~~~~.
~~~~~~~~~~~~~~~~~~~~~....~~~~~.
.~~~~~~~~~~~~~~~~~~~~~..~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~..~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~..~~~~~~.
.~~~~~~~~~~~~~~~~~~~~....~~~~~.
~~~~~~~~~o~.~~~~~~~~~....~~~~~.
~~~~~~~~~~~..~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~..~~~~~~~~~~~~~~~~~.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~.
...............................
,,,............................
EOL
rat 50 50
42 changes: 29 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,38 @@
# controls/player.py
# This is a combination mouse and key listener which we then pass to input manager
class PlayerController(KeyListener, MouseListener):
k_moveLeft = K_LEFT
k_moveRight = K_RIGHT
def __init__(self, player):
self.player = player
self.pressed = {}


def on_keydown(self, event):
self.pressed[event.key] = True
if not self.player.thrown:
if event.key == K_SPACE and self.player.jumping < PLAYER_MAX_JUMPS:
self.player.jump()
if event.key == K_LEFT:
self.player.move(-1)
if event.key == K_RIGHT:
self.player.move(1)
if event.key == K_z:

elif event.key == K_z:
self.player.shoot()
if event.key == K_x:
elif event.key == K_x:
self.player.meleeAttack()



def update(self):
if self.pressed.get(self.k_moveLeft) and self.pressed.get(self.k_moveRight):
self.player.move(0)
elif self.pressed.get(self.k_moveLeft):
self.player.move(-1)
elif self.pressed.get(self.k_moveRight):
self.player.move(1)
else:
self.player.move(0)


def on_keyup(self,event):
self.pressed[event.key] = False
if event.key == K_LEFT or event.key == K_RIGHT:
self.player.vX = 0

Expand All @@ -64,8 +79,8 @@ def __init__(self):
self.playerGroup = pygame.sprite.GroupSingle(self.player)

# Create the PlayerController and pass it player and add a keyboard listener to it
pc = PlayerController(self.player)
self.input.add_key_listener(pc)
self.pc = PlayerController(self.player)
self.input.add_key_listener(self.pc)

#Create Hud instance
self.gameHud = Hud(self.player.health,self.hud)
Expand Down Expand Up @@ -103,9 +118,9 @@ def changeLevel(self, nextLevel):

def update(self):
# update
dT = self.clock.get_time()

collisionCheck(self.playerGroup,self.currLevel.enemies, self.currLevel.ammo, self.currLevel)
dT = min(200, self.clock.get_time())
self.pc.update()
collisionCheck(self.playerGroup, self.currLevel.enemies, self.currLevel.ammo, self.currLevel)

self.cam.update(self.player.rect)

Expand All @@ -123,10 +138,11 @@ def update(self):

def draw(self, screen):
# draw

self.cam.draw_background(self.gameArea, self.currLevel.background)
self.cam.draw_sprite(self.gameArea, self.player)
self.cam.draw_sprite_group(self.gameArea, self.player.bullets)
self.cam.draw_sprite_group(self.gameArea,self.player.meleeGroup)
self.cam.draw_sprite_group(self.gameArea, self.currLevel.enemies)
self.cam.draw_sprite_group(self.gameArea, self.currLevel.ammo)
pygame.display.flip() # Refresh the screen
Expand Down
6 changes: 2 additions & 4 deletions world/pickUp.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ def touches(self, group):
touching.add(sprite)
return touching

def update(self, dT, level):
self.vX = self.direction * PICKUP_THROW_SPEED # This doesn't actually MOVE anything, it just sets velocity

def update(self, dT, level):
dT = dT / 1000.0

self.vY -= dT * GRAVITY_SPEED
Expand Down Expand Up @@ -51,11 +49,11 @@ def update(self, dT, level):
self.rect.bottom = rect.top
self.vX = 0


class AmmoPickup(Pickup):
def __init__(self, x, y, direction, vY):
Pickup.__init__(self)
self.direction = direction
self.vX = self.direction * PICKUP_THROW_SPEED
self.rect = Rect((x,y), AMMO_SIZE)
self.image = Surface(self.rect.size)
draw.rect(self.image, (255,0,0), self.image.get_rect())
Expand Down
42 changes: 23 additions & 19 deletions world/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from pygame.locals import *
from pygame.sprite import *
from pygame import Surface,Rect,draw
from projectiles import Bullet
from projectiles import *
from core.settings import *
from core.level import *

class Player(Sprite):
vX = 0
Expand All @@ -20,6 +21,7 @@ def __init__(self):
self.thrown = False

self.rect = Rect(PLAYER_START, PLAYER_SIZE) # Build the player's rect
self.cleanRect = Rect(self.rect.bottomleft, (10,10))

self.image = Surface(self.rect.size) # Give the player a surface the size of the rect
self.image.fill((0,0,0)) # Fill the surface with black
Expand All @@ -30,16 +32,17 @@ def __init__(self):
self.direction = 1

self.bullets = Group()
self.meleeGroup = GroupSingle()
self.ammo = 9001
self.health = 100

self.melRect = Rect(self.rect.x + 27, self.rect.y + 10, 32,12)
self.melImage = Surface(self.melRect.size)
self.melImage.fill((0,255,0))
self.melImage.set_colorkey((0,255,0))
self.facing = 1


def move(self, direction):
self.vX = direction * PLAYER_SPEED # This doesn't actually MOVE anything, it just sets velocity
if direction !=0:
self.facing = self.direction

self.direction = direction

def jump(self):
Expand Down Expand Up @@ -75,10 +78,13 @@ def update(self, dT, level):
# update position
prev_rect = self.rect
self.rect = self.rect.move(dX, dY)
self.melRect.move(dX,dY)

self.rect.clamp_ip(level.bounds)

for sprite in self.touches(level.solidTiles):
if isinstance(sprite, DirtyTile):
sprite.clean()

rect = sprite.rect

# collide with walls
Expand Down Expand Up @@ -107,13 +113,19 @@ def update(self, dT, level):
elif self.jumping > 0 and not ((self.rect.left <= rect.right and prev_rect.left >= rect.right) or (self.rect.right >= rect.left and prev_rect.right <= rect.left)):
self.jumping = 0
self.rect.bottom = rect.top

self.cleanRect.topleft = self.rect.topleft

if self.meleeGroup is not None:
self.meleeGroup.update(dT)





def shoot(self):
if self.ammo > 0:
bullet = Bullet(self.rect.x, self.rect.y, self.direction, 0)
bullet = Bullet(self.rect.x, self.rect.y, self.facing, 0)
self.bullets.add(bullet)
self.ammo -= 1
print self.ammo
Expand All @@ -122,19 +134,11 @@ def meleeAttack(self):

self.melee = True
if self.melee:
print "Here"
draw.rect(self.melImage, (0,0,255), self.melImage.get_rect())
self.attack = MelRect(self)
self.meleeGroup.add(self.attack)

self.timer+=1
print self.timer
if self.timer > 10:
self.timer = 0
self.melee = False


##
## self.melImage.fill(0,255,0)
## self.melee = False




Expand Down
42 changes: 41 additions & 1 deletion world/projectiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,44 @@ def update(self, dT, level):

# Kill if we're out of the level
if not level.bounds.contains(self.rect):
self.kill()
self.kill()

class MelRect(Sprite):
color = 0,0,255
damage = MELEE_DAMAGE
rectSize = MELEE_SIZE
def __init__(self,player):
Sprite.__init__(self)
self.player = player
if self.player.facing == 1:
self.rect = Rect(self.player.rect.midright,self.rectSize)
elif self.player.facing == -1:
self.rect = Rect((self.player.rect.midleft[0]-self.rectSize[0],self.player.rect.midleft[1]),self.rectSize)

self.image = Surface(self.rect.size)
draw.rect(self.image, self.color, self.rect)
self.timer = 0

#Collision-y code stuff
self.hasHurt = []
def update(self,dT):
self.timer+=dT
if self.player.facing == 1:
self.rect.midleft = self.player.rect.midright
elif self.player.facing == -1:
self.rect.midright = self.player.rect.midleft

self.rect.midleft
#print self.timer
if self.timer > .40:

self.kill()
def hurt(self, enemy, level):
if enemy not in self.hasHurt:
self.hasHurt.append(enemy)
enemy.takeDamage(MELEE_DAMAGE, level)





0 comments on commit b403b7e

Please sign in to comment.