Advertisement
shh_algo_PY

Platformer - 30th April 2022

Apr 29th, 2022
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. import play
  2. import pygame
  3. from random import randint
  4.  
  5. play.set_backdrop('sky blue')
  6.  
  7. coin_sound = pygame.mixer.Sound('coin.wav')
  8. sea_sound = pygame.mixer.Sound('sea.ogg')
  9. pygame.display.set_caption('Platformer Game')
  10.  
  11. score_txt = play.new_text(words='Score:', x=play.screen.right-100, y=play.screen.top-30, size=70)
  12.  
  13. score = play.new_text(words='0', x=play.screen.right-30, y=play.screen.top-30, size=70)
  14. text = play.new_text(words='Tap SPACE to jump, LEFT/RIGHT to move', x=0, y=play.screen.bottom+60, size=70)
  15.  
  16. sprite = play.new_circle(color='white', x=play.screen.left+20, y=play.screen.top-20, border_color='grey', border_width=3, radius=15)
  17.  
  18. platforms = []
  19.  
  20. coins = []
  21.  
  22. sea = play.new_box(
  23.         color='blue', width=play.screen.width, height=50, x=0, y=play.screen.bottom+20
  24.     )
  25.  
  26. def draw_platforms():
  27.     platform1 = play.new_box(color='firebrick', border_width=1, border_color ='black', width=150, height=30, x=play.screen.left+70,y=play.screen.top-170)
  28.     platforms.append(platform1)
  29.  
  30.     platform2 = play.new_box(color='firebrick', border_width=1, border_color ='black', width= 100, height=30, x=play.screen.left+250,y=play.screen.top-170)
  31.     platforms.append(platform2)
  32.  
  33.     platform3 = play.new_box(color='firebrick', border_width=1, border_color ='black', width= 130, height=30, x=play.screen.left+400,y=play.screen.top-230)
  34.     platforms.append(platform3)
  35.  
  36.     platform4 = play.new_box(color='firebrick', border_width=1, border_color ='black', width= 100, height=30, x=play.screen.left+550,y=play.screen.top-120)
  37.     platforms.append(platform4)
  38.  
  39.     platform5 = play.new_box(color='firebrick', border_width=1, border_color ='black', width= 130, height=30, x=play.screen.left+670,y=play.screen.top-185)
  40.     platforms.append(platform5)
  41.  
  42.     for platform in platforms:
  43.         platform.start_physics(can_move=False, stable=True, obeys_gravity=False, mass=10)
  44.  
  45. def draw_coins():
  46.     coin1 = play.new_circle(color='yellow', x=play.screen.left+250, y=play.screen.top-140, radius=10)
  47.     coins.append(coin1)
  48.  
  49.     coin2 = play.new_circle(color='yellow', x=play.screen.left+550, y=play.screen.top-90, radius=10)
  50.     coins.append(coin2)
  51.  
  52.  
  53. @play.when_program_starts
  54. def start():
  55.     pygame.mixer_music.load('soundtrack.mp3')
  56.     pygame.mixer_music.play()
  57.  
  58.     sprite.start_physics(can_move=True, stable=False, obeys_gravity=True, mass=50, friction=1.0, bounciness=0.5)
  59.    
  60.     draw_platforms()
  61.     draw_coins()
  62.    
  63. @play.repeat_forever
  64.  
  65. async def game():
  66.  
  67.     for c in coins:
  68.         if c.is_touching(sprite):
  69.             coin_sound.play()
  70.             sprite.physics.y_speed = -1 *sprite.physics.y_speed
  71.             coins.remove(c)
  72.             c.hide()
  73.             score.words=str(int(score.words) + 1)
  74.  
  75.     if sprite.is_touching(sea):
  76.         sea_sound.play()
  77.         sprite.hide()
  78.         await play.timer(seconds=2.0)
  79.         quit()
  80.    
  81.     if play.key_is_pressed('right'):
  82.         sprite.physics.x_speed = 10
  83.     elif play.key_is_pressed('left'):
  84.         sprite.physics.x_speed = -10
  85.     elif play.key_is_pressed('space'):
  86.         sprite.physics.y_speed = 50
  87.         await play.timer(seconds=1)
  88.         sprite.physics.y_speed = 0
  89.     else:
  90.         sprite.physics.x_speed=0
  91.  
  92.     await play.timer(seconds=1/48)
  93.  
  94.  
  95. play.start_program()
  96.  
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement