Lorenzo1818

Main - CWiid, Pygame example.

Feb 20th, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.66 KB | None | 0 0
  1. """
  2. Program: Terzo script
  3. Author: Lorenzo Benevento
  4. First - Last data modified: 15-02-2014 - 15-02-2014
  5. Description: Software di prova.
  6. PyVersion = 2.*
  7. """
  8. import pygame
  9. from pygame.locals import *
  10. import cwiid
  11. import time
  12. import sys
  13.  
  14. def initWiimote():
  15.     while True:
  16.         print("Press 1 + 2 on your wiimote now...")
  17.         time.sleep(1)
  18.  
  19.         try:
  20.             wm = cwiid.Wiimote()
  21.             break
  22.         except RuntimeError:
  23.             tempVar = raw_input("Error while connecting wiimote. Press enter to retry, other button and enter to close software> ")
  24.             if tempVar == "":
  25.                 pass
  26.             else:
  27.                 sys.exit()
  28.  
  29.     print("Connected.")
  30.  
  31.     wm.rpt_mode = cwiid.RPT_BTN
  32.  
  33.     return wm
  34.  
  35. def initJoystick():
  36.     joystick = None
  37.  
  38.     if pygame.joystick.get_count() > 0:
  39.         joystick = pygame.joystick.Joystick(0)
  40.         joystick.init()
  41.  
  42.     if joystick is None:
  43.         print ("Siamo spiacenti, ma hai bisogno di un joystick con almeno 10 tasti!")
  44.         sys.exit()
  45.  
  46.     return joystick
  47.  
  48. def main(controller, velocita):
  49.    
  50.     pygame.init()
  51.  
  52.     pressedButtons = set()
  53.     pointerImm = "puntatore.png"
  54.     speed = 500
  55.     clock = pygame.time.Clock()
  56.  
  57.     if controller == "wiimote":
  58.         wm = initWiimote()
  59.  
  60.     screen = pygame.display.set_mode((640,480), DOUBLEBUF | HWSURFACE, 32)
  61.     pygame.display.set_caption("Input Vari")
  62.  
  63.     backgroundIntro = pygame.image.load("introduzione.jpg").convert()
  64.     backgroundGame = pygame.image.load("giocoavviato.jpg").convert()
  65.     background = backgroundIntro
  66.     pointer = pygame.image.load(pointerImm).convert_alpha()
  67.  
  68.     tasto_start = pygame.Rect((200,100),(200,200))
  69.  
  70.     if controller == "joystick":
  71.         joystick = initJoystick()
  72.  
  73.     x , y = 0,0
  74.     move_x, move_y = 0,0
  75.  
  76.     while True:
  77.  
  78.         if controller == "wiimote":
  79.             buttons = wm.state["buttons"]
  80.             if (buttons - cwiid.BTN_UP == 0):
  81.                 pressedButtons.add("UP")
  82.                 move_y = -1
  83.             elif "UP" in pressedButtons:
  84.                 pressedButtons.remove("UP")
  85.                 move_y = 0
  86.  
  87.             if (buttons - cwiid.BTN_DOWN == 0):
  88.                 pressedButtons.add("DOWN")
  89.                 move_y = 1
  90.             elif "DOWN" in pressedButtons:
  91.                 pressedButtons.remove("DOWN")
  92.                 move_y = 0
  93.  
  94.             if (buttons - cwiid.BTN_LEFT == 0):
  95.                 pressedButtons.add("LEFT")
  96.                 move_x = -1
  97.             elif "LEFT" in pressedButtons:
  98.                 pressedButtons.remove("LEFT")
  99.                 move_x = 0
  100.  
  101.             if (buttons - cwiid.BTN_RIGHT == 0):
  102.                 pressedButtons.add("RIGHT")
  103.                 move_x = 1
  104.             elif "RIGHT" in pressedButtons:
  105.                 pressedButtons.remove("RIGHT")
  106.                 move_x = 0
  107.  
  108.             if (buttons - cwiid.BTN_HOME == 0):
  109.                 background = backgroundIntro
  110.  
  111.             if (buttons - cwiid.BTN_A == 0):
  112.                 background = backgroundGame
  113.  
  114.             if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
  115.                 wm.rumble = 1
  116.                 time.sleep(1)
  117.                 wm.rumble = 0
  118.                 sys.exit(wm)
  119.  
  120.         for event in pygame.event.get():
  121.             if event.type == QUIT:
  122.                 sys.exit()
  123.  
  124.             if controller == "keyboard" or controller == "joystick":
  125.                 if event.type == KEYDOWN:
  126.                     tasti_premuti = pygame.key.get_pressed()
  127.  
  128.                     if tasti_premuti[K_ESCAPE]:
  129.                         sys.exit(wm)
  130.  
  131.                     if tasti_premuti[K_LALT] and tasti_premuti[K_F4]:
  132.                         sys.exit(wm)
  133.  
  134.                     if event.key == K_UP:
  135.                         move_y = -1
  136.  
  137.                     if event.key == K_DOWN:
  138.                         move_y = 1
  139.  
  140.                     if event.key == K_LEFT:
  141.                         move_x = -1
  142.  
  143.                     if event.key == K_RIGHT:
  144.                         move_x = 1
  145.  
  146.                 elif event.type == KEYUP:
  147.  
  148.                     if event.key == K_UP:
  149.                         move_y = 0
  150.  
  151.                     if event.key == K_DOWN:
  152.                         move_y = 0
  153.  
  154.                     if event.key == K_LEFT:
  155.                         move_x = 0
  156.  
  157.                     if event.key == K_RIGHT:
  158.                         move_x = 0
  159.  
  160.         if controller == "joystick":
  161.             if joystick != None and joystick.get_button(7):
  162.                 if background == backgroundIntro:
  163.                     background = backgroundGame
  164.  
  165.         pygame.mouse.set_visible(False)
  166.  
  167.         timeSpent = clock.tick(30)
  168.         timeSpentSec = timeSpent/1000.0
  169.  
  170.         movementDistance = timeSpentSec*speed
  171.  
  172.         x += move_x * movementDistance
  173.         y += move_y * movementDistance
  174.  
  175.         tasto_start.move_ip(move_x * movementDistance,move_y * movementDistance)
  176.  
  177.         screen.fill((0,0,0))
  178.         screen.blit(background, (x,y))
  179.  
  180.                 if controller == "keyboard":
  181.                         pulsanti_mouse = pygame.mouse.get_pressed()
  182.                         coordinate_mouse = pygame.mouse.get_pos()
  183.  
  184.                         if pulsanti_mouse[0]==1:
  185.                                 if background == backgroundIntro:
  186.                                         if tasto_start.collidepoint(coordinate_mouse):
  187.                                                 background = backgroundGame
  188.  
  189.                         if pulsanti_mouse[2]==1:
  190.                                 if background == backgroundGame:
  191.                                         background = backgroundIntro
  192.  
  193.                         screen.blit(pointer, coordinate_mouse)
  194.  
  195.         pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment