migrated to keyboard controls

This commit is contained in:
Page Asgardius 2022-01-02 06:39:31 -07:00
parent 2528956239
commit de3dece645

194
r3.py
View file

@ -9,40 +9,23 @@ import subprocess
#process = subprocess.Popen(introplay.split(), stdout=subprocess.PIPE) #process = subprocess.Popen(introplay.split(), stdout=subprocess.PIPE)
#output, error = process.communicate() #output, error = process.communicate()
pygame.init() pygame.init()
pygame.joystick.init() screen = pygame.display.set_mode((800, 480))
if pygame.joystick.get_count() == 0: pygame.display.set_caption('The Red Robot Radio - Virtualx Game Engine')
print ("Please connect a compatible joystick") font = pygame.font.Font(None, 30)
else: clock = pygame.time.Clock()
joystick = pygame.joystick.Joystick(0) FPS = 60
#tested with ds4 controller on rpi, please use joysticktest script to test your controller BLACK = (0, 0, 0)
joystick.init() #WHITE = (255, 255, 255)
if (int(joystick.get_numaxes()) > 3) & ((str(platform.machine()) == "x86") | (str(platform.machine()) == "AMD64")): pygame.mixer.music.load('music/space.ogg')
gamepad = 1 csfx = pygame.mixer.Sound('sfx/crash.ogg')
compatible = True lcfx = pygame.mixer.Sound('sfx/complete.ogg')
pygame.mixer.music.play(-1)
elif int(joystick.get_numaxes()) > 4: #sound = pygame.mixer.Sound(file='bmx.ogg')
gamepad = 2 #raw_array = sound.get_raw()
compatible = True #raw_array = raw_array[100000:92557920]
else: #cut_sound = pygame.mixer.Sound(buffer=raw_array)
compatible = False #cut_sound.play(-1)
if compatible: class Background(pygame.sprite.Sprite):
screen = pygame.display.set_mode((800, 480))
pygame.display.set_caption('The Red Robot Radio - Virtualx Game Engine')
font = pygame.font.Font(None, 30)
clock = pygame.time.Clock()
FPS = 60
BLACK = (0, 0, 0)
#WHITE = (255, 255, 255)
pygame.mixer.music.load('music/space.ogg')
csfx = pygame.mixer.Sound('sfx/crash.ogg')
lcfx = pygame.mixer.Sound('sfx/complete.ogg')
pygame.mixer.music.play(-1)
#sound = pygame.mixer.Sound(file='bmx.ogg')
#raw_array = sound.get_raw()
#raw_array = raw_array[100000:92557920]
#cut_sound = pygame.mixer.Sound(buffer=raw_array)
#cut_sound.play(-1)
class Background(pygame.sprite.Sprite):
def __init__(self): def __init__(self):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.x = 0 self.x = 0
@ -54,7 +37,7 @@ else:
self.velocity = [0, 0] self.velocity = [0, 0]
def update(self): def update(self):
self.rect.move_ip(*self.velocity) self.rect.move_ip(*self.velocity)
class Supernova(pygame.sprite.Sprite): class Supernova(pygame.sprite.Sprite):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.image = pygame.image.load('supernova.png') self.image = pygame.image.load('supernova.png')
@ -62,7 +45,7 @@ else:
#self.image.fill(WHITE) #self.image.fill(WHITE)
self.rect = self.image.get_rect() # Get rect of some size as 'image'. self.rect = self.image.get_rect() # Get rect of some size as 'image'.
self.velocity = [0, 0] self.velocity = [0, 0]
class Antenna(pygame.sprite.Sprite): class Antenna(pygame.sprite.Sprite):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.image = pygame.image.load('antenna.png') self.image = pygame.image.load('antenna.png')
@ -70,7 +53,7 @@ else:
#self.image.fill(WHITE) #self.image.fill(WHITE)
self.rect = self.image.get_rect() # Get rect of some size as 'image'. self.rect = self.image.get_rect() # Get rect of some size as 'image'.
self.velocity = [0, 0] self.velocity = [0, 0]
class Player(pygame.sprite.Sprite): class Player(pygame.sprite.Sprite):
def __init__(self,xset,yset): def __init__(self,xset,yset):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.x = xset self.x = xset
@ -83,7 +66,7 @@ else:
self.velocity = [0, 0] self.velocity = [0, 0]
def update(self): def update(self):
self.rect.move_ip(*self.velocity) self.rect.move_ip(*self.velocity)
class Wallh(pygame.sprite.Sprite): class Wallh(pygame.sprite.Sprite):
def __init__(self,xset,yset): def __init__(self,xset,yset):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.x = xset self.x = xset
@ -95,7 +78,7 @@ else:
self.velocity = [0, 0] self.velocity = [0, 0]
def update(self): def update(self):
self.rect.move_ip(*self.velocity) self.rect.move_ip(*self.velocity)
class Wallv(pygame.sprite.Sprite): class Wallv(pygame.sprite.Sprite):
def __init__(self,xset,yset): def __init__(self,xset,yset):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.x = xset self.x = xset
@ -107,7 +90,7 @@ else:
self.velocity = [0, 0] self.velocity = [0, 0]
def update(self): def update(self):
self.rect.move_ip(*self.velocity) self.rect.move_ip(*self.velocity)
class Css(pygame.sprite.Sprite): class Css(pygame.sprite.Sprite):
def __init__(self,xset,yset): def __init__(self,xset,yset):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.x = xset self.x = xset
@ -120,7 +103,7 @@ else:
self.velocity = [0, 0] self.velocity = [0, 0]
def update(self): def update(self):
self.rect.move_ip(*self.velocity) self.rect.move_ip(*self.velocity)
class Sat(pygame.sprite.Sprite): class Sat(pygame.sprite.Sprite):
def __init__(self,xset,yset): def __init__(self,xset,yset):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.x = xset self.x = xset
@ -133,7 +116,7 @@ else:
self.velocity = [0, 0] self.velocity = [0, 0]
def update(self): def update(self):
self.rect.move_ip(*self.velocity) self.rect.move_ip(*self.velocity)
class Goal(pygame.sprite.Sprite): class Goal(pygame.sprite.Sprite):
def __init__(self,xset,yset): def __init__(self,xset,yset):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.x = xset self.x = xset
@ -146,7 +129,7 @@ else:
self.velocity = [0, 0] self.velocity = [0, 0]
def update(self): def update(self):
self.rect.move_ip(*self.velocity) self.rect.move_ip(*self.velocity)
class Ast(pygame.sprite.Sprite): class Ast(pygame.sprite.Sprite):
def __init__(self,xset,yset): def __init__(self,xset,yset):
pygame.sprite.Sprite.__init__(self) pygame.sprite.Sprite.__init__(self)
self.x = xset self.x = xset
@ -159,36 +142,36 @@ else:
self.velocity = [0, 0] self.velocity = [0, 0]
def update(self): def update(self):
self.rect.move_ip(*self.velocity) self.rect.move_ip(*self.velocity)
player = Player(346,206) player = Player(346,206)
css1 = Css(483,262) css1 = Css(483,262)
css2 = Css(500,400) css2 = Css(500,400)
sat1 = Sat(550,-100) sat1 = Sat(550,-100)
sat2 = Sat(600,100) sat2 = Sat(600,100)
goal = Goal(3000,-100) goal = Goal(3000,-100)
ast1 = Ast(120,200) ast1 = Ast(120,200)
ast2 = Ast(300,100) ast2 = Ast(300,100)
ast3 = Ast(500,150) ast3 = Ast(500,150)
wall1 = Wallh(-100,-200) wall1 = Wallh(-100,-200)
wall2 = Wallh(3100,-200) wall2 = Wallh(3100,-200)
wall3 = Wallv(-200,-200) wall3 = Wallv(-200,-200)
wall4 = Wallv(-200,800) wall4 = Wallv(-200,800)
background = Background() background = Background()
supernova = Supernova() supernova = Supernova()
antenna = Antenna() antenna = Antenna()
running = True running = True
live = True live = True
complete = False complete = False
ax = 0 ax = 0
ay = 0 ay = 0
bx = 0 bx = 0
by = 0 by = 0
debug = False # This set debug mode debug = False # This set debug mode
#rect = pygame.Rect((0, 0), (32, 32)) #rect = pygame.Rect((0, 0), (32, 32))
#image = pygame.Surface((32, 32)) #image = pygame.Surface((32, 32))
#image.fill(WHITE) #image.fill(WHITE)
start_time = pygame.time.get_ticks() start_time = pygame.time.get_ticks()
runtime = 0 runtime = 0
while running: while running:
dt = clock.tick(FPS) / 1000 dt = clock.tick(FPS) / 1000
#screen.fill(BLACK) #screen.fill(BLACK)
datetime.datetime.now() datetime.datetime.now()
@ -244,42 +227,9 @@ else:
bx = 0 bx = 0
elif event.key == pygame.K_d: elif event.key == pygame.K_d:
bx = 0 bx = 0
#ax and ay are for left stick
#ax = joystick.get_axis(0)
#ay = joystick.get_axis(1)
#bx = 0
#by = 0
#if gamepad == 1:
#bx and by are for right stick
#bx = joystick.get_axis(2)
#by = joystick.get_axis(3)
#elif gamepad == 2:
#bx and by are for right stick
#bx = joystick.get_axis(3)
#by = joystick.get_axis(4)
#b0 is for cross button on ds4
b0 = joystick.get_button(0)
#b1 is for circle button on ds4
b1 = joystick.get_button(1)
#b2 is for triangle button on ds4
b2 = joystick.get_button(2)
#b3 is for square button on ds4
b3 = joystick.get_button(3)
if live: if live:
runtime = pygame.time.get_ticks() - start_time runtime = pygame.time.get_ticks() - start_time
if (live & debug == False): if (live & debug == False):
#if pygame.sprite.collide_rect(player, wall3):
# if ay < 0:
# ay = 0
#elif pygame.sprite.collide_rect(player, wall4):
# if ay > 0:
# ay = 0
#if pygame.sprite.collide_rect(player, wall1):
# if ax < 0:
# ax = 0
#elif pygame.sprite.collide_rect(player, wall2):
# if ax > 0:
# ax = 0
if pygame.sprite.collide_rect(background, wall3): if pygame.sprite.collide_rect(background, wall3):
if by < 0: if by < 0:
by = 0 by = 0
@ -331,36 +281,6 @@ else:
ast2.velocity[1] = int(-150 * dt * by) ast2.velocity[1] = int(-150 * dt * by)
ast3.velocity[0] = int(-120 * dt * bx) ast3.velocity[0] = int(-120 * dt * bx)
ast3.velocity[1] = int(-120 * dt * by) ast3.velocity[1] = int(-120 * dt * by)
if b0:
#this stop music playback
pygame.mixer.music.stop()
elif b1:
#this start music playback
pygame.mixer.music.play(-1)
elif (b2 & debug):
#this trigger spaceship crash event
live = False
pygame.mixer.music.stop()
csfx.play()
elif b3:
#this restart the game
player = Player(346,206)
css1 = Css(483,262)
css2 = Css(500,400)
sat1 = Sat(550,-100)
sat2 = Sat(600,100)
goal = Goal(3000,-100)
ast1 = Ast(120,200)
ast2 = Ast(300,100)
ast3 = Ast(500,150)
wall1 = Wallh(-100,-200)
wall2 = Wallh(3100,-200)
wall3 = Wallv(-200,-200)
wall4 = Wallv(-200,800)
live = True
complete = False
pygame.mixer.music.play(-1)
start_time = pygame.time.get_ticks()
player.update() player.update()
css1.update() css1.update()
css2.update() css2.update()
@ -421,5 +341,3 @@ else:
#screen.blit(infox, (50, 110)) #screen.blit(infox, (50, 110))
#screen.blit(infoy, (50, 140)) #screen.blit(infoy, (50, 140))
pygame.display.update() pygame.display.update()
else:
print ("Unknown Joystick Detected")