Advertisement
Guest User

Main.py

a guest
Jul 7th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.45 KB | None | 0 0
  1. import pygame
  2. from Player import player
  3. from Enemy import enemy
  4. from projectile import projectile
  5.  
  6. pygame.init()
  7. score = 0
  8. kills = 0
  9. win = pygame.display.set_mode((500, 480))
  10. keys = pygame.key.get_pressed()
  11. pygame.display.set_caption("First Game")
  12. bg = pygame.image.load('Game/bg.jpg')
  13. char = pygame.image.load('Game/standing.png')
  14.  
  15. clock = pygame.time.Clock()
  16.  
  17. def redrawGameWindow():
  18.     win.blit(bg, (0, 0))
  19.     text = font.render('Score: ' + str(score), 1, (0,0,0))
  20.     win.blit(text, (390,10))
  21.     man.draw(win)
  22.     goblin.draw(win)
  23.     for bullet in bullets:
  24.         bullet.draw(win)
  25.     pygame.display.update()
  26.  
  27. # mainloop
  28. man = player(200, 410, 64, 64)
  29. goblin = enemy(100, 410, 64, 64, 300)
  30. shootLoop = 0
  31. font = pygame.font.SysFont('comicsans', 30, True, False)
  32. bullets = []
  33. run = True
  34. while run:
  35.     clock.tick(27)
  36.  
  37.     if man.isDead:
  38.         run = False
  39.         print("You lost the game ;(")
  40.  
  41.     if goblin.visible:
  42.         if man.hitbox[1] < goblin.hitbox[1] + goblin.hitbox[3] and man.hitbox[1] + man.hitbox[3] > goblin.hitbox[1]:
  43.             if man.hitbox[0] + man.hitbox[2] > goblin.hitbox[0] and man.hitbox[0] < goblin.hitbox[0] + goblin.hitbox[2]:
  44.                 man.hit()
  45.                 score -= 2
  46.                 man.health -= 2
  47.  
  48.     if shootLoop > 0:
  49.         shootLoop+=1
  50.     if shootLoop > 3:
  51.         shootLoop = 0
  52.  
  53.     for event in pygame.event.get():
  54.         if event.type == pygame.QUIT:
  55.             run = False
  56.  
  57.     for bullet in bullets:
  58.         if goblin.visible:
  59.             if bullet.y - bullet.radius < goblin.hitbox[1] + goblin.hitbox[3] and bullet.y + bullet.radius > goblin.hitbox[1]:
  60.                 if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - bullet.radius < goblin.hitbox[0] + goblin.hitbox[2]:
  61.                     goblin.hit()
  62.                     score += 1
  63.                     if goblin.visible:
  64.                         bullets.pop(bullets.index(bullet))
  65.  
  66.         if bullet.x < 500 and bullet.x > 0:
  67.             bullet.x += bullet.vel
  68.         else:
  69.                 bullets.pop(bullets.index(bullet))
  70.  
  71.     if keys[pygame.K_SPACE] and shootLoop == 0:
  72.         if man.left:
  73.             facing = -1
  74.         else:
  75.             facing = 1
  76.  
  77.         if len(bullets) < 5:
  78.             bullets.append(
  79.                 projectile(round(man.x + man.width // 2), round(man.y + man.height // 2), 6, (0, 0, 0), facing))
  80.         shootLoop = 1
  81.  
  82.         if keys[pygame.K_LEFT] and man.x > man.vel:
  83.             man.x -= man.vel
  84.             man.left = True
  85.             man.right = False
  86.             man.standing = False
  87.         elif keys[pygame.K_RIGHT] and man.x < 500 - man.width - man.vel:
  88.             man.x += man.vel
  89.             man.right = True
  90.             man.left = False
  91.             man.standing = False
  92.         else:
  93.             man.standing = True
  94.             man.walkCount = 0
  95.  
  96.         if not (man.isJump):
  97.             if keys[pygame.K_UP]:
  98.                 man.isJump = True
  99.                 man.right = False
  100.                 man.left = False
  101.                 man.walkCount = 0
  102.         else:
  103.             if man.jumpCount >= -10:
  104.                 neg = 1
  105.                 if man.jumpCount < 0:
  106.                     neg = -1
  107.                 man.y -= (man.jumpCount ** 2) * 0.5 * neg
  108.                 man.jumpCount -= 1
  109.             else:
  110.                 man.isJump = False
  111.                 man.jumpCount = 10
  112.             man.jumpCount = 10
  113.  
  114.     redrawGameWindow()
  115.  
  116. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement