def process_events(self): controller = xbox360_controller.Controller(0) xmodifier = 1 left_x, left_y = controller.get_left_stick() for event in pygame.event.get(): if event.type == pygame.QUIT: self.done = True elif event.type == pygame.JOYBUTTONDOWN: if event.button == xbox360_controller.START: if self.stage == Game.SPLASH or self.stage == Game.START: self.stage = Game.PLAYING play_music() elif self.stage == Game.PLAYING: pass elif self.stage == Game.PAUSED: pass elif self.stage == Game.LEVEL_COMPLETED: self.advance() elif self.stage == Game.VICTORY or self.stage == Game.GAME_OVER: if event.joy == controller.get_id(): if event.button == xbox360_controller.START: self.reset() elif event.button == xbox360_controller.BACK: if self.stage == Game.PLAYING: self.start() elif self.stage == Game.PAUSED: self.start() elif self.stage == Game.VICTORY or self.stage == Game.GAME_OVER: if event.joy == controller.get_id(): if event.button == xbox360_controller.START: self.reset() elif event.button == xbox360_controller.A: self.hero.jump(self.level.blocks) elif event.button == xbox360_controller.B: pass#sound on/off: global sound_on, sound_on = False elif event.button == xbox360_controller.X: pass elif left_x != 0: pressed = controller.get_buttons() if pressed[xbox360_controller.LEFT_STICK_BTN] and self.hero.vy == 0: left_x = left_x*2 self.hero.move(left_x) elif left_x == 0: self.hero.stop()
def joystick(x, d): joints = [ "right_s0", "right_s1", "right_e0", "right_e1", "right_w0", "right_w1", "right_w2"] joint = 0 pygame.init() my_controller = xbox360_controller.Controller(0) done = False while not done: # event handling for event in pygame.event.get(): if event.type == pygame.QUIT: done=True if event.type == pygame.JOYBUTTONDOWN: if event.button == xbox360_controller.A: joint += 1 if event.button == xbox360_controller.B: joint -= 1 if event.button == xbox360_controller.Y: done = True if joint < 0: joint += len(joints) if joint >= len(joints): joint -= len(joints) print("joint is {}", joints[joint]); # handle joysticks left_x, left_y = my_controller.get_left_stick() if left_x != 0: s = joints[joint] + "," + str(left_x) print(s) x.rotate_joint(s) d.show(x)
item_images = { "Gem": load_image('assets/images/items/bluewrench.png'), "HealthItem": load_image('assets/images/items/redwrench.png') } # Levels levels = [ "assets/levels/level_1.json", "assets/levels/level_2.json", "assets/levels/level_3.json", "assets/levels/level_4.json", "assets/levels/level_5.json", "assets/levels/level_6.json", "assets/levels/level_7.json", "assets/levels/level_8.json" ] # making a controller controller = xbox360_controller.Controller(0) # draw functions def draw_clouds(): x = random.randrange(0, SCREEN_WIDTH) y = random.randrange(0, SCREEN_HEIGHT) pygame.draw.ellipse(screen, GREEN, [x, y + 20, 40, 40]) pygame.draw.ellipse(screen, GREEN, [x + 60, y + 20, 40, 40]) pygame.draw.ellipse(screen, GREEN, [x + 20, y + 10, 25, 25]) pygame.draw.ellipse(screen, GREEN, [x + 35, y, 50, 50]) pygame.draw.rect(screen, GREEN, [x + 20, y + 20, 60, 40]) # Sprite classes
def __init__(self, controllerId): self.controller = xbox360_controller.Controller(controllerId) self.controllerId = controllerId self.printJoystickInfo()
pygame.init() # define some colors BLACK = (0, 0, 0) WHITE = (255, 255, 255) RED = (255, 0, 0) # window settings size = [600, 600] screen = pygame.display.set_mode(size) pygame.display.set_caption("Simple Game") FPS = 60 clock = pygame.time.Clock() # make a controller controller1 = xbox360_controller.Controller() controller2 = xbox360_controller.Controller() print(controller1.get_id(), controller2.get_id()) # make a ball ball_1_pos = [250, 290] ball_1_radius = 10 ball_1_color = WHITE ball_2_pos = [330, 290] ball_2_radius = 10 ball_2_color = WHITE # settings MAX_SPEED = 5
pygame.init() # Window WIDTH = 800 HEIGHT = 600 SIZE = (WIDTH, HEIGHT) TITLE = "Space Fighters" screen = pygame.display.set_mode(SIZE) pygame.display.set_caption(TITLE) # Timer clock = pygame.time.Clock() refresh_rate = 60 #controller objects my_controller1 = xbox360_controller.Controller(0) my_controller2 = xbox360_controller.Controller(1) # Colors TIEL = (19, 239, 183) MAROON = (198, 3, 3) RED = (255, 0, 0) WHITE = (255, 255, 255) BLACK = (0, 0, 0) YELLOW = (255, 255, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 205) #sounds coin = pygame.mixer.Sound('sounds/coin.ogg') track = pygame.mixer.Sound('sounds/retro1.ogg')
handled: bool finalized: bool date: datetime.datetime def __init__(self, state, handled=False, finalized=True): self.state = state self.handled = handled self.finalized = finalized self.date = datetime.datetime.now() ### pygame.init() dbg = os.environ.get('DEBUGME', '') c = x.Controller(dead_zone=DEADZONE) pressed = None g = [deque(maxlen=10) for i in range(200)] # global state of all keys def updateG(id, state, force=False): d = g[id] needsUpdate = True if not force and d: # d not empty last = d[-1] if last.state == state: needsUpdate = False if force or needsUpdate: d.append(State(state)) if True or dbg: