Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os, sys
- import math
- import pygame
- import json
- from pygame.locals import *
- from pygame.gfxdraw import *
- from pygame.key import *
- pygame.init()
- screen = pygame.display.set_mode((1024, 768))
- pygame.mouse.set_visible(0)
- clock = pygame.time.Clock()
- bat_x = 512 # 1024 / 2
- bat_y = 700
- bat_w = 150
- bat_h = 30
- bat_speed_max = 10
- bat_speed_acc = 2
- bat_speed_cur = 0
- running = True
- while running:
- clock.tick(24) # Simulate Raspberry Pi's low fps
- keys = pygame.key.get_pressed()
- if keys[K_LEFT]:
- bat_speed_cur -= bat_speed_acc
- elif keys[K_RIGHT]:
- bat_speed_cur += bat_speed_acc
- else:
- bat_speed_cur *= 0.4
- if bat_speed_cur < -bat_speed_max:
- bat_speed_cur = -bat_speed_max
- elif bat_speed_cur > bat_speed_max:
- bat_speed_cur = bat_speed_max
- elif abs(bat_speed_cur) < 0.1:
- bat_speed_cur = 0
- bat_x += bat_speed_cur
- bat_x = round(bat_x)
- background = pygame.Surface(screen.get_size())
- background = background.convert()
- background.fill((200, 200, 200))
- pygame.gfxdraw.box(background, (10, 10, 1004, 748), (100, 0, 0))
- pygame.gfxdraw.box(background, (bat_x - bat_w/2, bat_y, bat_w, bat_h), (200, 200, 200))
- if pygame.font:
- font = pygame.font.Font(None, 36)
- text = font.render(json.dumps([bat_speed_max, bat_speed_acc, bat_speed_cur]), 1, (200, 200, 200))
- background.blit(text, (20, 20))
- screen.blit(background, (0, 0))
- pygame.display.flip()
- for event in pygame.event.get():
- if event.type == QUIT:
- running = False
- elif event.type == KEYDOWN and event.key == K_ESCAPE:
- running = False
Advertisement
Add Comment
Please, Sign In to add comment