pizzaholic81

Pygame Test

Apr 3rd, 2017
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 20.10 KB | None | 0 0
  1. #**************************************************************************
  2. #   IMPORTS
  3. #**************************************************************************
  4.  
  5. import pygame
  6. from pygame.locals import *
  7. import os
  8. import sys
  9. import math
  10. from math import sqrt
  11. import time
  12. import random
  13. from random import randint
  14.  
  15. #**************************************************************************
  16. #   COLORS
  17. #**************************************************************************
  18.  
  19. """
  20. FIREBRICK = (178, 34, 34)
  21. SEASHELL = (255, 245, 238)
  22. ROYAL_BLUE = (65, 105, 225)
  23. WHITE_SMOKE = (245, 245, 245)
  24. WHITE = (255, 255, 255)
  25. PURPLE = (186, 85, 211)
  26. PAPAYA_WHIP = (255, 239, 213)
  27. BISQUE_2 = (238, 213, 183)
  28. BISQUE_3 = (205, 183, 158)
  29. BISQUE_4 = (139, 125, 107)
  30. HONEY_DEW = (131, 139, 131)
  31. """
  32. BLACK = (0, 0, 0)
  33. DIM_GRAY = (105, 105, 105)
  34. DEEP_RED = (165, 42, 42)
  35. DARK_ORANGE = (255, 140, 0)
  36. LIME_GREEN = (50, 205, 50)
  37.  
  38. #*************************************************************************
  39. #   SYSTEM SETTINGS
  40. #*************************************************************************
  41.  
  42. pygame.init()
  43. #Screen Dimensions
  44. SCREEN_WIDTH = 800
  45. SCREEN_HEIGHT = 500
  46. PLAYABLE_SCREEN_WIDTH = SCREEN_WIDTH - 100
  47. PLAYABLE_SCREEN_HEIGHT = SCREEN_HEIGHT - 100
  48. screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  49. #background_image = 'res' + os.sep + 'images' + os.sep + 'bg-level-1-1-1.jpg'
  50. #pygame.image.load(os.path.join("res","images","bg-level-1-1-1.jpg")).convert()
  51. #bg = pygame.image.load("bg-level-1-1-1.jpg").convert()
  52. #screen.blit(pygame.Surface, [0, 0])
  53.  
  54. clock = pygame.time.Clock()
  55. Score = 0
  56.  
  57. font_hero_name = pygame.font.SysFont("monospace", 15)
  58.  
  59. """
  60. **************************************************************************
  61.    SPRITE GROUPS
  62. **************************************************************************
  63. """
  64. heroes_list = pygame.sprite.Group()
  65. enemy_list = pygame.sprite.Group()
  66. near_hero_list = pygame.sprite.Group()
  67. all_sprites_list = pygame.sprite.Group()
  68.  
  69. """
  70. **************************************************************************
  71.    CREATING THE HERO CLASS
  72.  
  73.    Values: NAME, SPEED, LEVEL, HP, STAMINA, FEAR
  74.    Format: player = Hero(str, int, int, int, int, int)
  75.  
  76.    Example:
  77.    heroBoonrit = Hero("Boonrit", 30, 1, 10000, 50, 20)
  78. **************************************************************************
  79. """
  80. class Hero(pygame.sprite.Sprite):
  81.     def __init__(self, name, level, speed, hp, stamina, fear, blocking, jumping_cooldown, attacking_cooldown, held_cooldown, knockdown_cooldown, stun_cooldown, jumping_countdown, attacking_countdown, held_countdown, knockdown_countdown, stun_countdown, grabbed_cooldown, grabbed_countdown):
  82.         super().__init__()
  83.         self.image = pygame.Surface([30,20])
  84.         #self.image.fill(color)
  85.         self.rect = self.image.get_rect()
  86.         self.surf = pygame.Surface((30,20))
  87.         self.name = name
  88.         self.speed = speed
  89.         self.level = level
  90.         self.hp = hp
  91.         self.stamina = stamina
  92.         self.fear = fear
  93.         self.blocking = blocking
  94.         self.jumping_cooldown = jumping_cooldown
  95.         self.jumping_countdown = jumping_countdown
  96.         self.attacking_cooldown = attacking_cooldown
  97.         self.attacking_countdown = attacking_countdown
  98.         self.held_cooldown = held_cooldown
  99.         self.held_countdown = held_countdown
  100.         self.knockdown_cooldown = knockdown_cooldown
  101.         self.knockdown_countdown = knockdown_countdown
  102.         self.stun_cooldown = stun_cooldown
  103.         self.stun_countdown = stun_countdown
  104.  
  105.         blocking = False
  106.         jumping_cooldown = False
  107.         jumping_coutndown = 0
  108.         attacking_cooldown = False
  109.         attacking_countdown = 0
  110.         held_cooldown = False
  111.         held_countdown = 0
  112.         knockdown_cooldown = False
  113.         knockdown_countdown = 0
  114.         stun_cooldown = False
  115.         stun_countdown = 0
  116.    
  117.     def update(self, pressed_keys):
  118.  
  119.         if self.jumping_cooldown == True:
  120.             if self.jumping_countdown <= 0:
  121.                 self.jumping_cooldown == False
  122.                 self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-3.png'))
  123.             elif self.jumping_countdown <= 20:
  124.                 self.rect.y += 3
  125.             else:
  126.                 self.rect.y -= 3
  127.                 self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-2.png'))
  128.                
  129.         if self.attacking_cooldown == True:
  130.             self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-attack-1.png'))
  131.             self.attacking_countdown -= 1
  132.             if self.attacking_countdown <= 0:
  133.                 self.attacking_countdown = 0
  134.                 self.attacking_cooldown = False
  135.                 self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-3.png'))
  136.        
  137.         if self.stun_cooldown == True:
  138.             self.stun_countdown -= 1
  139.             if self.held_cooldown == True:
  140.                 self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-held-stun.png'))
  141.             elif self.jumping_cooldown == True:
  142.                 self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-jumping-stun.png'))
  143.             elif self.knockdown_cooldown == True:
  144.                 self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-knockdown-stun.png'))
  145.             else:
  146.                 self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-stun.png'))                
  147.             if self.stun_countdown < 0:
  148.                 self.stun_countdown = 0
  149.                 self.stun_cooldown = False
  150.                 self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-3.png'))
  151.  
  152.         if self.held_cooldown == True:
  153.             self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-held.png'))
  154.             self.held_countdown -= 1
  155.             if self.held_countdown <= 0:
  156.                 self.held_countdown = 0
  157.                 self.held_cooldown == False
  158.                 self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-3.png'))
  159.  
  160.         if self.blocking == True:
  161.             self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-block.png'))
  162.        
  163.         # Jumping
  164.             #self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-jump.png'))
  165.  
  166.         # If hero isn't blocking, attacking or being stunned, then s(he) is able to move
  167.         if self.blocking == False and self.attacking_cooldown == False and self.stun_cooldown == False and self.held_cooldown == False:
  168.             if pressed_keys[K_LEFT]:
  169.                 self.rect.move_ip(-heroBoonrit.speed, 0)
  170.                
  171.             if pressed_keys[K_RIGHT]:
  172.                 self.rect.move_ip(heroBoonrit.speed, 0)
  173.                
  174.             if pressed_keys[K_UP]:
  175.                 self.rect.move_ip(0, -heroBoonrit.speed)
  176.                
  177.             if pressed_keys[K_DOWN]:
  178.                 self.rect.move_ip(0, heroBoonrit.speed)
  179.                
  180.             if pressed_keys[K_d]:
  181.                 self.attacking_cooldown = True
  182.                 self.attacking_countdown = 20
  183.            
  184.             if pressed_keys[K_s]:
  185.                 self.jumping_cooldown = True
  186.                 self.jumping_countdown = 40
  187.  
  188.             if pressed_keys[K_a]:
  189.                 self.blocking = True
  190.  
  191.         # Stop the player from moving too far off the screen
  192.  
  193.         # Health
  194.         if self.hp <= 0:
  195.             self.kill()
  196.         elif self.hp <= 20:
  197.             #self.image.fill(DEEP_RED)
  198.             self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-1.png'))
  199.         elif self.hp <= 60:
  200.             #self.image.fill(DARK_ORANGE)
  201.             self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-2.png'))
  202.         elif self.hp <= 90:
  203.             #self.image.fill(LIME_GREEN)
  204.             self.image = pygame.image.load(os.path.join('res','img','chars','boonrit-3.png'))
  205.         else:
  206.             pass
  207.  
  208. # Active character
  209. ac = ["Boonrit", "Hugo", "Joy", "Victoria", "Kelly"]
  210.  
  211. """
  212. **************************************************************************
  213.    CREATING THE ENEMY
  214.    
  215.    Values: COLOR, SPEED, LEVEL, HP, STAMINA, FEAR
  216.    Format: Shit_Clown = Enemy(str, int, int, int, int, int)
  217.    Example:
  218.    Shit_Clown = Enemy("Shit Clown",30,1,10000,50,20)
  219.    
  220. **************************************************************************
  221. """
  222.  
  223. class Enemy(pygame.sprite.Sprite):
  224.     def __init__(self, name, level, speed, hp, stamina, fear, blocking_cooldown, jumping_cooldown, attacking_cooldown, held_cooldown, knockdown_cooldown, stun_cooldown, jumping_countdown, attacking_countdown, held_countdown, knockdown_countdown, stun_countdown, grabbing_cooldown, grabbing_countdown, blocking_countdown):
  225.         super().__init__()
  226.         self.image = pygame.image.load(os.path.join('res','img','chars','shit_clown-3.png'))
  227.         #self.image = pygame.Surface([30,20])
  228.         #self.image.fill(color)
  229.         self.rect = self.image.get_rect()
  230.         self.surf = pygame.Surface((30,20))
  231.         self.name = name
  232.         self.speed = speed
  233.         self.level = level
  234.         self.hp = hp
  235.         self.stamina = stamina
  236.         self.fear = fear
  237.         self.blocking_cooldown = blocking_cooldown
  238.         self.jumping_cooldown = jumping_cooldown
  239.         self.jumping_countdown = jumping_countdown
  240.         self.attacking_cooldown = attacking_cooldown
  241.         self.attacking_countdown = attacking_countdown
  242.         self.held_cooldown = held_cooldown
  243.         self.held_countdown = held_countdown
  244.         self.knockdown_cooldown = knockdown_cooldown
  245.         self.knockdown_countdown = knockdown_countdown
  246.         self.stun_cooldown = stun_cooldown
  247.         self.stun_countdown = stun_countdown
  248.         self.grabbing_cooldown = grabbing_cooldown
  249.         self.grabbing_countdown = grabbing_countdown
  250.         self.blocking_countdown = blocking_countdown
  251.        
  252.         blocking_cooldown = False
  253.         jumping_cooldown = False
  254.         jumping_coutndown = 0
  255.         attacking_cooldown = False
  256.         attacking_countdown = 0
  257.         held_cooldown = False
  258.         held_countdown = 0
  259.         knockdown_cooldown = False
  260.         knockdown_countdown = 0
  261.         stun_cooldown = False
  262.         stun_countdown = 0
  263.         grabbing_cooldown = False
  264.         grabbing_countdown = 0
  265.         blocking_countdown = 0
  266.  
  267.     def update(self):
  268.        
  269.        
  270.        
  271.         #if blocking_cooldown == False and held_cooldown == False and knockdown_cooldown == False and stun_cooldown == False and grabbing_cooldown == False:
  272.             # Flip the coin.
  273.             #coin_flip = random.randrange(1,4)
  274.             #if coin_flip == 1:
  275.                 #timer -= 1
  276.                 #self.rect.move_ip(0, 0)
  277.                 #if timer <= 0
  278.         #elif coinFlip == 2:
  279.             #self.rect.move_ip(-1, 0)        
  280.         #else:
  281.             #self.rect.move_ip(1, 0)
  282.        
  283.         #if self.attacking_cooldown == True:
  284.             #self.attacking_countdown -= 1
  285.             #if self.attacking_countdown <= 0:
  286.                 #self.attacking_countdown = 0
  287.                 #self.attacking_cooldown = False
  288.        
  289.         if self.hp <= 0:
  290.             self.kill()
  291.         elif self.hp <= 20:
  292.             self.image.fill(DEEP_RED)
  293.         elif self.hp <= 60:
  294.             self.image.fill(DARK_ORANGE)
  295.         elif self.hp <= 90:
  296.             self.image.fill(LIME_GREEN)
  297.         else:
  298.             pass
  299.    
  300.     # stateIDLE will be the behaviour of the enemy when they are not aware of the hero.
  301.     def stateIDLE():
  302.         pass
  303.    
  304.     # stateIDLE will be the behaviour of the enemy when they are aware of the hero.
  305.     def stateAWARE():
  306.         pass
  307.  
  308.     # stateIDLE will be the behaviour of the enemy when they are aggressive.
  309.     def stateAGGRESSIVE():
  310.         pass
  311.  
  312.     # stateIDLE will be the behaviour of the enemy when they fleeing from the hero.
  313.     def stateFLEE():
  314.         pass
  315.  
  316. """
  317. **************************************************************************
  318.    SPAWNING THE HERO
  319.    Outdate Values: name, speed, level, hp, stamina, fear, blocking, jumping_cooldown, attacking_cooldown, held_cooldown, knockdown_cooldown, jumping_countdown, attacking_countdown, held_countdown, knockdown_countdown
  320. **************************************************************************
  321. """
  322. # Boonrit, Male, Thailand
  323. heroBoonrit = Hero("Boonrit",1,3,10000,50,20,False,False,False,False,False,False,False,0,0,0,0,0,0)
  324. #Name
  325. #Level
  326. #Speed
  327. #HP
  328. #Stamina
  329. #Fear
  330. #Blocking
  331. #Jumping Cooldown
  332. #Attacking Cooldown
  333. #Held Cooldown
  334. #Knockdown Cooldown
  335. #Stun Cooldown
  336. #Grabbed Cooldown
  337. #Jumping Countdown
  338. #Attacking Countdown
  339. #Held Countdown
  340. #Knockdown Countdown
  341. #Stun Countdown
  342. #Grabbed Countdown
  343. heroBoonrit.image = pygame.image.load(os.path.join('res','img','chars','boonrit-3.png')).convert()
  344. heroBoonrit.rect.x = 400
  345. heroBoonrit.rect.y = 400
  346. heroes_list.add(heroBoonrit)
  347. all_sprites_list.add(heroBoonrit)
  348.  
  349. """
  350. **************************************************************************
  351.    SPAWNING ENEMIES
  352.    Values: name, level, speed, hp, stamina, fear, blocking_cooldown, jumping_cooldown, attacking_cooldown, held_cooldown, knockdown_cooldown, stun_cooldown, jumping_countdown, attacking_countdown, held_countdown, knockdown_countdown, stun_countdown, grabbing_cooldown, grabbing_countdown, blocking_countdown
  353. **************************************************************************
  354. """
  355.  
  356. # Spawn 3 Shit Clowns
  357. #def __init__(self, name, level, speed, hp, stamina, fear, blocking_cooldown, jumping_cooldown, attacking_cooldown, held_cooldown, knockdown_cooldown, stun_cooldown, jumping_countdown, attacking_countdown, held_countdown, knockdown_countdown, stun_countdown, grabbing_cooldown, grabbing_countdown, blocking_countdown):
  358. for i in range(1):
  359.     enemyShit_Clown = Enemy("Shit Clown", 1, 4, 1000, 10, 90, False, False, False, False, False, False, 0, 0, 0, 0, 0, True, 60, 0)
  360.     enemyShit_Clown.image = pygame.image.load(os.path.join('res','img','chars','shit_clown-3.png')).convert()
  361.     enemyShit_Clown.rect.x = 300 #random.randrange(300, 400)
  362.     enemyShit_Clown.rect.y =  300 #random.randrange(200, 400)
  363.     enemy_list.add(enemyShit_Clown)
  364.     all_sprites_list.add(enemyShit_Clown)
  365.  
  366. # Spawn 2 Jack Scrappers
  367. for i in range(0):
  368.     enemyJack_Scrapper = Enemy("Jack Scrapper "+str(i), 1, 4, 2000, 15, 85, False, False, False, False, False, False, 0, 0, 0, 0, 0, 0, 0)
  369.     enemyJack_Scrapper.image = pygame.image.load(os.path.join('res','img','chars','jack_scrapper-3.png')).convert()
  370.     enemyJack_Scrapper.rect.x = 100 #random.randrange(600, 700)
  371.     enemyJack_Scrapper.rect.y = 100 #random.randrange(200, 400)
  372.     enemy_list.add(enemyJack_Scrapper)
  373.     all_sprites_list.add(enemyJack_Scrapper)
  374.  
  375. """
  376. **************************************************************************
  377.    MAIN GAME LOOP
  378. **************************************************************************
  379. """
  380. #screen.blit(heroBoonrit.surf, (30,20))
  381. running = True
  382. while running:
  383.    
  384.     for event in pygame.event.get():
  385.         if event.type == pygame.QUIT:
  386.             running = False
  387.    
  388.     screen.fill(BLACK)
  389.     pygame.draw.rect(screen, DIM_GRAY, (100, 20, 250, 30))
  390.     pygame.draw.rect(screen, DIM_GRAY, (100, 55, 250, 5))
  391.     #pygame.font.Font.render("Boonrit")
  392.     pressed_keys = pygame.key.get_pressed()
  393.     heroBoonrit.update(pressed_keys)
  394.     enemy_list.update()
  395.     near_hero_list.update()
  396.    
  397.     contact = pygame.sprite.spritecollide(heroBoonrit, enemy_list, False)
  398.  
  399.     for villan in contact:
  400.         for villan in enemy_list:
  401.             if villan.grabbing_cooldown:
  402.                 heroBoonrit.held_cooldown = True
  403.                 heroBoonrit.rect.y = villan.rect.y
  404.                 heroBoonrit.rect.x = (villan.rect.x + 30)
  405.                 heroBoonrit.held_countdown = villan.grabbing_countdown
  406.                 villan.grabbing_countdown -= 1
  407.                 heroBoonrit.held_countdown -= 1
  408.                 if villan.grabbing_countdown <= 0:
  409.                     villan.grabbing_countdown = 0
  410.                     heroBoonrit.held_countdown = 0
  411.                     villan.grabbing_cooldown = False
  412.                     heroBoonrit.held_cooldown = False
  413.                     heroBoonrit.rect.x += 30
  414.             elif villan.attacking_cooldown:
  415.                 if heroBoonrit.blocking != True:
  416.                     heroBoonrit.hp -= 100
  417.                 else:
  418.                     heroBoonrit.hp -= 10
  419.    
  420.     if heroBoonrit.attacking_cooldown:
  421.         for villan in enemy_list:
  422.             for villan in contact:
  423.                 if villan.blocking_cooldown != True:
  424.                     villan.hp -= 100
  425.                 else:
  426.                     villan.hp -= 10
  427.    
  428.     #for villan in enemy_list:
  429.         #if (abs(villan.rect.x) - abs(heroBoonrit.rect.x) <= 10) and (abs(villan.rect.y) - abs(heroBoonrit.rect.y) <= 10):
  430.             #near_hero_list.add(villan)
  431.         #if (abs(villan.rect.x) - abs(heroBoonrit.rect.x) > 100) and (abs(villan.rect.y) - abs(heroBoonrit.rect.y) > 10):
  432.             #near_hero_list.remove(villan)
  433.         #if math.sqrt(((villan.rect.x - heroBoonrit.rect.x) **2) + ((villan.rect.x - heroBoonrit.rect.x) **2)) <= 10 and math.sqrt(((villan.rect.y - heroBoonrit.rect.y) **2) + ((villan.rect.y - heroBoonrit.rect.y) **2)) <= 100:
  434.             #near_hero_list.add(villan)
  435.         #if math.sqrt(((villan.rect.x - heroBoonrit.rect.x) **2) + ((villan.rect.x - heroBoonrit.rect.x) **2)) > 10 and math.sqrt(((villan.rect.y - heroBoonrit.rect.y) **2) + ((villan.rect.y - heroBoonrit.rect.y) **2)) > 100:
  436.             #near_hero_list.add(villan)
  437.         #contact = pygame.sprite.spritecollide(heroBoonrit, enemy_list, False)
  438.         #if heroBoonrit.attacking_cooldown:
  439.             #for villan in contact:
  440.                 #if villan.blocking != True:
  441.                     #villan.hp -= 100
  442.                 #else:
  443.                     #villan.hp -= 10
  444.  
  445.    
  446.     #screen.blit(bg, (0, 0))
  447.     all_sprites_list.draw(screen)
  448.     pygame.display.flip()
  449.     clock.tick(60)
  450.  
  451. #*************************************************************************
  452. #   DIAGNOSTICS
  453. #*************************************************************************
  454.  
  455.     #print("|Boonrit HP| ", heroBoonrit.hp)
  456.     print("||Shit Clown|| Grabbing = ", enemyShit_Clown.grabbing_cooldown, " Countdown = ", enemyShit_Clown.grabbing_countdown, "||Boonrit|| Grabbed = ", heroBoonrit.held_cooldown, " Countdown = ", heroBoonrit.held_countdown)
  457.     #print("|Clown HP| ", enemyShit_Clown.hp, "|Boonrit HP| ", heroBoonrit.hp, enemyShit_Clown.grabbing_cooldown, heroBoonrit.held_cooldown)
  458.     #print(" |Score| ", Score)
  459.     #print(" |J| ", heroBoonrit.jumping_cooldown)
  460.     #print(" |A| ", heroBoonrit.attacking_cooldown)
  461.     #print(heroBoonrit.attacking_countdown)
  462.     #print(" |H| " , heroBoonrit.held_cooldown)
  463.     #print(" |K| ", heroBoonrit.knockdown_cooldown)
  464.     #print(" |S| ", heroBoonrit.stun_cooldown)
  465.     #print("|Clown HP| ", enemyShit_Clown.hp)
  466.     #print(enemyShit_Clown.image.get_rect())
  467.     #print(enemyShit_Clown.rect)
  468.     #print(heroBoonrit.rect)
  469.     #print(contact)
  470.     #print(enemy_list)
  471.     #print(all_sprites_list)
  472.     #print(heroes_list)
  473.     #for i in enemy_list:
  474.     #    print(i.name, "Grabbing: ", i.grabbing_cooldown)
  475.     #for i in near_hero_list:
  476.     #    print(i.name, "Grabbing: ", i.grabbing_cooldown)
  477.     #for i in enemy_list:
  478.          #print("|Delta X| ", (abs(villan.rect.x) - abs(heroBoonrit.rect.x) <= 100), "|Delta Y| ", (abs(villan.rect.y) - abs(heroBoonrit.rect.y) <= 10))
  479.     #for i in near_hero_list:
  480.         #print(i.name)
  481.     #print(heroBoonrit.image.get_rect())
  482.  
  483. pygame.QUIT
  484.  
  485. """
  486.    #See if the player has collided with anything
  487.    #enemy_hit_list = pygame.sprite.spritecollide(player, enemy_list, True)
  488.    #for enemy in enemy_hit_list:
  489.    #    player.hp -= 10
  490.    #    print(player.hp)
  491.    #    #Stun the player
  492.    #    player_stun = 3
  493.    #    pygame.mixer.Sound('res\audio\oof.wav').play()
  494.    #    while player_stun > 0:
  495.    #        player_stun -= 1
  496.    #        pygame.time.delay(300)
  497. """
Advertisement
Add Comment
Please, Sign In to add comment