Advertisement
XeN

skate-sim.py

XeN
Mar 5th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.84 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #    skate-sim.py
  5. #
  6. #    Copyright 2012 Ricardo Band <xen@makesyouhappy.org>
  7. #
  8. #    This program is free software; you can redistribute it and/or
  9. #    modify it under the terms of the GNU Library General Public
  10. #    License as published by the Free Software Foundation; either
  11. #    version 3 of the License, or (at your option) any later version.
  12. #
  13. #    This library is distributed in the hope that it will be useful,
  14. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16. #    Library General Public License for more details.
  17. #
  18. #    You should have received a copy of the GNU Library General Public
  19. #    License along with this library; if not, write to the Free
  20. #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21.  
  22. import sys, os
  23. import pygame
  24. from pygame.locals import *
  25. from array import *
  26.  
  27. pygame.init()
  28. fpsClock = pygame.time.Clock()
  29.  
  30. windowSurfaceObj = pygame.display.set_mode((800, 480), FULLSCREEN)
  31. pygame.display.set_caption("Skate Sim")
  32.  
  33. redColor = pygame.Color(255, 0, 0)
  34. greenColor = pygame.Color(0, 255, 0)
  35. blueColor = pygame.Color(0, 0, 255)
  36. whiteColor = pygame.Color(255, 255, 255)
  37.  
  38. fontObj = pygame.font.Font("freesansbold.ttf", 32)
  39.  
  40. # find SIXAXIX
  41. six_present = False
  42. numJoy = pygame.joystick.get_count()
  43. sixaxis = pygame.joystick.Joystick
  44. msg = "SIXAXIS not connected."
  45. if numJoy > 0:
  46.     joyId = -1
  47.     for i in range(0, numJoy):
  48.         joy = pygame.joystick.Joystick(i)
  49.         if "PLAYSTATION(R)3 Controller" in joy.get_name():
  50.             joyId = i
  51.             continue
  52.     if joyId > -1:
  53.         sixaxis = pygame.joystick.Joystick(0)
  54.         if not sixaxis.get_init():
  55.             sixaxis.init()
  56.         six_present = True
  57.     else:
  58.         msg = "This is no SIXAXIS"
  59.  
  60. # prepare arrays for graphs
  61.  
  62. graphx = array('I')
  63. graphy = array('I')
  64. graphz = array('I')
  65. for i in range(0, 800):
  66.     graphx.append(240)
  67.     graphy.append(240)
  68.     graphz.append(240)
  69.  
  70. while True:
  71.     windowSurfaceObj.fill(whiteColor)
  72.  
  73.     if six_present:
  74.         msgx = "x: " + str(sixaxis.get_axis(4))
  75.         msgy = "y: " + str(sixaxis.get_axis(5))
  76.         msgz = "z: " + str(sixaxis.get_axis(6))
  77.     else:
  78.         msgx = ""
  79.         msgy = ""
  80.         msgz = ""
  81.  
  82.     msgxSurfaceObj = fontObj.render(msgx, False, redColor)
  83.     msgySurfaceObj = fontObj.render(msgy, False, greenColor)
  84.     msgzSurfaceObj = fontObj.render(msgz, False, blueColor)
  85.     msgxRectObj = msgxSurfaceObj.get_rect()
  86.     msgyRectObj = msgySurfaceObj.get_rect()
  87.     msgzRectObj = msgzSurfaceObj.get_rect()
  88.     msgxRectObj.topleft = (10, 10)
  89.     msgyRectObj.topleft = (10, 47) # 32px font + 5px space
  90.     msgzRectObj.topleft = (10, 84)
  91.     windowSurfaceObj.blit(msgxSurfaceObj, msgxRectObj)
  92.     windowSurfaceObj.blit(msgySurfaceObj, msgyRectObj)
  93.     windowSurfaceObj.blit(msgzSurfaceObj, msgzRectObj)
  94.  
  95.     for i in range(0, 799):
  96.         graphx[i] = graphx[i+1]
  97.         graphy[i] = graphy[i+1]
  98.         graphz[i] = graphz[i+1]
  99.     if six_present:
  100.         graphx[799] = int(sixaxis.get_axis(4)*-240+240)
  101.         graphy[799] = int(sixaxis.get_axis(5)*-240+240)
  102.         graphz[799] = int(sixaxis.get_axis(6)*-240+240)
  103.     else:
  104.         graphx[799] = 240
  105.         graphy[799] = 240
  106.         graphz[799] = 240
  107.  
  108.     pixArr = pygame.PixelArray(windowSurfaceObj)
  109.     for x in range(0, 800):
  110.         pixArr[x][graphx[x]] = redColor
  111.         pixArr[x][graphy[x]] = greenColor
  112.         pixArr[x][graphz[x]] = blueColor
  113.     del pixArr
  114.  
  115.     for event in pygame.event.get():
  116.         if event.type == QUIT:
  117.             pygame.quit()
  118.             sys.exit()
  119.         elif event.type == MOUSEBUTTONUP:
  120.             pygame.event.post(pygame.event.Event(QUIT))
  121.  
  122.     pygame.display.update()
  123.     fpsClock.tick(30)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement