cookertron

UFO Python Pygame

Jan 23rd, 2021 (edited)
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. import os, sys, time, lzma, base64
  2. import pygame
  3.  
  4. png =   b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4ACxAKxdAESUBcR6J/b37omOUJCIs6rVUCYF0PNiLs+9xFYYB8Sul44ej\
  5.        96b+oKBBOpaT6rxFLCiqvfumffQaGjsBfQNxri9WeZzMTUNcsqJcwfuA6GxOwip+wELqVu9pBSSaxEANipkUacT/ArD\
  6.        ys+CZuPmU09pRgc6Ejm84LnSlyycN8atFjBfCpA/EpDZ4PjOIojaW5t18NqVRsCmxam6hM+az8qMx+3JsDIsxeiiz6A\
  7.        AoFP90XGhYFkAAcgBsgEAAJKNN7WxxGf7AgAAAAAEWVo='
  8.  
  9. if not os.path.exists("ufo.png"):
  10.     print("Writing title image...", end = "")
  11.     data = lzma.decompress(base64.b64decode(png))
  12.     r = open("UFO.png", "bw")
  13.     r.write(data)
  14.     r.close()
  15.     print("Done!")
  16.  
  17. def loadnscale(scale, filename):
  18.     i = pygame.image.load(filename)
  19.     r = i.get_rect()
  20.     ni = pygame.transform.scale(i, (r.w * scale, r.h * scale))
  21.     return ni, ni.get_rect()
  22.  
  23. pygame.init()
  24. PDR = pygame.Rect(0, 0, 1024, 800)
  25. PDCENTER = pygame.math.Vector2(PDR.center)
  26. PDS = pygame.display.set_mode(PDR.size)
  27. FPS = 120
  28.  
  29. BLACK = (0, 0, 0)
  30. SCALE = 4
  31.  
  32. ship_spritesheet, sss_rect = loadnscale(SCALE, "UFO.png")
  33. ship_spritesheet.set_colorkey(BLACK)
  34. ship_size = pygame.math.Vector2(sss_rect.midright)
  35. ship_pos = pygame.math.Vector2(PDCENTER - ship_size / 2)
  36. ship_sprites = [pygame.Rect((0, 0), ship_size), pygame.Rect((0, ship_size[1]), ship_size)]
  37. ship_sprite_id = 0
  38.  
  39. gravity = 0.1
  40. resistence = 0.05
  41.  
  42. velocity = pygame.math.Vector2()
  43. max_velocity = 5
  44. thrust = 0.2
  45.  
  46. while True:
  47.     events = pygame.event.get()
  48.     for e in events:
  49.         if e.type == pygame.QUIT: sys.exit()
  50.  
  51.     PDS.fill((0, 0, 0))
  52.     PDS.blit(ship_spritesheet, ship_pos, ship_sprites[ship_sprite_id])
  53.  
  54.  
  55.     pygame.display.update()
  56.  
  57.     ship_pos += velocity
  58.     velocity += (0, gravity)
  59.  
  60.     if ship_pos[0] < -ship_size[0]:
  61.         ship_pos[0] = PDR.right
  62.     if ship_pos[0] > PDR.right:
  63.         ship_pos[0] = -ship_size[0]
  64.     if ship_pos[1] < -ship_size[1]:
  65.         ship_pos[1] = PDR.bottom
  66.     if ship_pos[1] > PDR.bottom:
  67.         ship_pos[1] = -ship_size[1]
  68.  
  69.     k = pygame.key.get_pressed()
  70.     if k[pygame.K_RIGHT]:
  71.         velocity += (thrust, 0)
  72.     elif k[pygame.K_LEFT]:
  73.         velocity += (-thrust, 0)
  74.     else:
  75.         try:
  76.             velocity[0] -= velocity[0] // abs(velocity[0]) * resistence
  77.         except:
  78.             pass
  79.  
  80.     if k[pygame.K_UP]:
  81.         velocity += (0, -thrust)
  82.         ship_sprite_id = 1
  83.     else:
  84.         ship_sprite_id = 0
  85.  
  86.     if velocity[0] > max_velocity or velocity[0] < -max_velocity:
  87.         velocity[0] = velocity[0] // abs(velocity[0]) * max_velocity
  88.     if velocity[1] > max_velocity or velocity[1] < -max_velocity:
  89.         velocity[1] = velocity[1] // abs(velocity[1]) * max_velocity
  90.  
  91.     velocity[0] = round(velocity[0], 3)
  92.  
  93.     pygame.time.Clock().tick(FPS)
  94.  
  95. pygame.quit()
  96. sys.exit()
Add Comment
Please, Sign In to add comment