在pygame中即插即用游戏杆

时间:2019-07-19 13:29:06

标签: python python-3.x pygame

我正在创建一个支持操纵杆的游戏。我正在使用pygame.joystick库,效果很好。我正在尝试使游戏手柄(xbox一个)在即插即用模式下工作。 (对不起,有语法错误,英语不是我的母语)。 所以问题是:程序启动后,我可以插入操纵杆并使用它们吗?

我还尝试创建一个wait()函数,让玩家有时间插入游戏手柄,然后再按下键,然后开始实际游戏。

代码如下:

import pygame
from pygame import mixer
from pygame import gfxdraw
import sys
from random import randint

pygame.init()

nero = (0,0,0)
celeste = (173,216,230)
giallo = (255,255,0)
bianco = (255,255,255)
verde = (0,255,0)

screen = pygame.display.set_mode((1280,720))
pygame.display.set_caption("Geometry Fighters")
clock = pygame.time.Clock()
font = pygame.font.SysFont("Arial", 50)
bullets = []


class projectile(pygame.sprite.Sprite):
    def __init__(self,x,y,facingx,facingy,color,shape):

        self.image = pygame.image.load("assets\\hitbox2.png")
        self.rect = self.image.get_rect()
        self.facingx = facingx
        self.velx = 10 * facingx
        self.facingy = facingy
        self.vely = 10 * facingy
        self.rect.x = x
        self.rect.y = y
        self.color = color
        self.shape = shape

    def draw(self,screen):
        screen.blit(self.image,(self.rect.x,self.rect.y))
        if self.shape == 1:
            pygame.draw.rect(screen,self.color,(self.rect.x,self.rect.y, 15,15))
        elif self.shape == 2:
            pygame.draw.polygon(screen, self.color, [[self.rect.x, self.rect.y], [self.rect.x-7.5, self.rect.y+12.99],[self.rect.x+7.5, self.rect.y+12.99]], 0)
        elif self.shape == 3:
            pygame.draw.polygon(screen, self.color, [[self.rect.x+8,self.rect.y], [self.rect.x+16,self.rect.y+6], [self.rect.x+12,self.rect.y+16], [self.rect.x+3,self.rect.y+16], [self.rect.x,self.rect.y+6]] , 0)


class Square(pygame.sprite.Sprite):
    def __init__(self):

        self.image = pygame.image.load("assets\\hitbox1.png")
        self.rect = self.image.get_rect()
        self.rect.x = 100
        self.rect.y = 100
        self.shape = 1

    def draw(self,screen):
        screen.blit(self.image,(self.rect.x,self.rect.y))
        pygame.draw.rect(screen, celeste, (self.rect.x,self.rect.y, 35,35))
    def shoot(self):
        bullets.append(projectile(round(self.rect.x+49.5), round(self.rect.y+6.25), 1,0,celeste,1))
        bullets.append(projectile(round(self.rect.x-27), round(self.rect.y+6.25), -1,0,celeste,1))
        bullets.append(projectile(round(self.rect.x+6.25), round(self.rect.y+49.5), 0,1,celeste,1))
        bullets.append(projectile(round(self.rect.x+6.25), round(self.rect.y-27), 0,-1,celeste,1))



class Triangle(pygame.sprite.Sprite):
    def __init__(self):

        self.image = pygame.image.load("assets\\hitbox1.png")
        self.rect = self.image.get_rect()
        self.rect.x = 100
        self.rect.y = 100
        self.shape = 2

        self.verticies = [[self.rect.x+12.5,self.rect.y],[self.rect.x,self.rect.y],[self.rect.x+25,self.rect.y+25]]

    def draw(self,screen):
        screen.blit(self.image, (self.rect.x-12.5,self.rect.y))
        gfxdraw.filled_polygon(screen, self.verticies, giallo)
    def shoot(self):
        bullets.append(projectile(round(self.rect.x+35), round(self.rect.y+35), 1,1,giallo,2))
        bullets.append(projectile(round(self.rect.x), round(self.rect.y+35), -1,1,giallo,2))
        bullets.append(projectile(round(self.rect.x+17.5), round(self.rect.y-15), 0,-1,giallo,2))
    def update(self):
        self.verticies = [[self.rect.x+17.5,self.rect.y],[self.rect.x,self.rect.y+35],[self.rect.x+35,self.rect.y+35]]

class Pentagon(pygame.sprite.Sprite):
    def __init__(self):

        self.image = pygame.image.load("assets\\hitbox1.png")
        self.rect = self.image.get_rect()
        self.rect.x = 100
        self.rect.y = 200
        self.shape = 3
        self.verticies = [[self.rect.x+18,self.rect.y], [self.rect.x+36,self.rect.y+14], [self.rect.x+28,self.rect.y+36], [self.rect.x+7,self.rect.y+36], [self.rect.x,self.rect.y+14]]



    def draw(self,screen):
        screen.blit(self.image, (self.rect.x-12.5,self.rect.y))
        gfxdraw.filled_polygon(screen, self.verticies, verde)
    def shoot(self):
        bullets.append(projectile(round(self.rect.x+11), round(self.rect.y-20), 0,-1,verde,3))
        bullets.append(projectile(round(self.rect.x+46), round(self.rect.y+8), 1,0,verde,3))
        bullets.append(projectile(round(self.rect.x+33), round(self.rect.y+41), 1,1.5,verde,3))
        bullets.append(projectile(round(self.rect.x-15), round(self.rect.y+41), -1,1.5,verde,3))
        bullets.append(projectile(round(self.rect.x-15), round(self.rect.y+8), -1,0,verde,3))

    def update(self):
        self.verticies = [[self.rect.x+18,self.rect.y], [self.rect.x+36,self.rect.y+14], [self.rect.x+29,self.rect.y+36], [self.rect.x+7,self.rect.y+36], [self.rect.x,self.rect.y+14]]










