Darkwater

Untitled

Dec 13th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. import os, sys
  2. import math
  3. import pygame
  4. import json
  5. from pygame.locals import *
  6. from pygame.gfxdraw import *
  7. from pygame.key import *
  8.  
  9. pygame.init()
  10. screen = pygame.display.set_mode((1024, 768))
  11. pygame.mouse.set_visible(0)
  12.  
  13. clock = pygame.time.Clock()
  14.  
  15. bat_x = 512 # 1024 / 2
  16. bat_y = 700
  17. bat_w = 150
  18. bat_h =  30
  19.  
  20. bat_speed_max = 10
  21. bat_speed_acc = 2
  22. bat_speed_cur = 0
  23.  
  24. running = True
  25. while running:
  26.     clock.tick(24) # Simulate Raspberry Pi's low fps
  27.  
  28.     keys = pygame.key.get_pressed()
  29.  
  30.     if keys[K_LEFT]:
  31.         bat_speed_cur -= bat_speed_acc
  32.     elif keys[K_RIGHT]:
  33.         bat_speed_cur += bat_speed_acc
  34.     else:
  35.         bat_speed_cur *= 0.4
  36.  
  37.     if bat_speed_cur < -bat_speed_max:
  38.         bat_speed_cur = -bat_speed_max
  39.     elif bat_speed_cur > bat_speed_max:
  40.         bat_speed_cur = bat_speed_max
  41.     elif abs(bat_speed_cur) < 0.1:
  42.         bat_speed_cur = 0
  43.  
  44.     bat_x += bat_speed_cur
  45.     bat_x = round(bat_x)
  46.  
  47.     background = pygame.Surface(screen.get_size())
  48.     background = background.convert()
  49.     background.fill((200, 200, 200))
  50.  
  51.     pygame.gfxdraw.box(background, (10, 10, 1004, 748), (100, 0, 0))
  52.  
  53.     pygame.gfxdraw.box(background, (bat_x - bat_w/2, bat_y, bat_w, bat_h), (200, 200, 200))
  54.  
  55.  
  56.     if pygame.font:
  57.         font = pygame.font.Font(None, 36)
  58.         text = font.render(json.dumps([bat_speed_max, bat_speed_acc, bat_speed_cur]), 1, (200, 200, 200))
  59.         background.blit(text, (20, 20))
  60.  
  61.  
  62.     screen.blit(background, (0, 0))
  63.     pygame.display.flip()
  64.  
  65.     for event in pygame.event.get():
  66.         if event.type == QUIT:
  67.             running = False
  68.         elif event.type == KEYDOWN and event.key == K_ESCAPE:
  69.             running = False
Advertisement
Add Comment
Please, Sign In to add comment