Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, sys, time, lzma, base64
- import pygame
- png = b'/Td6WFoAAATm1rRGAgAhARYAAAB0L+Wj4ACxAKxdAESUBcR6J/b37omOUJCIs6rVUCYF0PNiLs+9xFYYB8Sul44ej\
- 96b+oKBBOpaT6rxFLCiqvfumffQaGjsBfQNxri9WeZzMTUNcsqJcwfuA6GxOwip+wELqVu9pBSSaxEANipkUacT/ArD\
- ys+CZuPmU09pRgc6Ejm84LnSlyycN8atFjBfCpA/EpDZ4PjOIojaW5t18NqVRsCmxam6hM+az8qMx+3JsDIsxeiiz6A\
- AoFP90XGhYFkAAcgBsgEAAJKNN7WxxGf7AgAAAAAEWVo='
- if not os.path.exists("ufo.png"):
- print("Writing title image...", end = "")
- data = lzma.decompress(base64.b64decode(png))
- r = open("UFO.png", "bw")
- r.write(data)
- r.close()
- print("Done!")
- def loadnscale(scale, filename):
- i = pygame.image.load(filename)
- r = i.get_rect()
- ni = pygame.transform.scale(i, (r.w * scale, r.h * scale))
- return ni, ni.get_rect()
- pygame.init()
- PDR = pygame.Rect(0, 0, 1024, 800)
- PDCENTER = pygame.math.Vector2(PDR.center)
- PDS = pygame.display.set_mode(PDR.size)
- FPS = 120
- BLACK = (0, 0, 0)
- SCALE = 4
- ship_spritesheet, sss_rect = loadnscale(SCALE, "UFO.png")
- ship_spritesheet.set_colorkey(BLACK)
- ship_size = pygame.math.Vector2(sss_rect.midright)
- ship_pos = pygame.math.Vector2(PDCENTER - ship_size / 2)
- ship_sprites = [pygame.Rect((0, 0), ship_size), pygame.Rect((0, ship_size[1]), ship_size)]
- ship_sprite_id = 0
- gravity = 0.1
- resistence = 0.05
- velocity = pygame.math.Vector2()
- max_velocity = 5
- thrust = 0.2
- while True:
- events = pygame.event.get()
- for e in events:
- if e.type == pygame.QUIT: sys.exit()
- PDS.fill((0, 0, 0))
- PDS.blit(ship_spritesheet, ship_pos, ship_sprites[ship_sprite_id])
- pygame.display.update()
- ship_pos += velocity
- velocity += (0, gravity)
- if ship_pos[0] < -ship_size[0]:
- ship_pos[0] = PDR.right
- if ship_pos[0] > PDR.right:
- ship_pos[0] = -ship_size[0]
- if ship_pos[1] < -ship_size[1]:
- ship_pos[1] = PDR.bottom
- if ship_pos[1] > PDR.bottom:
- ship_pos[1] = -ship_size[1]
- k = pygame.key.get_pressed()
- if k[pygame.K_RIGHT]:
- velocity += (thrust, 0)
- elif k[pygame.K_LEFT]:
- velocity += (-thrust, 0)
- else:
- try:
- velocity[0] -= velocity[0] // abs(velocity[0]) * resistence
- except:
- pass
- if k[pygame.K_UP]:
- velocity += (0, -thrust)
- ship_sprite_id = 1
- else:
- ship_sprite_id = 0
- if velocity[0] > max_velocity or velocity[0] < -max_velocity:
- velocity[0] = velocity[0] // abs(velocity[0]) * max_velocity
- if velocity[1] > max_velocity or velocity[1] < -max_velocity:
- velocity[1] = velocity[1] // abs(velocity[1]) * max_velocity
- velocity[0] = round(velocity[0], 3)
- pygame.time.Clock().tick(FPS)
- pygame.quit()
- sys.exit()
Add Comment
Please, Sign In to add comment