Skip to content

Instantly share code, notes, and snippets.

@VeggieVampire
Last active June 13, 2018 07:33
Show Gist options
  • Save VeggieVampire/8cf1490df6592ad75bf8f15541a744ee to your computer and use it in GitHub Desktop.
Save VeggieVampire/8cf1490df6592ad75bf8f15541a744ee to your computer and use it in GitHub Desktop.
just something I have been playing with in python3. Note add your own images and only setup for a gamepad. Also I only tested on a Pi3 (non windows)
import pygame,sys,glob
pygame.init()
#from pygame import *
try:
j = pygame.joystick.Joystick(0) # create a joystick instance
j.init() # init instance
print ("Enabled joystick: {0} with {1} buttons".format( j.get_name(),j.get_numbuttons()))
#print("{0} has {1} buttons".format(j.get_name(), j.get_numbuttons() ) )
except pygame.error:
print ("no joystick found.")
class Player:
def __init__(self):
self.x = 300
self.y = 300
self.speed_init = 7
self.speed = self.speed_init
self.movement = 7
self.scale = [75,75]
self.imagesR = [pygame.image.load(imgR) for imgR in glob.glob("sprites/chr/moves/s_player_run_right*.png")]
self.imagesD = [pygame.image.load(imgD) for imgD in glob.glob("sprites/chr/moves/s_player_run_down*.png")]
self.imagesU = [pygame.image.load(imgU) for imgU in glob.glob("sprites/chr/moves/s_player_run_up*.png")]
self.indexR = 0
self.max_indexR = len(self.imagesR)-1
self.imgR = self.imagesR[self.indexR] #using as main image holder
self.indexU = 0
self.max_indexU = len(self.imagesU)-1
# self.imgU = self.imagesU[self.indexU]
self.indexD = 0
self.max_indexD = len(self.imagesD)-1
#self.imgD = self.imagesD[self.indexD]
# Add to screen
# Print self.max_index
def update(self, posX, posY):
if posX == 0 or posY == 0: # Wait state
screen.blit(self.imgR, (self.x, self.y))
self.imgR = pygame.transform.scale(self.imgR, (50, 50))
if posX != 0 and posY != 0: # Wait state
screen.blit(self.imgR, (self.x, self.y))
self.imgR = pygame.transform.scale(self.imgR, (50, 50))
if posX !=0: #right
#posY = 0
if posX == 1:
self.speed -= 1
if self.speed == 0:
self.speed = self.speed_init
self.x += (posX * self.movement)
if self.indexR <= self.max_indexR -1:
self.indexR += 1
else:
self.indexR = 0
self.imgR = self.imagesR[self.indexR] #Add to screen
self.imgR = pygame.transform.scale(self.imgR, (50, 50))
# Change True to False if needed,or change the operator.
if posX == -1: #left
self.speed -= 1
if self.speed == 0:
self.speed = self.speed_init
self.x += (posX * self.movement)
if self.indexR <= self.max_indexR - 1:
self.indexR += 1
else:
self.indexR = 0
self.imgR = self.imagesR[self.indexR] # Add to screen
self.imgR = pygame.transform.scale(self.imgR, (50, 50))
self.imgR = pygame.transform.flip(self.imgR, True, False) # flips image for left
if posY !=0: # Down and Up
# posY = 0
if posY == 1: # Down
self.speed -= 1
if self.speed == 0:
self.speed = self.speed_init
self.y += (posY * self.movement)
if self.indexD <= self.max_indexD - 1:
self.indexD += 1
else:
self.indexD = 0
self.imgR = self.imagesD[self.indexD] # Add to screen
self.imgR = pygame.transform.scale(self.imgR, (50, 50))
if posY == -1: # UP
self.speed -= 1
if self.speed == 0:
self.speed = self.speed_init
self.y += (posY * self.movement)
if self.indexU <= self.max_indexU - 1:
self.indexU += 1
else:
self.indexU = 0
self.imgR = self.imagesU[self.indexU] # Add to screen
self.imgR = pygame.transform.scale(self.imgR, (50, 50))
class background:
def __init__(self):
self.img = pygame.image.load('./sprites/chr/images/s_grass_background_0.png') # linux same path
self.posX = 0
self.posY = 255
screen.blit(self.img, [self.posX, self.posY, 10, 10])
def message_to_screen(msg, color):
pygame.font.init()
font = pygame.font.SysFont(None, 25)
screen_text = font.render(msg, True, color)
screen.blit(screen_text, [display_width / 16, display_height / 24])
#pygame.font.quit()
display_width = 800
display_height = 600
screen = pygame.display.set_mode((display_width,display_height))
clock = pygame.time.Clock()
FPS = clock.tick(20)
black = (0, 0, 0)
white = (255,255,255)
player1 = Player()
posX = 0
posY = 0
message = "DEMO"
while True:
screen.fill((black))
background()
message_to_screen("{0}".format(message), white)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.joystick.quit()
print("END OF LINE!")
sys.exit()
if event.type == pygame.JOYAXISMOTION: # Joystick
if j.get_axis(0) >= 0.5:
# print ("right has been pressed") # Right
message = "Right has been pressed"
posX = 1
posY = 0
if j.get_axis(0) <= 0.5 and j.get_axis(0) >= -1: #resets back to center for X
posX = 0
if j.get_axis(1) <= 0.5 and j.get_axis(1) >= -1: #resets back to center for Y
posY = 0
if j.get_axis(0) <= -1:
# print ("left has been pressed") # Left
message = "Left has been pressed"
posX = -1
posY = 0
if j.get_axis(1) >= 0.5:
#print ("Down has been pressed") # Down
message = "Down has been pressed"
posY = 1
posX = 0
if j.get_axis(1) <= -1:
message = "Up has been pressed"
#print ("Up has been pressed") # Up
posY = -1
posX = 0
# print(j.get_axis(0))
# print("Joystick Moved")
if event.type == pygame.JOYBUTTONDOWN:
# print("Joystick Button pressed")
# print (j.get_button(3))
if j.get_button(0) == 1:
message = "X button"
#print("Adding current X{0},Y{1} to sullList".format(lead_x, lead_y))
#skullHead = []
#skullHead.append(lead_x)
# skullHead.append(lead_y)
#skullList.append(skullHead)
if j.get_button(1) == 1:
message = "A button"
if j.get_button(2) == 1:
message = "B button"
if j.get_button(3) == 1:
message = "Y button"
if j.get_button(4) == 1:
message = "Left bumper"
if j.get_button(5) == 1:
message = "Right bumper"
if j.get_button(6) == 1:
message = "unknown button 6"
if j.get_button(7) == 1:
message = "unknown button 7"
if j.get_button(8) == 1:
message = "Select button"
if j.get_button(9) == 1:
message = "Start button"
player1.update(posX,posY)
pygame.display.update()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment