Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pygame import *
- import random
- spawnpoint = 0
- x = 1
- while x != 10:
- spawnpoint += random.randint(1, 10)
- x = x + 1
- init()
- width = 1500 # width of window
- height = 800 # height of window
- window = display.set_mode((width, height)) # draws window
- screen = display.get_surface() # draws ontop of window
- exitProg = False # allows to exit while loop
- overallTime = time.Clock() # starts a clock
- ######################################################################
- # player interaction
- class Character():
- def __init__(self, m, n, movement, spawnpoint): # initialises the objects
- self.Posx = m # sets the image to the center of the map
- self.Posy = n # sets the image to the center of the map
- self.dx = 0 # sets horizontal acceleration to 0
- self.dy = 0 # sets vertical acceleration to 0
- self.Movespeed = movement # sets how fast the player moves(it can change)
- self.image = image.load('Isaac.xcf') # loads the sprite for the player
- self.sword = image.load('swordSwish2.png') # loads the sword for the sprite
- self.roomCounter = 0 # sets the room counter
- self.ATK = 8 # sets the attack of the player
- self.HP = 25 # sets the hp of the player
- self.maxhp = 25 # the hp and masx hp will be equal to beging with and only the curhp should change when damage is taken
- self.attackspeed = 1 # sets attack speed of the player
- self.spawnpoint = spawnpoint # assigns where the player has begun on the map
- def collisionDoor(self, door, locked): # detects door collisions for the player
- rect = self.image.get_rect() # gets the dimensions of the sprite
- rect.x = self.Posx # sets coords for a rectangle == to the sprite
- rect.y = self.Posy # sets coords for a rectangle == to the sprite
- spawnRoom = None # changes it so that the value that shows the room the player begun in is changed
- if rect.colliderect(door) == True and locked == False: # checks for collision between player and door
- clock.resettime()
- if self.Posx > 1342: # collisionDoor also changes the position of the player and then changes where the player is on the map
- self.Posx = 95
- self.spawnpoint += 1
- spawnRoom = self.spawnpoint
- if self.Posx < 94:
- self.Posx = 1341
- self.spawnpoint -= 1
- spawnRoom = self.spawnpoint
- if self.Posy > 642:
- self.Posy = 103
- self.spawnpoint += 10
- spawnRoom = self.spawnpoint
- if self.Posy < 102:
- self.Posy = 643
- self.spawnpoint -= 10
- spawnRoom = self.spawnpoint
- character.update()
- if spawnRoom != None and not (
- spawnRoom in visited): # if the player is in a room they havent visited before, append it to the visited list
- visited.append(spawnRoom)
- self.roomCounter += 1 # increase the room counter
- else:
- return False
- return True # the code also checks for a change in the room and returns true, I use it for this purpose a bit
- return False
- def collisiondoorother(self, door, locked, dead):
- rect = self.image.get_rect() # gets the dimensions of the sprite
- rect.x = self.Posx # sets coords for a rectangle == to the sprite
- rect.y = self.Posy # sets coords for a rectangle == to the sprite
- if rect.colliderect(
- door) == True and locked == False and dead == True: # checks for collision between player and door
- return True
- return False
- def getPosition(self): # allows other classes to get the poisition of the player
- return self.Posx, self.Posy
- def moveleft(self): # sets the change in speed
- self.dx = -self.Movespeed
- return self.Posx
- def moveright(self): # sets the change in speed
- self.dx = self.Movespeed
- return self.Posx
- def movedown(self): # sets the change in speed
- self.dy = -self.Movespeed
- return self.Posy
- def moveup(self): # sets the change in speed
- self.dy = self.Movespeed
- return self.Posy
- def stopHorizontal(self): # stops character moving
- self.dx = 0
- def stopVertical(self): # stops character moving
- self.dy = 0
- def bordercheckx(self): # stops character moving past screen
- if self.Posx > 1364:
- self.Posx = 1364
- if self.Posx < 89:
- self.Posx = 89
- return self.Posx
- def borderchecky(self): # stops char moving past screen
- if self.Posy > 652:
- self.Posy = 652
- if self.Posy < 90:
- self.Posy = 90
- return self.Posy
- def update(self): # changes the coords for the sprite
- self.Posx = self.Posx + self.dx
- self.Posy = self.Posy + self.dy
- def show(self): # updates the players position on the map
- screen.blit(self.image, (self.Posx, self.Posy))
- def returnRect(self): # gets the rect for the player to compare for collision
- rect = Rect(self.Posx, self.Posy, self.image.get_width(), self.image.get_height())
- return rect
- def returnSwordrect(
- self): # sword rect based on the players position and returns it to check for collision against enemies
- rect = Rect(self.Posx - 20, self.Posy - 20, self.sword.get_width(), self.sword.get_height())
- return rect
- def GetRoomCount(self): # returns room counter
- return self.roomCounter
- def attack(self, target):
- target.reduceHP(self.ATK) # damage done to the monster being attacked
- def ShowAttack(self): # shows the sword when called
- screen.blit(self.sword, (self.Posx - 20, self.Posy - 14))
- def reduceHP(self, amount): # HP stat is encaptulated so it cant be
- self.HP -= amount
- def returncurHP(self): # returns the current health of the player
- return self.HP
- def returnmaxHP(self): # returns the max health of the player
- return self.maxhp
- def isDead(self): # This checks how high the monsters health
- if self.HP <= -1:
- return True # is and then returns true or false if its
- else:
- return False
- def levelUP(self): # this levels up the player and increases the players stats and heals them fully
- self.ATK = round(self.ATK * 1.5)
- self.maxhp = round(self.maxhp * 1.2)
- self.HP = self.maxhp
- def changemodel(self):
- health = (self.HP / self.maxhp)
- if health <= 0.1:
- self.image = image.load('Isaacdam5.png')
- elif health <= 0.2 and health > 0.1:
- self.image = image.load('Isaacdam4.png')
- elif health <= 0.4 and health > 0.2:
- self.image = image.load('Isaacdam3.png')
- elif health <= 0.6 and health > 0.4:
- self.image = image.load('Isaacdam2.png')
- elif health <= 0.8 and health > 0.6:
- self.image = image.load('Isaacdam1.png')
- else:
- self.image = image.load('Isaac.png')
- def getatkspd(self):
- return self.attackspeed
- def resetroomcount(self):
- self.roomCounter = 0
- ######################################################################
- # Door interactions with player
- class doors(Rect): # draws the doors in their positions
- def draw(self, screen):
- draw.rect(screen, (225, 225, 225), self)
- def lock(self, screen): # changes colour of the doors if they are locked
- draw.rect(screen, (255, 215, 0), self)
- ######################################################################
- # levels up the player
- class levelUp():
- def __init__(self, currentXP, NeededtoLVL): # initialises the level up system
- self.currentXP = currentXP
- self.neededtoLVL = NeededtoLVL
- self.level = 1
- def xpgain(self, xpgained): # levels up the player
- self.currentXP += xpgained
- if self.currentXP >= self.neededtoLVL:
- self.currentXP -= self.neededtoLVL
- self.neededtoLVL *= 1.7
- self.level += 1
- character.levelUP()
- def drawXPbar(self): # Draws the xp bar on the screen and changes it
- width = 1000 * (self.currentXP / self.neededtoLVL)
- bar1 = Rect(250, 760, 1000, 10)
- bar2 = Rect(250, 760, width, 10)
- draw.rect(screen, (166, 16, 30), bar1)
- draw.rect(screen, (0, 229, 238), bar2)
- def getLVL(self): # returns the level of the player to be drawn on the screen
- return self.level
- ######################################################################
- # Creates and controls the enemies
- class Enemy():
- def __init__(self): # initialises the enemies
- self.Enx = 0 # sets the position of every enemy to begin with in the top right corner
- self.Eny = 0 # sets all the stats for the enemy to be 0 as the stats need to be set with the enemy type
- self.dx = 0 # they all need to be set to 0 except for health so they cant do anything
- self.dy = 0 # the health is so high so that it doesnt accidentally trigger the death function for the enemies
- self.speed = 0
- self.HP = 99999999999
- self.ATK = 0
- self.image = image.load('blank.png')
- self.attackspeed = 0
- self.xpGain = 7
- self.levelmult = 1
- self.levelmultboss = 1
- self.xpmodifier = 1
- self.height = 0
- self.width = 0
- def moveToPlayer(self, x, y, time): # checks where the player is then moves to the position
- if time > 1:
- if x - self.width < self.Enx:
- self.dx = -self.speed
- if x - self.width > self.Enx:
- self.dx = self.speed
- if y - self.height > self.Eny:
- self.dy = self.speed
- if y - self.height < self.Eny:
- self.dy = -self.speed
- def playerCollidesDoor(self,
- enter): # checks if the player has entered a door, if it has then it resets the enemies
- if enter == True:
- self.Enx = random.randint(300, 1200)
- self.Eny = random.randint(300, 500)
- self.dx = 0
- self.dy = 0
- def collisionSword(self, sword): # checks if the enemy collides with the swords hit box
- enemy = Rect(self.Enx, self.Eny, self.image.get_width(), self.image.get_height())
- if enemy.colliderect(sword) == True:
- return True
- return False
- def collisionPlayer(self, player): # checks if the player touches the enemy
- rect = Rect(self.Enx, self.Eny, self.image.get_width(), self.image.get_height())
- if rect.colliderect(player) == True:
- EnATKSpd.resettime()
- return True
- return False
- def Archer(self): # sets the Archer class for enemies
- self.HP = round(8 * self.levelmult)
- self.ATK = round(3 * self.levelmult)
- self.speed = 1.5
- self.attackspeed = 1
- self.image = image.load('Archer.png')
- self.width = -5
- def Orc(self): # sets the Orc class for enemies
- self.HP = round(22 * self.levelmult)
- self.ATK = round(3 * self.levelmult)
- self.speed = 1.3
- self.attackspeed = 1
- self.image = image.load('Orc2.png')
- self.height = 25
- self.width = 5
- def Soldier(self): # sets the Soldier class for enemies
- self.HP = round(14 * self.levelmult)
- self.ATK = round(2 * self.levelmult)
- self.speed = 1.7
- self.attackspeed = 1
- self.image = image.load('soldier.png')
- self.width = -5
- def blank(self):
- self.HP = 9999999
- self.ATK = 0
- self.speed = 0
- self.image = image.load('blank.png')
- self.Enx = 1
- self.Eny = 1
- def update(self): # changes the speed of the enemy
- self.Enx = self.Enx + self.dx
- self.Eny = self.Eny + self.dy
- def show(self): # updates the enemy on the map
- screen.blit(self.image, (self.Enx, self.Eny))
- def attack(self, target):
- target.reduceHP(self.ATK) # damage done to the monster being attacker
- def reduceHP(self, amount): # HP stat is encapsulated so it cant be
- self.HP -= amount
- def isDead(self): # This checks how high the monsters health
- if self.HP <= 0: # is and then returns true or false if its
- self.image = image.load('blank.png')
- self.ATK = 0
- self.Enx = 1
- self.Eny = 1
- self.HP = 9999999
- self.speed = 0
- xpgain = self.xpGain * self.xpmodifier
- CharLVL.xpgain(xpgain)
- return True
- else:
- return False
- def returnHP(self):
- return self.HP
- def Boss(self):
- self.HP = round(50 * self.levelmultboss)
- self.ATK = round(5 * self.levelmultboss)
- self.speed = 1
- self.attackspeed = 4
- self.image = image.load('boss.png')
- self.Enx = 750
- self.Eny = 350
- self.height = 40
- self.width = 30
- def levelup(self):
- self.levelmultboss = self.levelmultboss * 1.2
- self.levelmult = self.levelmult * 1.25
- self.xpmodifier = self.xpmodifier * 1.2
- ######################################################################
- # Sets the time for the game
- class gameTime():
- duration = 0
- def __init__(self): # just initialises the objects
- pass
- def increase(self):
- self.duration += 0.016 # how long a frame is in secs
- def gettime(self):
- return self.duration # returns time
- def printtime(self):
- print(self.duration) # shows the time
- def resettime(self):
- self.duration = 0 # resets the clock
- ######################################################################
- # draws User interface stuff and changes it
- class UI():
- def __init__(self, HP): # inititalises the objects
- pass
- def drawHPbar(self, curHP,
- maxHP): # draws the hp bar in relation to how much health the player has left compared to the max health of the player
- width = 400 * (int(curHP) / int(maxHP))
- bar1 = Rect(40, 40, 400, 10)
- bar2 = Rect(40, 40, width, 10)
- draw.rect(screen, (166, 16, 30), bar1) # draws bar that shows lost health
- draw.rect(screen, (0, 255, 0), bar2) # draws bar that shows how much health the player has
- def showhealth(self, curHP, maxHP): # writes out with numbers how much health the player has as a fraction
- text.showtext(str(character.returncurHP()), screen, (200, 0, 200), 465, 34)
- text.showtext("___", screen, (200, 0, 200), 465, 34)
- text.showtext(str(maxHP), screen, (200, 0, 200), 465, 61)
- def dead(self, curHP): # if the player dies, replace the screen with a balck screen and write game over
- if curHP < 1:
- text.showtext("Game Over", screen, (255, 255, 255), 700, 400)
- return True
- return False
- def drawattackbar(self,
- attackspeed): # draws the attack speed bar in relation to how fast the player can attack compared to how long it has been since the players last successful hit
- width = 100 * attackspeed
- if width > 100:
- width = 100
- bar1 = Rect(1200, 40, 100, 10)
- bar2 = Rect(1200, 40, width, 10)
- draw.rect(screen, (166, 16, 30), bar1) # draws how much time left to wait for next attack
- draw.rect(screen, (0, 255, 0), bar2) # draws how much tiem waited for next attack
- ######################################################################
- class Text():
- def __init__(self): # initialises text
- pass
- def showtext(self, text, screen, colour, x, y): # calls the message display function
- self.message_display(text, screen, colour, x, y)
- def message_display(self, text, screen, colour, x, y): # blits the text onto the screen
- largetext = font.Font("freesansbold.ttf", 25) # sets size and font of text
- TextSurf, TextRect = self.text_objects(text, largetext, colour) # calls text objects function
- TextRect.center = (x, y) # gets the center of the rectangle with position x,y
- screen.blit(TextSurf, TextRect) # blits the text
- def text_objects(self, text, font, colour): # assigns the colour and renders the text
- textSurface = font.render(text, True, (colour))
- return textSurface, textSurface.get_rect() # returns the rendered text and rectangle of text
- ######################################################################
- ######################################################################
- gameMap = [[[0], [1], [2], [3], [4], [5], [6], [7], [8], [9]]
- , [[10], [11], [12], [13], [14], [15], [16], [17], [18], [19]]
- , [[20], [21], [22], [23], [24], [25], [26], [27], [28], [29]]
- , [[30], [31], [32], [33], [34], [35], [36], [37], [38], [39]]
- , [[40], [41], [42], [43], [44], [45], [46], [47], [48], [49]]
- , [[50], [51], [52], [53], [54], [55], [56], [57], [58], [59]]
- , [[60], [61], [62], [63], [64], [65], [66], [67], [68], [69]]
- , [[70], [71], [72], [73], [74], [75], [76], [77], [78], [79]]
- , [[80], [81], [82], [83], [84], [85], [86], [87], [88], [89]]
- , [[90], [91], [92], [93], [94], [95], [96], [97], [98], [99]]]
- firstroom = 1
- visited = [] # creates an empty array for visited rooms
- visited.append(spawnpoint) # appends the place where the player starts to the visited list
- textpos = 400 # this is just setting variables used later
- text = Text() # creates an object for the text class
- justonce = True
- showdamage = 0
- enemycount = 0
- number = 1
- nextlevel = False
- boss = False
- bossisdead = False
- locked = False # this is just setting variables used later
- collided = False # this is just setting variables used later
- plyAtkSpd = gameTime() # creates an object for the player attack speed
- EnATKSpd = gameTime() # creates an object for the enemy attack speed
- clock = gameTime() # creates an object for the clock
- m, n = 720, 370 # this is just setting variables used later
- x, y = 0, 0 # this is just setting variables used later
- length = random.randint(6, 10)
- enemy1 = Enemy() # sets the enemies
- enemy2 = Enemy()
- enemy3 = Enemy()
- enemy4 = Enemy()
- enemy5 = Enemy()
- enemy6 = Enemy()
- enemies = [enemy1, enemy2, enemy3, enemy4, enemy5, enemy6] # creates list of enemies
- character = Character(m, n, 3.2, spawnpoint) # sets character class
- CharLVL = levelUp(0, 100) # sets char level class
- HPbar = UI(character.returncurHP())
- doorwest = doors(85, 350, 10, 100) # sets a door
- dooreast = doors(1405, 350, 10, 100) # sets a door
- doornorth = doors(700, 85, 100, 10) # sets a door
- doorsouth = doors(700, 705, 100, 10) # sets a door
- roomModel = (90, 90, 1320, 620) # sets background size
- key.set_repeat(1, 30) # sets keys to repeat
- nextleveldoor = doors(700, 350, 100, 100)
- doors = [doorwest, dooreast, doornorth, doorsouth] # creates list of doors
- fill = (40, 0, 0)
- once = 1
- while exitProg == False: # loops while the program is running
- for e in event.get():
- if e.type == constants.QUIT: # if quit buttom pressed, end loop
- exitProg = True
- if e.type == KEYDOWN: # if key is pressed, move the character in the direction
- if (e.key == constants.K_a):
- character.moveleft()
- if (e.key == constants.K_d):
- character.moveright()
- if (e.key == constants.K_w):
- character.movedown()
- if (e.key == constants.K_s):
- character.moveup()
- if (e.key == constants.K_SPACE):
- character.ShowAttack()
- if e.type == KEYUP: # stops the character moving after the key is let go
- if (e.key == constants.K_a):
- character.stopHorizontal()
- if (e.key == constants.K_d):
- character.stopHorizontal()
- if (e.key == constants.K_w):
- character.stopVertical()
- if (e.key == constants.K_s):
- character.stopVertical()
- if (e.key == constants.K_SPACE):
- character.ShowAttack()
- screen.fill(fill) # fills the screen
- draw.rect(screen, (20, 173, 70), roomModel) # draws the green background
- for door in doors:
- door.draw(screen)
- if character.collisionDoor(door, locked) == True:
- collided = True
- if nextlevel == True:
- nextleveldoor.draw(screen)
- if character.collisionDoor(nextleveldoor, locked) == True:
- collided = True
- EnATKSpd.increase()
- plyAtkSpd.increase()
- clock.increase() # increases clock time
- p, q = character.getPosition() # gets position of the player
- time = clock.gettime() # gets current time
- for enemy in enemies: # for each enemy
- if enemycount != 0: # if there are enemies on the screen
- for door in doors:
- door.lock(screen) # lock the doors
- locked = True # set the lock to be true
- else:
- locked = False # if there are no enemies, then unlock
- if boss == True and once == 1:
- enemy.Boss()
- once += 1
- enemycount += 1
- if boss == True and enemycount == 0:
- nextlevel = True
- once = 1
- boss = False
- bossisdead = True
- if enemy.attackspeed < EnATKSpd.gettime(): # if the enemy can attack as its time before next attack is above time since last attack
- if enemy.collisionPlayer(character.returnRect()) == True: # if its touching the player
- enemy.attack(character) # attack the player
- if e.type == KEYDOWN: # if the plasyer presses space
- if (e.key == constants.K_SPACE):
- character.ShowAttack() # show the sword
- if enemy.collisionSword(
- character.returnSwordrect()) == True and plyAtkSpd.gettime() >= character.getatkspd(): # if the sword collides with the enemy
- character.attack(enemy) # then attack
- plyAtkSpd.resettime() # reset the players time to next attack
- number = 10
- if enemy.returnHP() < 1:
- enemycount -= 1
- enemy.isDead()
- if character.GetRoomCount() >= length:
- collided = False
- boss = True
- character.resetroomcount()
- length = length + random.randint(-1, 6)
- if collided == True: # if the doors are touched and entered
- spawnRate = random.randint(1, 100)
- if spawnRate <= 20:
- enemy.Archer()
- enemycount += 1
- if spawnRate >= 21 and spawnRate <= 40:
- enemy.Orc()
- enemycount += 1
- if spawnRate >= 41 and spawnRate <= 60:
- enemy.Soldier()
- enemycount += 1
- if spawnRate >= 61:
- enemy.blank() # select the enemies
- enemy.playerCollidesDoor(collided) # randomly spawn in enemies on the map
- enemy.moveToPlayer(p, q, time) # moves the enemy to the player
- enemy.update()
- if showdamage % number <= 5:
- enemy.show() # update enemies position and blits it
- character.update() # updates players position
- CharLVL.drawXPbar() # draws the xp bar for the player
- text.showtext("level " + str(CharLVL.getLVL()), screen, (200, 0, 200), 200,
- 765) # writes the level of the player next to the xp bar
- character.bordercheckx() # checks if player is out of bounds
- character.borderchecky() # checks if player is out of bounds
- character.changemodel()
- text.showtext("attack speed", screen, (200, 0, 200), 1110, 45) # writes attack speed onto the screen
- a = character.returncurHP() # sets a variable for the current hp of the player
- b = character.returnmaxHP() # sets a variable for the max hp of player
- HPbar.drawHPbar(a, b) # draws the hp bar in relation to the cur and max hp
- HPbar.showhealth(a, b) # writes the total health and current health as a fraction
- HPbar.drawattackbar(plyAtkSpd.gettime()) # draws the attack speed bar for the player
- if HPbar.dead(character.returncurHP()) == True: # if the player dies
- screen.fill((0, 0, 0)) # make the screen go black
- HPbar.dead(character.returncurHP()) # the player dies and the screen says game over
- firstroom = 10
- print(character.GetRoomCount())
- if character.collisiondoorother(nextleveldoor, locked, bossisdead) == True:
- enemy.levelup()
- red = random.randint(0, 80)
- green = random.randint(0, 80)
- blue = random.randint(0, 80)
- fill = (red, green, blue)
- bossisdead = False
- nextlevel = False
- firstroom += 1
- character.show() # shows players position on screen
- if e.type == KEYDOWN:
- if (e.key == constants.K_SPACE): # if the player presses space
- character.ShowAttack() # show the attack
- if plyAtkSpd.gettime() >= 0.8:
- number = 1
- if length < 6:
- length == 6
- showdamage = showdamage + 1
- collided = False # resets collided
- overallTime.tick(60) # sets fps
- display.flip() # shows the entire screen
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement