<-- Back to list of examples

```"""
Example program to show move a sprite with a game controller.

Sample Python/Pygame Programs
Simpson College Computer Science
http://simpson.edu/computer-science/
"""

import pygame

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

class Player(pygame.sprite.Sprite):
"""
This class represents the player.
It derives from the "Sprite" class in Pygame.
"""

def __init__(self):
"""
Constructor. Pass in the color of the block, and its x and y position.
"""

# Call the parent class (Sprite) constructor
super().__init__()

# Variables to hold the height and width of the block
width = 20
height = 15

# Create an image of the ball, and fill it with a color.
# This could also be an image loaded from the disk.
self.image = pygame.Surface([width, height])
self.image.fill(BLACK)

# Fetch the rectangle object that has the dimensions of the image
self.rect = self.image.get_rect()

# Set initial position of sprite to 100,100
self.rect.x = 100
self.rect.y = 100

# Count the joysticks the computer has
self.joystick_count = pygame.joystick.get_count()
if self.joystick_count == 0:
# No joysticks!
print("Error, I didn't find any joysticks.")
else:
# Use joystick #0 and initialize it
self.my_joystick = pygame.joystick.Joystick(0)
self.my_joystick.init()

def update(self):
""" Update the position of the ball """

# As long as there is a joystick
if self.joystick_count != 0:

# This gets the position of the axis on the game controller
# It returns a number between -1.0 and +1.0
horiz_axis_pos = self.my_joystick.get_axis(0)
vert_axis_pos = self.my_joystick.get_axis(1)

# Move x according to the axis. We multiply by 10 to speed up the
# movement.
self.rect.x = self.rect.x+horiz_axis_pos*10
self.rect.y = self.rect.y+vert_axis_pos*10

pygame.init()

# Set the height and width of the screen
WINDOW_SIZE = [700, 500]
screen = pygame.display.set_mode(WINDOW_SIZE)

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

# This is a list of 'sprites.' Each ball in the program (there is only 1) is
# added to this list. The list is managed by a class called 'Group.'
all_sprites_list = pygame.sprite.Group()

# This represents the ball controlled by the player
player = Player()

# Add the ball to the list of player-controlled objects

# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True

# Clear the screen
screen.fill(WHITE)

# Update the position of the ball (using the mouse) and draw the ball
all_sprites_list.update()
all_sprites_list.draw(screen)

# Limit to 60 frames per second
clock.tick(60)

# Go ahead and update the screen with what we've drawn.
pygame.display.flip()

# Be IDLE friendly
pygame.quit()

```
﻿
{"ft_lang_label":"__label__en","ft_lang_prob":0.73406327,"math_prob":0.51256555,"size":2854,"snap":"2021-43-2021-49","text_gpt3_token_len":695,"char_repetition_ratio":0.14421053,"word_repetition_ratio":0.018648019,"special_character_ratio":0.2862649,"punctuation_ratio":0.17540687,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9520903,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-12-03T06:52:52Z\",\"WARC-Record-ID\":\"<urn:uuid:a72e4a23-a121-4a0c-a86b-f0fdb6aafc64>\",\"Content-Length\":\"7279\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:008ac76d-9552-4767-9f1d-a3d94cb779a7>\",\"WARC-Concurrent-To\":\"<urn:uuid:14a4dcdc-6875-40ac-8488-e0cc3b3ea66b>\",\"WARC-IP-Address\":\"54.218.116.180\",\"WARC-Target-URI\":\"http://programarcadegames.com/python_examples/show_file.php?file=move_sprite_game_controller.py\",\"WARC-Payload-Digest\":\"sha1:3EJOILY7XUOWBGSYDK7L4E3ZXGSSZNFT\",\"WARC-Block-Digest\":\"sha1:76T37NV3KEAGEXPXOBN5PXV4U4U23U2D\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964362605.52_warc_CC-MAIN-20211203060849-20211203090849-00291.warc.gz\"}"}