PlayerOne = Triangle()
PlayerTwo = Pentagon()
input_type = 1


def main():
    global input_type
    import pygame
    counter = 0
    global bullets

    while True:
        pygame.joystick.init()
        clock.tick(60)
        MouseX = pygame.mouse.get_pos()[0]
        MouseY = pygame.mouse.get_pos()[1]
        keys = pygame.key.get_pressed()
        click = pygame.mouse.get_pressed()



        joystick_count = pygame.joystick.get_count()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                    pygame.quit()
                    sys.exit()
            if event.type == pygame.JOYAXISMOTION:
                input_type = 3
            if event.type == pygame.JOYHATMOTION:
                input_type = 2

            if event.type == pygame.KEYDOWN:
                input_type = 1
            if keys[pygame.K_v]:
                PlayerOne.shoot()
            if keys[pygame.K_RCTRL]:
                PlayerTwo.shoot()

        screen.fill(nero)
        print(joystick_count)
        for i in range(joystick_count):
            joystick = pygame.joystick.Joystick(i)
            joystick.init()
            hats = joystick.get_numhats()
            axes = joystick.get_numaxes()
            player_to_move = i

            for i in range( axes ):
                axis = joystick.get_axis( i )
                if abs(axis) < 0.2:
                    axis = 0
                if input_type == 3:
                    if player_to_move == 0:
                        if i == 0:
                            PlayerOne.rect.x += 5*axis
                        if i == 1:
                            PlayerOne.rect.y += 5*axis
                    if player_to_move == 1:
                        if i == 0:
                            PlayerTwo.rect.x += 5*axis
                        if i == 1:
                            PlayerTwo.rect.y += 5*axis
            for i in range(hats):
                hat = joystick.get_hat(i)

                if input_type == 2:
                    if hat[0] == 1:
                        if player_to_move == 0:
                            PlayerOne.rect.x += 5
                        elif player_to_move == 1:
                            PlayerTwo.rect.x += 5
                    if hat[0] == -1:
                        if player_to_move == 0:
                            PlayerOne.rect.x -= 5
                        elif player_to_move == 1:
                            PlayerTwo.rect.x -= 5
                    if hat[1] == -1:
                        if player_to_move == 0:
                            PlayerOne.rect.y += 5
                        elif player_to_move == 1:
                            PlayerTwo.rect.y += 5
                    if hat[1] == 1:
                        if player_to_move == 0:
                            PlayerOne.rect.y -= 5
                        elif player_to_move == 1:
                            PlayerTwo.rect.y -= 5

        if input_type == 1:


            if keys[pygame.K_w] and PlayerOne.rect.y-5 > 0:
                PlayerOne.rect.y -= 5

            if keys[pygame.K_s] and PlayerOne.rect.y+30 < 720:
                PlayerOne.rect.y += 5

            if keys[pygame.K_a] and PlayerOne.rect.x-5 > 0:
                PlayerOne.rect.x -= 5

            if keys[pygame.K_d] and PlayerOne.rect.x+30 < 1280:
                PlayerOne.rect.x += 5





            if keys[pygame.K_UP] and PlayerTwo.rect.y-5 > 0:
                PlayerTwo.rect.y -= 5
            if keys[pygame.K_DOWN] and PlayerTwo.rect.y+30 < 720:
                PlayerTwo.rect.y += 5
            if keys[pygame.K_LEFT] and PlayerTwo.rect.x-17.5 > 0:
                PlayerTwo.rect.x -= 5
            if keys[pygame.K_RIGHT] and PlayerTwo.rect.x+17.5 < 1280:
                PlayerTwo.rect.x += 5


        if PlayerOne.shape == 2 or PlayerOne.shape == 3:
            PlayerOne.update()
        if PlayerTwo.shape == 2 or PlayerTwo.shape == 3:
            PlayerTwo.update()








        PlayerOne.draw(screen)
        PlayerTwo.draw(screen)

        for bullet in bullets:
            if pygame.sprite.collide_rect(bullet,PlayerOne):
                print("HIT")
                bullets.pop(bullets.index(bullet))
            if pygame.sprite.collide_rect(bullet,PlayerTwo):
                print("HIT")
                bullets.pop(bullets.index(bullet))
            if bullet.rect.x > 1280 or bullet.rect.x < 0 or bullet.rect.y > 720 or bullet.rect.y < 0:
               bullets.pop(bullets.index(bullet))
            else:

                bullet.rect.x += bullet.velx
                bullet.rect.y += bullet.vely
            bullet.draw(screen)

        pygame.display.flip()






if __name__ == "__main__":
    main()

0 个答案:

没有答案