Friday, November 11, 2011

Dungeon Source

 Here is the dungeon source:

#import right here
import pygame
import sys
import time
from pygame.locals import *
import random
import math
import entities

pygame.init()
mainClock = pygame.time.Clock()

windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('Dungeon Sam is pretty damn cool v0.9,  10/16/11')

white = (255, 255, 255)
black = (0, 0, 0)

windowSurface.fill(white)

#Load/ define images here
hero = pygame.Rect(300, 100, 30, 33)#hero-the-rectangle must have the same co-ordinates as heroStretchedImage
heroImage = pygame.image.load('hero.png')
heroStrechedImage = pygame.transform.scale(heroImage,(30, 33))
wall = pygame.Rect(250, 200, 38, 38)
wallImage = pygame.image.load('Wall.png')
enemy = pygame.Rect(150, 350, 38, 38)
enemyImage = pygame.image.load('enemy.png')
powerUp = pygame.Rect(100, 100, 21, 21)#power up, may be removed, just an idea that is easy to make
powerUpImage = pygame.image.load('Diamond.png')
arrowImage = pygame.image.load('arrowUp.png')
swordImage = pygame.image.load('Sword.png')

HP = pygame.font.SysFont(None, 20).render('HP', False, black,)#Draw "HP" on the screen(not really, "blit" is the one that does it)
#Define Variables here
HpRedContent = 0 #hpredcontent is the amount of red is in the hp bar. It makes the hpbar slide from green to red
HpGreenContent = 255
HpRect = HP.get_rect()
blit = False
heroHp = 200
monsterHp = 50
arrowBlit = False
ARROWMOVESPEED = 5
enemyBlit = True
swordBlit = False
shifty = False
lifetime = 0
onScreen = False



#Define Objects here


#static object, doesn't do much yet...
class Static(object):
    """base class for solid non-moving objects such as walls"""

    def __init__(self, image, name):
        self.name = name
        self.image = image
        self.x = self.image.x
        self.y = self.image.y       
   


class weapon(object): 
    """Arrow"""
    def shoot(self, direction):
        if direction == "Left":
            arrow.move_ip(-1 * ARROWMOVESPEED, 0)
        if direction == "Right":
            arrow.move_ip(ARROWMOVESPEED, 0)
        if direction == "Up":
            arrow.move_ip(0, -1 * ARROWMOVESPEED)
        if direction == "Down":
            arrow.move_ip(0, ARROWMOVESPEED)
       
    def getRatio (self, A, B, AMS):#AMS means arrowmovespeed
        self.__ratio = AMS / math.sqrt(A * A + B * B)
    def getXY (self, A, B):
        self.__X = self.__ratio * A
        self.__Y = self.__ratio * B
    def getNextX(self, heroX):
        NextX = heroX + self.__X
        return NextX
    def getNextY(self, heroY):
        NextY = heroY + self.__Y
        return NextY

class melee(weapon): 
    """Sword"""
    def __init__(self):
        lifetime = 0

    def slash(self, direction):
        if direction == "Left":
            arrow.move_ip(-1 * ARROWMOVESPEED, 0)
        if direction == "Right":
            arrow.move_ip(ARROWMOVESPEED, 0)
        if direction == "Up":
            arrow.move_ip(0, -1 * ARROWMOVESPEED)
        if direction == "Down":
            arrow.move_ip(0, ARROWMOVESPEED)
     

player = entities.Hero(hero, 'Bob', heroHp)
monster1 = entities.Monster(enemy, 'joe', monsterHp)
MOVESPEED = 2
staticWall = Static(wall, 'wall')

