Advertisement
amlxv

tea

Jun 29th, 2022
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.87 KB | None | 0 0
  1. from pydoc import importfile
  2. from turtle import width
  3. import pygame
  4. from pygame.locals import *
  5. from pygame import mixer
  6.  
  7. pygame.init()
  8.  
  9. width = 1000
  10. height = 500
  11. window = pygame.display.set_mode((width, height))
  12. bg_img = pygame.image.load('bg.jpg')
  13. bg_img = pygame.transform.scale(bg_img, (width, height))
  14.  
  15. mixer.init()
  16. mixer.music.load('musicbg.wav')
  17. mixer.music.play()
  18.  
  19. walkRight = [pygame.image.load('images_lab/R1.png'), pygame.image.load('images_lab/R2.png'),
  20.              pygame.image.load(
  21.                  'images_lab/R3.png'), pygame.image.load('images_lab/R4.png'),
  22.              pygame.image.load(
  23.                  'images_lab/R5.png'), pygame.image.load('images_lab/R6.png'),
  24.              pygame.image.load(
  25.                  'images_lab/R7.png'), pygame.image.load('images_lab/R8.png'),
  26.              pygame.image.load('images_lab/R9.png')]
  27. walkLeft = [pygame.image.load('images_lab/L1.png'), pygame.image.load('images_lab/L2.png'),
  28.             pygame.image.load(
  29.                 'images_lab/L3.png'), pygame.image.load('images_lab/L4.png'),
  30.             pygame.image.load(
  31.                 'images_lab/L5.png'), pygame.image.load('images_lab/L6.png'),
  32.             pygame.image.load(
  33.                 'images_lab/L7.png'), pygame.image.load('images_lab/L8.png'),
  34.             pygame.image.load('images_lab/L9.png')]
  35.  
  36. char = pygame.image.load('images_lab/standing.png')
  37.  
  38. x = 50
  39. y = 290
  40.  
  41. person_width = 40
  42. person_height = 60
  43. vel = 5
  44.  
  45. clock = pygame.time.Clock()
  46.  
  47. isJump = False
  48. jumpCount = 10
  49.  
  50. left = False
  51. right = False
  52. walkCount = 0
  53.  
  54.  
  55. def redrawGameWindow():
  56.     global walkCount
  57.  
  58.     window.blit(bg_img, (0, 0))
  59.     if walkCount + 1 >= 27:
  60.         walkCount = 0
  61.  
  62.     if left:
  63.         window.blit(walkLeft[walkCount//3], (x, y))
  64.         walkCount += 1
  65.     elif right:
  66.         window.blit(walkRight[walkCount//3], (x, y))
  67.         walkCount += 1
  68.     else:
  69.         window.blit(char, (x, y))
  70.         walkCount = 0
  71.  
  72.     pygame.display.update()
  73.  
  74.  
  75. running = True
  76.  
  77. while running:
  78.     clock.tick(27)
  79.  
  80.     for event in pygame.event.get():
  81.         if event.type == pygame.QUIT:
  82.             running = False
  83.  
  84.     keys = pygame.key.get_pressed()
  85.  
  86.     if keys[pygame.K_LEFT] and x > vel:
  87.         x -= vel
  88.         left = True
  89.         right = False
  90.  
  91.     elif keys[pygame.K_RIGHT] and x < 760 - vel - person_width:
  92.         x += vel
  93.         left = False
  94.         right = True
  95.     else:
  96.         left = False
  97.         right = False
  98.         walkCount = 0
  99.  
  100.     if not(isJump):
  101.         if keys[pygame.K_SPACE]:
  102.             isJump = True
  103.             left = False
  104.             right = False
  105.             walkCount = 0
  106.  
  107.     else:
  108.         if jumpCount >= -10:
  109.             y -= (jumpCount * abs(jumpCount)) * 0.5
  110.             jumpCount -= 1
  111.         else:
  112.             jumpCount = 10
  113.             isJump = False
  114.  
  115.     redrawGameWindow()
  116.  
  117. pygame.quit()
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement