Advertisement
dronkowitz

RPGPythonProject with Lola Game-U Instructor

Nov 4th, 2022 (edited)
709
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.26 KB | None | 0 0
  1. # namespace
  2. import pygame
  3. from pygame.locals import *
  4. import sys
  5. import random
  6. import numpy
  7. import time
  8. from tkinter import filedialog
  9. from tkinter import *
  10.  
  11. # Setup Pygame Game Functions
  12. pygame.init()
  13.  
  14. # RPGPython Game Variables
  15. vec = pygame.math.Vector2
  16. HEIGHT = 350
  17. WIDTH = 700
  18. ACC = 0.3
  19. FRIC = -0.10
  20. FPS = 60
  21. FPS_CLOCK = pygame.time.Clock()
  22. COUNT = 0
  23.  
  24. # Create the display surface
  25. displaysurface = pygame.display.set_mode((WIDTH, HEIGHT))
  26. pygame.display.set_caption("Awesome RPG")
  27.  
  28. # Sets up The Game Color Modifications
  29. color_light = (170, 170, 170)
  30. color_dark = (100, 100, 100)
  31. color_white = (255, 255, 255)
  32.  
  33. # Sets up the fonts for the game
  34. headingfont = pygame.font.SysFont("Verdana", 40)
  35. regularfont = pygame.font.SysFont('Corbel', 25)
  36. smallerfont = pygame.font.SysFont('Corbel', 16)
  37. text = regularfont.render('LOAD', True, color_light)
  38.  
  39. # Frame sets - Sets up character movements animation
  40. run_ani_R = [pygame.image.load("Movement_Animations/Player_Sprite_R.png"), pygame.image.load("Movement_Animations/Player_Sprite2_R.png"),
  41.              pygame.image.load("Movement_Animations/Player_Sprite3_R.png"), pygame.image.load("Movement_Animations/Player_Sprite4_R.png"),
  42.              pygame.image.load("Movement_Animations/Player_Sprite5_R.png"), pygame.image.load("Movement_Animations/Player_Sprite6_R.png"),
  43.              pygame.image.load("Movement_Animations/Player_Sprite_R.png")]
  44. run_ani_L = [pygame.image.load("Movement_Animations/Player_Sprite_L.png"), pygame.image.load("Movement_Animations/Player_Sprite2_L.png"),
  45.              pygame.image.load("Movement_Animations/Player_Sprite3_L.png"), pygame.image.load("Movement_Animations/Player_Sprite4_L.png"),
  46.              pygame.image.load("Movement_Animations/Player_Sprite5_L.png"), pygame.image.load("Movement_Animations/Player_Sprite6_L.png"),
  47.              pygame.image.load("Movement_Animations/Player_Sprite_L.png")]
  48. att_ani_L = [pygame.image.load("Attack_Animations/Player_Attack_L.png"), pygame.image.load("Attack_Animations/Player_Attack2_L.png"),
  49.              pygame.image.load("Attack_Animations/Player_Attack2_L.png"), pygame.image.load("Attack_Animations/Player_Attack3_L.png"),
  50.              pygame.image.load("Attack_Animations/Player_Attack3_L.png"), pygame.image.load("Attack_Animations/Player_Attack4_L.png"),
  51.              pygame.image.load("Attack_Animations/Player_Attack4_L.png"), pygame.image.load("Attack_Animations/Player_Attack5_L.png"),
  52.              pygame.image.load("Attack_Animations/Player_Attack5_L.png"), pygame.image.load("Movement_Animations/Player_Sprite_L.png")]
  53. att_ani_R = [pygame.image.load("Attack_Animations/Player_Attack_R.png"), pygame.image.load("Attack_Animations/Player_Attack2_R.png"),
  54.              pygame.image.load("Attack_Animations/Player_Attack2_R.png"), pygame.image.load("Attack_Animations/Player_Attack3_R.png"),
  55.              pygame.image.load("Attack_Animations/Player_Attack3_R.png"), pygame.image.load("Attack_Animations/Player_Attack4_R.png"),
  56.              pygame.image.load("Attack_Animations/Player_Attack4_R.png"), pygame.image.load("Attack_Animations/Player_Attack5_R.png"),
  57.              pygame.image.load("Attack_Animations/Player_Attack5_R.png"), pygame.image.load("Movement_Animations/Player_Sprite_R.png")]
  58. # creates health animations
  59. health_ani = [pygame.image.load("Health_Animations/heart0.png"), pygame.image.load("Health_Animations/heart.png"),
  60.               pygame.image.load("Health_Animations/heart2.png"), pygame.image.load("Health_Animations/heart3.png"),
  61.               pygame.image.load("Health_Animations/heart4.png"), pygame.image.load("Health_Animations/heart5.png")]
  62.  
  63. # Functions for Background Class
  64. class Background(pygame.sprite.Sprite):
  65.     def __init__(self):
  66.         super().__init__()
  67.         self.bgimage = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/World Building Assets/Background.png")
  68.         self.rectBGimg = self.bgimage.get_rect()
  69.         self.bgY = 0
  70.         self.bgX = 0
  71. # Primary Game Render Functions
  72.     def render(self):
  73.         displaysurface.blit(self.bgimage, (self.bgX, self.bgY))
  74.  
  75. # Functions for Ground Class
  76. class Ground(pygame.sprite.Sprite):
  77.     def __init__(self):
  78.         super().__init__()
  79.         self.image = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/World Building Assets/ground.png")
  80.         self.rect = self.image.get_rect(center = (350, 350))
  81.  
  82. # Secondary Game Rendering Functions
  83.     def render(self):
  84.         displaysurface.blit(self.image, (self.rect.x, self.rect.y))
  85.  
  86. # Functions for Player Class
  87. class Player(pygame.sprite.Sprite):
  88.     def __init__(self):
  89.         super().__init__()
  90.         self.type = None
  91.         self.image = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/World Building Assets/Player_Sprite_R.png")
  92. # position of player to current target
  93.         self.rect = self.image.get_rect()
  94.         self.vx = 0
  95.         self.pos = vec((340, 240))
  96.         self.vel = vec(0,0)
  97.         self.acc = vec (0,0)
  98.         self.direction = "RIGHT"
  99. # movement of player
  100.         self.jumping = False
  101.         self.running = False
  102.         self.move_frame = 0
  103.  
  104. # combat of player
  105.         self.attacking = False
  106.         self.attack_frame = 0
  107.         self.cooldown = False
  108. # set attack animations in a loop
  109.         self.attack_frame = True
  110.         self.health = 5
  111.         self.mana = 0
  112.         self.experience = 0
  113.  
  114. # Functions for character movements
  115.     def move(self):
  116.         self.acc = vec(0,0.5)
  117.  
  118.         if abs(self.vel.x) > 0.3:
  119.             self.running = True
  120.         else:
  121.             self.running = False
  122.  
  123. # movement directions for player
  124.         pressed_keys = pygame.key.get_pressed()
  125.  
  126.         if pressed_keys[K_a]:
  127.             self.acc.x = -ACC
  128.         if pressed_keys[K_d]:
  129.             self.acc.x = ACC
  130.  
  131. # sets physics and velocity for movements
  132.         self.acc.x += self.vel.x * FRIC
  133.         self.vel += self.acc
  134.         self.pos += self.vel + 0.5 * self.acc
  135.  
  136. # warps the player from one point to another
  137.         if self.pos.x > WIDTH:
  138.             self.pos.x = 0
  139.  
  140.         if self.pos.x < 0:
  141.             self.pos.x = WIDTH
  142.  
  143.         self.rect.midbottom = self.pos
  144. # gravity (physics checks)
  145.     def gravity_check(self):
  146.         hits = pygame.sprite.spritecollide(player, ground_group, False)
  147.         if self.vel.y > 0:
  148.             if hits:
  149.                 lowest = hits[0]
  150.                 if self.pos.y < lowest.rect.bottom:
  151.                     self.pos.y = lowest.rect.top +1
  152.                     self.vel.y = 0
  153.                     self.jumping = False
  154.  
  155. # defines character updates to movements, jumps, attacks etc.
  156.     def update(self):
  157.         hit = pygame.sprite.spritecollide(self, Playergroup, False)
  158.         if self.move_frame > 6:
  159.            self.move_frame = 0
  160.            return
  161.         if self.jumping == False and self.running == True:
  162.             if self.vel.x > 0:
  163.                 self.image = run_ani_R[self.move_frame]
  164.                 self.direction = "RIGHT"
  165.             else:
  166.                 self.image = run_ani_L[self.move_frame]
  167.                 self.direction = "LEFT"
  168.             self.move_frame += 1
  169.  
  170. # checks if the player has picked up the correct item on screen
  171.         if hit:
  172.             if player.health < 5 and self.type == 1:
  173.                 player.health += 1
  174.  
  175.  
  176. # defines character attacks
  177.     def attack(self):
  178.  # Player Attack Loops
  179.         if player.attack_frame:
  180.  
  181.             if self.attack_frame > 5:
  182.                 self.attack_frame = 0
  183.                 self.attacking = False
  184.  
  185.             if self.direction == "LEFT":
  186.                 self.image = att_ani_L[self.attack_frame]
  187.             elif self.direction == "RIGHT":
  188.                 self.image = att_ani_R[self.attack_frame]
  189. # update the current attack
  190.             self.attack_frame += 1
  191.             player.attack_frame = False
  192.         else:
  193.             player.attack_frame = True
  194.  
  195. # defines character jump
  196.     def jump(self):
  197.         self.rect.x += 1
  198.  
  199. # checks if player is touching the ground
  200.         hits = pygame.sprite.spritecollide(self, ground_group, False)
  201.         self.rect.x -= 1
  202.  
  203. # if touching the ground, and not currently jumping, causes the player to jump
  204.         if hits and not self.jumping:
  205.             self.jumping = True
  206.             self.vel.y = -12
  207.  
  208. # Function for when the Player gets hit
  209.     def player_hit(self):
  210.         if self.cooldown == False:
  211.             self.cooldown = True
  212.             pygame.time.set_timer(hit_cooldown, 1000)
  213.             print("hit")
  214.             self.health = self.health - 1
  215.             health.image = health_ani[self.health]
  216.             if self.health <= 0:
  217.                 self.kill()
  218.                 pygame.display.update()
  219.  
  220. # Functions for Enemy Class
  221. class Enemy(pygame.sprite.Sprite):
  222.     def __init__(self):
  223.         super().__init__()
  224.         self.image = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/Enemy_Animations/Enemy.png")
  225.         self.rect = self.image.get_rect()
  226.         self.pos = vec(0,0)
  227.         self.vel = vec(0,0)
  228.         self.direction = random.randint(0,0)
  229.         self.mana = 0
  230.  
  231. # randomized the moment of the enemy
  232.         self.vel.x = random.randint(2,6)/2
  233.  
  234. # set the starting position of the enemy
  235. # starting position on the left side
  236.         if self.direction == 0:
  237.             self.pos.x = 0
  238.             self.pos.y = 265
  239. # starting position on the right side of the enemy
  240.         if self.direction == 1:
  241.             self.pos.x = 700
  242.             self.pos.y = 265
  243. # movement enemy to target
  244.     def move(self):
  245.         if self.pos.x >= (WIDTH - 20):
  246.             self.direction = 1
  247.         elif self.pos.x <= 0:
  248.             self.direction = 0
  249.         if self.direction == 0:
  250.             self.pos.x += self.vel.x
  251.         if self.direction == 1:
  252.             self.pos.x -= self.vel.x
  253.         # Update rectangle of enemy
  254.         self.rect.center = self.pos
  255.  
  256. # creates render for enemy character
  257.     def render(self):
  258.         displaysurface.blit(self.image, (self.pos.x, self.pos.y))
  259. # Checks for collision with the player
  260.     def update(self, f_hits = None):
  261.         hits = pygame.sprite.spritecollide(self, Playergroup, False)
  262.         if hits and player.attacking == True:
  263.             print("Enemy Killed")
  264.             self.kill()
  265.         elif hits and player.attacking == False:
  266.             player.player_hit()
  267.         rand_num = numpy.random.uniform(0, 100)
  268.  
  269. # Activates upon either of the two expressions being true
  270.         if hits and player.attacking == True or f_hits:
  271.             self.kill()
  272.         if player.mana < 100:
  273.             player.mana += self.mana
  274. # Release mana
  275. # Release experience
  276.         player.experience += 1
  277.         item_no = 0
  278.         if rand_num >= 0 and rand_num <= 5:
  279.             # 1 / 20 chance for an item (health) drop
  280.             item_no = 1
  281.         elif rand_num > 5 and rand_num <= 15:
  282.             item_no = 2
  283.         if item_no != 0:
  284.             # Add Item to Items group
  285.             item = itemDrops(item_no)
  286.             itemDrops.add(item_no)
  287. # Sets the item location to the location of the killed enemy
  288.             item.posx = self.pos.x
  289.             item.posy = self.pos.y
  290.  
  291.  
  292.  
  293. # Functions for Castle Class
  294. class Castle(pygame.sprite.Sprite):
  295.     def __init__(self):
  296.         super().__init__()
  297.         self.hide = False
  298.         self.image = pygame.image.load("RPG Assets-20221007T151405Z-001/RPG Assets/World Building Assets/castle.png")
  299.  
  300. # Creates Update Functions for hiding characters and animation
  301.     def update(self):
  302.         if self.hide == False:
  303.             displaysurface.blit(self.image, (250,90))
  304.  
  305. # Functions for event handler class
  306. class eventHandler():
  307.     def __init__(self):
  308.         self.enemy_count = 0
  309.         self.battle = False
  310.         self.stage = 1
  311.         self.enemy_generation = pygame.USEREVENT + 1
  312.         self.stage_enemies = []
  313.         for x in range(1, 21):
  314.             self.stage_enemies.append(int((x ** 2 / 2) + 2))
  315.  
  316. # creates the stage handle with Button Controls
  317.     def stage_handler(self):
  318.         self.root = Tk()
  319.         self.root.geometry('200x170')
  320.         button1 = Button(self.root, text = "Twilight Dungeon", width=18, height=2, command=self.world1)
  321.         button2 = Button(self.root, text = "Skyward Dungeon", width = 18, height = 2, command = self.world2)
  322.         button3 = Button(self.root, text = "Hall Dungeon", width = 18, height = 2, command = self.world3)
  323.         button1.place(x = 40, y = 15)
  324.         button2.place(x = 40, y = 65)
  325.         button3.place(x = 40, y = 115)
  326.         self.root.mainloop()
  327.  
  328. # creates world functions
  329.     def world1(self):
  330.             self.root.destroy()
  331.             pygame.time.set_timer(self.enemy_generation, 2000)
  332.             castle.hide = True
  333.             self.battle = True
  334.     def world2(self):
  335.  
  336.             self.battle = True
  337.     def world3(self):
  338.  
  339.             self.battle = True
  340.  
  341. # creates setup for next set of stages
  342.     def next_stage(self):
  343.         self.stage += 1
  344.         self.enemy_count = 0
  345.         print("Stage " + str(self.stage))
  346.         self.stage_enemies.append((int))
  347.         self.stage_enemies.append((int(self.stage)* 2) + 1)
  348.         pygame.time.set_timer(self.enemy_generation, 1500 - (50 * self.stage))
  349.  
  350. # creates a health bar for the player
  351. class HealthBar(pygame.sprite.Sprite):
  352.     def __init__(self):
  353.         super().__init__()
  354.         self.image = pygame.image.load("Health_Animations/heart5.png")
  355.  
  356. # creates Health Bar Render
  357.     def render (self):
  358.         displaysurface.blit(self.image, (10, 10))
  359.  
  360. class StageDisplay(pygame.sprite.Sprite):
  361.     def __init__(self):
  362.         super().__init__()
  363.         self.text = headingfont.render("STAGE: " + str(handler.stage), True, color_dark)
  364.         self.rect = self.text.get_rect()
  365.  
  366.         self.posx = -100
  367.         self.posy = 100
  368.         self.display = False
  369.  
  370. # Creates a movement function for display
  371.     def move_Display(self):
  372.         self.text = headingfont.render("STAGE: " + str(handler.stage), True, color_dark)
  373.         if self.posx < 700:
  374.             self.posx += 5
  375.             displaysurface.blit(self.text, (self.posx, self.posy))
  376.         else:
  377.             self.display = False
  378.         self.kill()
  379.  
  380. # creates item drop functions
  381. class itemDrops(pygame.sprite.Sprite):
  382.     def __init__(self, itemType):
  383.         super().__init__()
  384.         if itemType == 1:
  385.             self.image = pygame.image.load("Items/coin.png").convert_alpha()
  386.         elif itemType == 2:
  387.             self.image = pygame.image.load("Items/heart.png").convert_alpha()
  388.             self.rect = self.image.get_rect()
  389.             self.type = itemType
  390.             self.posx = 0
  391.             self.posy = 0
  392.  
  393. # creates render function for Item Type
  394.     def render(self):
  395.         self.rect.x = self.posx
  396.         self.rect.y = self.posy
  397.         displaysurface.blit(self.image, self.rect)
  398.  
  399. # Code to be activated if items comes in contact with player
  400.     def update(self, health = None):
  401.         hits = pygame.sprite.spritecollide(self, Playergroup, False)
  402.         if hits:
  403.             if player.health < 5 and self.type == 1:
  404.                 player.health += 1
  405.                 health.image = health_ani[player.health]
  406.                 self.kill()
  407.             if self.type == 2:
  408.                 # hander.money += 1
  409.                 self.kill()
  410.  
  411.         hit = pygame.sprite
  412. # Classes in their own variables (To use later)
  413. # Game Loop
  414. player = Player()
  415. castle = Castle()
  416. background = Background()
  417. ground = Ground()
  418. # itemDrops = pygame.sprite.Group()
  419. Playergroup = pygame.sprite.Group()
  420. Playergroup.add(player)
  421. stage_display = StageDisplay()
  422. # Ground collision detections
  423. ground_group = pygame.sprite.Group()
  424. ground_group.add(ground)
  425. hit_cooldown = pygame.USEREVENT + 1
  426. handler = eventHandler()
  427. Enemies = pygame.sprite.Group()
  428. health = HealthBar()
  429.  
  430. # while loops
  431. while True:
  432.     player.gravity_check()
  433.     player.update()
  434.     if player.attacking == True:
  435.         player.attack()
  436.     player.move()
  437.     for event in pygame.event.get():
  438.         if event.type == hit_cooldown:
  439.             player.cooldown = False
  440.             pygame.time.set_timer(hit_cooldown, 0)
  441.         # Will run when the close window button is clicked
  442.         if event.type == QUIT:
  443.             pygame.quit()
  444.             sys.exit()
  445.         if event.type == handler.enemy_generation:
  446.             if handler.enemy_count < handler.stage_enemies[handler.stage - 1]:
  447.                 print(handler.enemy_count)
  448.                 print(handler.stage_enemies[handler.stage - 1])
  449.                 enemy = Enemy()
  450.                 Enemies.add(enemy)
  451.                 handler.enemy_count += 1
  452.  
  453.  
  454. # For events that occur upon clicking the mouse (left click)
  455.         if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  456.             if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
  457.                 player.jump()
  458.  
  459.  
  460.         # Event handling for a range of different key presses
  461.         if event.type == pygame.KEYDOWN:
  462.             if event.key == pygame.K_n:
  463.                 if handler.battle == True and len(Enemies) == 0:
  464.                     handler.next_stage()
  465.                     stage_display = StageDisplay()
  466.                     stage_display.display = True
  467.             if event.key == pygame.K_e and 300 < player.rect.x < 400:
  468.                 print("At the door")
  469.                 handler.stage_handler()
  470.             if event.key == pygame.K_SPACE:
  471.                 player.jump()
  472.             if event.key == pygame.K_x:
  473.                 if player.attacking == False:
  474.                     player.attack()
  475.                     player.attacking = True
  476. # player variable access points
  477.     player.move()
  478. # render functions
  479.     background.render()
  480.  
  481.     ground.render()
  482.     castle.update()
  483.  
  484.     if player.health > 0:
  485.         displaysurface.blit(player.image, player.rect)
  486.     health.render()
  487.  
  488. # creates a display surface for the player
  489.     #displaysurface.blit(player.image, player.rect)
  490. # This is used to render the stage to display it as an image in the game
  491.     # if stage_display.display == True:
  492.         # stage_display.move_Display()
  493.     # for i in itemDrops:
  494.         # i.render()
  495.         # i.update()
  496. # creates and updates the clock and set FPS Performance and Enemy groups and other things too
  497.     for entity in Enemies:
  498.         entity.update()
  499.         entity.move()
  500.         entity.render()
  501.     pygame.display.update()
  502.     FPS_CLOCK.tick(FPS)
  503.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement