我在树莓派上使用python为我的期末高中项目编写一些游戏,我想使用街机套件(游戏DIY街机套装套件可替换部件USB编码器到PC游戏杆和按钮)进行游戏,我已经测试过街机套件,并且在树莓派Raspberry Pi 3 Model B中完美运行,并使用https://html5gamepad.com/
测试了街机套件已经去过https://www.pygame.org/docs/ref/joystick.html并试图为自己编写按钮的编码,但是仍然没有。
如果有帮助,可以选择其中一款游戏
import random
import pygame
pygame.init()
WHITE = (255,255,255)
BLACK = (0,0,0)
PIPE = (0,255,128)
SKY = (102,178,255)
GROUND = (192,192,192)
GRASS = (0,153,76)
BIRD = (255,255,0)
BROWN = (124,115,46)
RED = (204,0,0)
size = (800,700)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Flappy Block")
done = False
clock = pygame.time.Clock()
arial18 = pygame.font.SysFont('arial',18, False, False)
arial30 = pygame.font.SysFont('arial',30, False, False)
gameState = 1
pipes = []
Pontuação = 0
pontuaçãoMáxima = 0
class Bird():
def __init__(self):
self.x = 250
self.y = 250
self.yV = 0
def flap(self):
self.yV = -10
def update(self):
self.yV += 0.5
self.y += self.yV
if self.y >= 608:
self.y = 608
self.yV = 0
if self.yV > 20:
self.yV = 20
def draw(self):
pygame.draw.rect(screen,BIRD,(self.x,self.y,40,40))
def reset(self):
self.x = 250
self.y = 250
self.yV = 0
bird = Bird()
class Pipe():
def __init__(self):
self.centerY = random.randrange(130,520)
self.x = 800
self.size = 100
def update(self):
global pipes
global bird
global gameState
global Pontuação
self.x -= 4
if self.x == 300:
pipes.append(Pipe())
if self.x <= -100:
del pipes[0]
if self.x >= 170 and self.x <= 290 and bird.y <= (self.centerY - self.size) or self.x >= 170 and self.x <= 290 and (bird.y + 40) >= (self.centerY + self.size):
gameState = 3
if self.x == 168 and bird.y > (self.centerY - 100) and bird.y < (self.centerY + 100):
Pontuação += 1
if bird.y >= 608:
gameState = 3
def draw(self):
pygame.draw.rect(screen,PIPE,(self.x,0,80,(self.centerY - self.size)))
pygame.draw.rect(screen,PIPE,(self.x,(self.centerY + self.size),80,(548 - self.centerY)))
pipes.append(Pipe())
# -------- Main Program Loop -----------
while not done:
# --- Main event loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if gameState == 1:
gameState = 2
elif gameState == 3:
bird.reset()
pipes = []
pipes.append(Pipe())
gameState = 2
Pontuação = 0
else:
bird.flap()
screen.fill(SKY)
pygame.draw.rect(screen,GROUND,(0,650,800,50))
pygame.draw.line(screen,GRASS,(0,650),(800,650),5)
pygame.draw.line(screen,GRASS,(0,650),(800,650),5)
if gameState == 1:
pygame.draw.rect(screen,BLACK,(300,300,220,100))
pygame.draw.rect(screen,WHITE,(300,300,220,100),5)
text = arial18.render("Prima um botão para jogar",True,GRASS)
textX = text.get_rect().width
textY = text.get_rect().height
screen.blit(text,((410 - (textX / 2)),(350 - (textY / 2))))
if gameState == 2:
bird.update()
bird.draw()
for pipe in pipes:
pipe.update()
pipe.draw()
if Pontuação > pontuaçãoMáxima:
pontuaçãoMáxima = Pontuação
text = arial30.render(str(Pontuação),True,WHITE)
textX = text.get_rect().width
textY = text.get_rect().height
screen.blit(text,((400 - (textX / 2)),(50 - (textY / 2))))
if gameState == 3:
for pipe in pipes:
pipe.draw()
bird.draw()
pygame.draw.rect(screen,BLACK,(300,250,220,200))
pygame.draw.rect(screen,WHITE,(300,250,220,200),5)
text = arial18.render(("Pontuação: " + str(Pontuação)),True,GRASS)
textX = text.get_rect().width
textY = text.get_rect().height
screen.blit(text,((410 - (textX / 2)),(300 - (textY / 2))))
text = arial18.render(("Pontuação Máxima: " + str(pontuaçãoMáxima)),True,GRASS)
textX = text.get_rect().width
textY = text.get_rect().height
screen.blit(text,((410 - (textX / 2)),(350 - (textY / 2))))
text = arial18.render("Prima um botão para jogar",True,GRASS)
textX = text.get_rect().width
textY = text.get_rect().height
screen.blit(text,((410 - (textX / 2)),(400 - (textY / 2))))
text = arial30.render(str(Pontuação),True,WHITE)
textX = text.get_rect().width
textY = text.get_rect().height
screen.blit(text,((410 - (textX / 2)),(50 - (textY / 2))))
pygame.display.flip()
clock.tick(60)
pygame.quit()