#mainloop...program actually runs here
while player.Hp > 0: # DON"T DELETE THE WHILE LOOP
    for event in pygame.event.get():#detects events
        if event.type == QUIT:#if the "X" button is pressed in top right
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT or event.key == ord('a'):
                player.moving = True
                player.movingLeft = True
                player.movingRight = False
            if event.key == K_RIGHT or event.key == ord('d'):
                player.moving = True
                player.movingRight = True
                player.movingLeft = False
            if event.key == K_UP or event.key == ord('w'):
                player.moving = True
                player.movingUp = True
                player.movingDown = False
            if event.key == K_DOWN or event.key == ord('s'):
                player.moving = True
                player.movingUp = False
                player.movingDown = True
            if event.key == K_x:
                pygame.quit()
                sys.exit()
            if event.key == K_LSHIFT:
                shifty = True

        if event.type == KEYUP:
            if event.key == K_UP or event.key == ord('w'): # WASD!!
                player.moving = False
                player.movingUp = False
            if event.key == K_DOWN or event.key == ord('s'):
                player.moving = False
                player.movingDown = False
            if event.key == K_LEFT or event.key == ord('a'):
                player.moving = False
                player.movingLeft = False
            if event.key == K_RIGHT or event.key == ord('d'):
                player.moving = False
                player.movingRight = False
            if event.key == K_LSHIFT:
                shifty = False
        if event.type == MOUSEBUTTONDOWN and shifty == False:#ARROWS ARROWS ARROWS ARROWS ARROWS
            xDelta = event.pos[0] - hero.centerx
            yDelta = event.pos[1] - hero.centery
            arrow = pygame.Rect(hero.centerx, hero.centery, 9, 14)
            newArrow = weapon()
            newArrow.getRatio(xDelta, yDelta, 10)
            newArrow.getXY(xDelta, yDelta)
            arrowBlit = True
        if event.type == MOUSEBUTTONDOWN and shifty == True:#SWORDS SWORDS SWORDS SWORDS SWORDS
            xDelta = event.pos[0] - hero.centerx
            yDelta = event.pos[1] - hero.centery
            sword = pygame.Rect(hero.centerx, hero.centery, 9, 14)
            newSword = melee()
            newSword.getRatio(xDelta, yDelta, 10)
            newSword.getXY(xDelta, yDelta)
            swordBlit = True
            onScreen = True

    if onScreen == True:
        lifetime += 1
    if lifetime >= 4 and swordBlit == True:
        swordBlit = False
        onScreen = False
        lifetime = 0
   
    # Move arrow and update arrows                   
    if arrowBlit == True:
        arrow.centerx = newArrow.getNextX(arrow.centerx)
        arrow.centery = newArrow.getNextY(arrow.centery)
    if arrowBlit == True and arrow.colliderect(enemy):
        monster1.Hp -= 1 #arrow does 1 damage
    if monster1.Hp == 0:
        enemyBlit == False
    #Move and update swords
    if swordBlit == True and lifetime < 4 and lifetime != 0:
        sword.centerx = newSword.getNextX(sword.centerx)
        sword.centery = newSword.getNextY(sword.centery)
    if swordBlit == True and sword.colliderect(enemy):
        monster1.Hp -= 1 #arrow does 1 damage
    if monster1.Hp < 0:
        enemyBlit == False

    player.getOldX()
       
    #calls on players moving methods      
    if player.movingLeft == True and player.image.left > 0:
        player.moveLeft(MOVESPEED)
    if player.movingRight == True and player.image.right < 500:
        player.moveRight(MOVESPEED)
    if player.movingUp == True and player.image.top > 0:
        player.moveUp(MOVESPEED)
    if player.movingDown == True and player.image.bottom < 400:
        player.moveDown(MOVESPEED)
   
    windowSurface.fill(white)

  
    if player.image.colliderect(wall):
        player.collideWall()
       
    if player.image.colliderect(enemy):
        player.takeDamage(HpRedContent, HpGreenContent)
    HpRedContent = player.newRed
    HpGreenContent = player.newGreen

Here is the entities module:

import pygame
import sys
from pygame.locals import*

hero = pygame.Rect(300, 100, 30, 33)#hero-the-rectangle must have the same co-ordinates as heroStretchedImage
heroImage = pygame.image.load('hero.png')
heroStrechedImage = pygame.transform.scale(heroImage,(30, 33))
enemy = pygame.Rect(150, 350, 38, 38)
enemyImage = pygame.image.load('enemy.png')

#Entity object - all moving things(player, monsters, arrow) will be derived from this
class Entity(object):
    """Entity"""
    #reintroduced moving; thought it would be useful
    moving = False
    movingLeft = False
    movingRight = False
    movingUp = False
    movingDown = False

   
      

    def __init__(self, image, name, Hp):
        self.name = name
        self.image = image
        self.Hp = Hp
        self.x = self.image.x
        self.y = self.image.y
        self.location = (self.x, self.y)
        self.newRed = 0
        self.newGreen = self.Hp + 55

    #moving methods
    def moveLeft(self, Xspeed):
        self.image.left -= Xspeed

    def moveRight(self, Xspeed):
        self.image.right += Xspeed

    def moveUp(self, Yspeed):
        self.image.top -= Yspeed

    def moveDown(self, Yspeed):
        self.image.bottom += Yspeed

    def takeDamage(amount):
        pass

#Hero object(what the player is)
class Hero(Entity):
    """Hero"""

   
    inventory = []
    oldX = None
    oldY = None

    def getOldX(self):
        self.oldX = self.image.left
        self.oldY = self.image.top

    def heroDie(self):
        pygame.quit()
        sys.exit()

    def collideWall(self):
        self.image.left = self.oldX
        self.image.top = self.oldY

    def takeDamage(self, RedContent, GreenContent):
        self.Hp -= 1
        self.newRed += 1
        self.newGreen = self.Hp

class Monster(Entity):
    """MONSTER"""

Images: