Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # namespace
- import pygame
- from pygame.locals import *
- import sys
- import random
- import numpy
- import time
- from tkinter import filedialog
- from tkinter import *
- # Setup Pygame Game Functions
- pygame.init()
- # RPGPython Game Variables
- vec = pygame.math.Vector2
- HEIGHT = 350
- WIDTH = 700
- ACC = 0.3
- FRIC = -0.10
- FPS = 60
- FPS_CLOCK = pygame.time.Clock()
- COUNT = 0
- # Create the display surface
- displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
- pygame.display.set_caption("Awesome RPG")
- # Sets up The Game Color Modifications
- color_light = (170, 170, 170)
- color_dark = (100, 100, 100)
- color_white = (255, 255, 255)
- # Sets up the fonts for the game
- headingfont = pygame.font.SysFont("Verdana", 40)
- regularfont = pygame.font.SysFont('Corbel', 25)
- smallerfont = pygame.font.SysFont('Corbel', 16)
- text = regularfont.render('LOAD', True, color_light)
- # Frame sets - Sets up character movements animation
- run_ani_R = [pygame.image.load("Movement_Animations/Player_Sprite_R.png"), pygame.image.load("Movement_Animations/Player_Sprite2_R.png"),
- pygame.image.load("Movement_Animations/Player_Sprite3_R.png"), pygame.image.load("Movement_Animations/Player_Sprite4_R.png"),
- pygame.image.load("Movement_Animations/Player_Sprite5_R.png"), pygame.image.load("Movement_Animations/Player_Sprite6_R.png"),
- pygame.image.load("Movement_Animations/Player_Sprite_R.png")]
- run_ani_L = [pygame.image.load("Movement_Animations/Player_Sprite_L.png"), pygame.image.load("Movement_Animations/Player_Sprite2_L.png"),
- pygame.image.load("Movement_Animations/Player_Sprite3_L.png"), pygame.image.load("Movement_Animations/Player_Sprite4_L.png"),
- pygame.image.load("Movement_Animations/Player_Sprite5_L.png"), pygame.image.load("Movement_Animations/Player_Sprite6_L.png"),
- pygame.image.load("Movement_Animations/Player_Sprite_L.png")]
- att_ani_L = [pygame.image.load("Attack_Animations/Player_Attack_L.png"), pygame.image.load("Attack_Animations/Player_Attack2_L.png"),
- pygame.image.load("Attack_Animations/Player_Attack2_L.png"), pygame.image.load("Attack_Animations/Player_Attack3_L.png"),
- pygame.image.load("Attack_Animations/Player_Attack3_L.png"), pygame.image.load("Attack_Animations/Player_Attack4_L.png"),
- pygame.image.load("Attack_Animations/Player_Attack4_L.png"), pygame.image.load("Attack_Animations/Player_Attack5_L.png"),
- pygame.image.load("Attack_Animations/Player_Attack5_L.png"), pygame.image.load("Movement_Animations/Player_Sprite_L.png")]
- att_ani_R = [pygame.image.load("Attack_Animations/Player_Attack_R.png"), pygame.image.load("Attack_Animations/Player_Attack2_R.png"),
- pygame.image.load("Attack_Animations/Player_Attack2_R.png"), pygame.image.load("Attack_Animations/Player_Attack3_R.png"),
- pygame.image.load("Attack_Animations/Player_Attack3_R.png"), pygame.image.load("Attack_Animations/Player_Attack4_R.png"),
- pygame.image.load("Attack_Animations/Player_Attack4_R.png"), pygame.image.load("Attack_Animations/Player_Attack5_R.png"),
- pygame.image.load("Attack_Animations/Player_Attack5_R.png"), pygame.image.load("Movement_Animations/Player_Sprite_R.png")]
- # creates health animations
- health_ani = [pygame.image.load("Health_Animations/heart0.png"), pygame.image.load("Health_Animations/heart.png"),
- pygame.image.load("Health_Animations/heart2.png"), pygame.image.load("Health_Animations/heart3.png"),
- pygame.image.load("Health_Animations/heart4.png"), pygame.image.load("Health_Animations/heart5.png")]
- # Functions for Background Class
- class Background(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.bgimage = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/World Building Assets/Background.png")
- self.rectBGimg = self.bgimage.get_rect()
- self.bgY = 0
- self.bgX = 0
- # Primary Game Render Functions
- def render(self):
- displaysurface.blit(self.bgimage, (self.bgX, self.bgY))
- # Functions for Ground Class
- class Ground(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.image = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/World Building Assets/ground.png")
- self.rect = self.image.get_rect(center = (350, 350))
- # Secondary Game Rendering Functions
- def render(self):
- displaysurface.blit(self.image, (self.rect.x, self.rect.y))
- # Functions for Player Class
- class Player(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.type = None
- self.image = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/World Building Assets/Player_Sprite_R.png")
- # position of player to current target
- self.rect = self.image.get_rect()
- self.vx = 0
- self.pos = vec((340, 240))
- self.vel = vec(0,0)
- self.acc = vec (0,0)
- self.direction = "RIGHT"
- # movement of player
- self.jumping = False
- self.running = False
- self.move_frame = 0
- # combat of player
- self.attacking = False
- self.attack_frame = 0
- self.cooldown = False
- # set attack animations in a loop
- self.attack_frame = True
- self.health = 5
- self.mana = 0
- self.experience = 0
- # Functions for character movements
- def move(self):
- self.acc = vec(0,0.5)
- if abs(self.vel.x) > 0.3:
- self.running = True
- else:
- self.running = False
- # movement directions for player
- pressed_keys = pygame.key.get_pressed()
- if pressed_keys[K_a]:
- self.acc.x = -ACC
- if pressed_keys[K_d]:
- self.acc.x = ACC
- # sets physics and velocity for movements
- self.acc.x += self.vel.x * FRIC
- self.vel += self.acc
- self.pos += self.vel + 0.5 * self.acc
- # warps the player from one point to another
- if self.pos.x > WIDTH:
- self.pos.x = 0
- if self.pos.x < 0:
- self.pos.x = WIDTH
- self.rect.midbottom = self.pos
- # gravity (physics checks)
- def gravity_check(self):
- hits = pygame.sprite.spritecollide(player, ground_group, False)
- if self.vel.y > 0:
- if hits:
- lowest = hits[0]
- if self.pos.y < lowest.rect.bottom:
- self.pos.y = lowest.rect.top +1
- self.vel.y = 0
- self.jumping = False
- # defines character updates to movements, jumps, attacks etc.
- def update(self):
- hit = pygame.sprite.spritecollide(self, Playergroup, False)
- if self.move_frame > 6:
- self.move_frame = 0
- return
- if self.jumping == False and self.running == True:
- if self.vel.x > 0:
- self.image = run_ani_R[self.move_frame]
- self.direction = "RIGHT"
- else:
- self.image = run_ani_L[self.move_frame]
- self.direction = "LEFT"
- self.move_frame += 1
- # checks if the player has picked up the correct item on screen
- if hit:
- if player.health < 5 and self.type == 1:
- player.health += 1
- # defines character attacks
- def attack(self):
- # Player Attack Loops
- if player.attack_frame:
- if self.attack_frame > 5:
- self.attack_frame = 0
- self.attacking = False
- if self.direction == "LEFT":
- self.image = att_ani_L[self.attack_frame]
- elif self.direction == "RIGHT":
- self.image = att_ani_R[self.attack_frame]
- # update the current attack
- self.attack_frame += 1
- player.attack_frame = False
- else:
- player.attack_frame = True
- # defines character jump
- def jump(self):
- self.rect.x += 1
- # checks if player is touching the ground
- hits = pygame.sprite.spritecollide(self, ground_group, False)
- self.rect.x -= 1
- # if touching the ground, and not currently jumping, causes the player to jump
- if hits and not self.jumping:
- self.jumping = True
- self.vel.y = -12
- # Function for when the Player gets hit
- def player_hit(self):
- if self.cooldown == False:
- self.cooldown = True
- pygame.time.set_timer(hit_cooldown, 1000)
- print("hit")
- self.health = self.health - 1
- health.image = health_ani[self.health]
- if self.health <= 0:
- self.kill()
- pygame.display.update()
- # Functions for Enemy Class
- class Enemy(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.image = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/Enemy_Animations/Enemy.png")
- self.rect = self.image.get_rect()
- self.pos = vec(0,0)
- self.vel = vec(0,0)
- self.direction = random.randint(0,0)
- self.mana = 0
- # randomized the moment of the enemy
- self.vel.x = random.randint(2,6)/2
- # set the starting position of the enemy
- # starting position on the left side
- if self.direction == 0:
- self.pos.x = 0
- self.pos.y = 265
- # starting position on the right side of the enemy
- if self.direction == 1:
- self.pos.x = 700
- self.pos.y = 265
- # movement enemy to target
- def move(self):
- if self.pos.x >= (WIDTH - 20):
- self.direction = 1
- elif self.pos.x <= 0:
- self.direction = 0
- if self.direction == 0:
- self.pos.x += self.vel.x
- if self.direction == 1:
- self.pos.x -= self.vel.x
- # Update rectangle of enemy
- self.rect.center = self.pos
- # creates render for enemy character
- def render(self):
- displaysurface.blit(self.image, (self.pos.x, self.pos.y))
- # Checks for collision with the player
- def update(self, f_hits = None):
- hits = pygame.sprite.spritecollide(self, Playergroup, False)
- if hits and player.attacking == True:
- print("Enemy Killed")
- self.kill()
- elif hits and player.attacking == False:
- player.player_hit()
- rand_num = numpy.random.uniform(0, 100)
- # Activates upon either of the two expressions being true
- if hits and player.attacking == True or f_hits:
- self.kill()
- if player.mana < 100:
- player.mana += self.mana
- # Release mana
- # Release experience
- player.experience += 1
- item_no = 0
- if rand_num >= 0 and rand_num <= 5:
- # 1 / 20 chance for an item (health) drop
- item_no = 1
- elif rand_num > 5 and rand_num <= 15:
- item_no = 2
- if item_no != 0:
- # Add Item to Items group
- item = itemDrops(item_no)
- itemDrops.add(item_no)
- # Sets the item location to the location of the killed enemy
- item.posx = self.pos.x
- item.posy = self.pos.y
- # Functions for Castle Class
- class Castle(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.hide = False
- self.image = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/World Building Assets/castle.png")
- # Creates Update Functions for hiding characters and animation
- def update(self):
- if self.hide == False:
- displaysurface.blit(self.image, (250,90))
- # Functions for event handler class
- class eventHandler():
- def __init__(self):
- self.enemy_count = 0
- self.battle = False
- self.stage = 1
- self.enemy_generation = pygame.USEREVENT + 1
- self.stage_enemies = []
- for x in range(1, 21):
- self.stage_enemies.append(int((x ** 2 / 2) + 2))
- # creates the stage handle with Button Controls
- def stage_handler(self):
- self.root = Tk()
- self.root.geometry('200x170')
- button1 = Button(self.root, text = "Twilight Dungeon", width=18, height=2, command=self.world1)
- button2 = Button(self.root, text = "Skyward Dungeon", width = 18, height = 2, command = self.world2)
- button3 = Button(self.root, text = "Hall Dungeon", width = 18, height = 2, command = self.world3)
- button1.place(x = 40, y = 15)
- button2.place(x = 40, y = 65)
- button3.place(x = 40, y = 115)
- self.root.mainloop()
- # creates world functions
- def world1(self):
- self.root.destroy()
- pygame.time.set_timer(self.enemy_generation, 2000)
- castle.hide = True
- self.battle = True
- def world2(self):
- self.battle = True
- def world3(self):
- self.battle = True
- # creates setup for next set of stages
- def next_stage(self):
- self.stage += 1
- self.enemy_count = 0
- print("Stage " + str(self.stage))
- self.stage_enemies.append((int))
- self.stage_enemies.append((int(self.stage)* 2) + 1)
- pygame.time.set_timer(self.enemy_generation, 1500 - (50 * self.stage))
- # creates a health bar for the player
- class HealthBar(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.image = pygame.image.load("Health_Animations/heart5.png")
- # creates Health Bar Render
- def render (self):
- displaysurface.blit(self.image, (10, 10))
- class StageDisplay(pygame.sprite.Sprite):
- def __init__(self):
- super().__init__()
- self.text = headingfont.render("STAGE: " + str(handler.stage), True, color_dark)
- self.rect = self.text.get_rect()
- self.posx = -100
- self.posy = 100
- self.display = False
- # Creates a movement function for display
- def move_Display(self):
- self.text = headingfont.render("STAGE: " + str(handler.stage), True, color_dark)
- if self.posx < 700:
- self.posx += 5
- displaysurface.blit(self.text, (self.posx, self.posy))
- else:
- self.display = False
- self.kill()
- # creates item drop functions
- class itemDrops(pygame.sprite.Sprite):
- def __init__(self, itemType):
- super().__init__()
- if itemType == 1:
- self.image = pygame.image.load("Items/coin.png").convert_alpha()
- elif itemType == 2:
- self.image = pygame.image.load("Items/heart.png").convert_alpha()
- self.rect = self.image.get_rect()
- self.type = itemType
- self.posx = 0
- self.posy = 0
- # creates render function for Item Type
- def render(self):
- self.rect.x = self.posx
- self.rect.y = self.posy
- displaysurface.blit(self.image, self.rect)
- # Code to be activated if items comes in contact with player
- def update(self, health = None):
- hits = pygame.sprite.spritecollide(self, Playergroup, False)
- if hits:
- if player.health < 5 and self.type == 1:
- player.health += 1
- health.image = health_ani[player.health]
- self.kill()
- if self.type == 2:
- # hander.money += 1
- self.kill()
- hit = pygame.sprite
- # Classes in their own variables (To use later)
- # Game Loop
- player = Player()
- castle = Castle()
- background = Background()
- ground = Ground()
- # itemDrops = pygame.sprite.Group()
- Playergroup = pygame.sprite.Group()
- Playergroup.add(player)
- stage_display = StageDisplay()
- # Ground collision detections
- ground_group = pygame.sprite.Group()
- ground_group.add(ground)
- hit_cooldown = pygame.USEREVENT + 1
- handler = eventHandler()
- Enemies = pygame.sprite.Group()
- health = HealthBar()
- # while loops
- while True:
- player.gravity_check()
- player.update()
- if player.attacking == True:
- player.attack()
- player.move()
- for event in pygame.event.get():
- if event.type == hit_cooldown:
- player.cooldown = False
- pygame.time.set_timer(hit_cooldown, 0)
- # Will run when the close window button is clicked
- if event.type == QUIT:
- pygame.quit()
- sys.exit()
- if event.type == handler.enemy_generation:
- if handler.enemy_count < handler.stage_enemies[handler.stage - 1]:
- print(handler.enemy_count)
- print(handler.stage_enemies[handler.stage - 1])
- enemy = Enemy()
- Enemies.add(enemy)
- handler.enemy_count += 1
- # For events that occur upon clicking the mouse (left click)
- if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
- if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
- player.jump()
- # Event handling for a range of different key presses
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_n:
- if handler.battle == True and len(Enemies) == 0:
- handler.next_stage()
- stage_display = StageDisplay()
- stage_display.display = True
- if event.key == pygame.K_e and 300 < player.rect.x < 400:
- print("At the door")
- handler.stage_handler()
- if event.key == pygame.K_SPACE:
- player.jump()
- if event.key == pygame.K_x:
- if player.attacking == False:
- player.attack()
- player.attacking = True
- # player variable access points
- player.move()
- # render functions
- background.render()
- ground.render()
- castle.update()
- if player.health > 0:
- displaysurface.blit(player.image, player.rect)
- health.render()
- # creates a display surface for the player
- #displaysurface.blit(player.image, player.rect)
- # This is used to render the stage to display it as an image in the game
- # if stage_display.display == True:
- # stage_display.move_Display()
- # for i in itemDrops:
- # i.render()
- # i.update()
- # creates and updates the clock and set FPS Performance and Enemy groups and other things too
- for entity in Enemies:
- entity.update()
- entity.move()
- entity.render()
- pygame.display.update()
- FPS_CLOCK.tick(FPS)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement