Сбой консоли Pygame при попытке сделать меню (Colliderect)

Я сделал игру в понг, теперь я пытаюсь сделать улучшения, но у меня проблема с созданием меню для выбора режима 1 против 1 или против IA.

import pygame, sys, os, time, random
from pygame.locals import *

width = 640
heigh = 480


def load_image(nombre, dir_imagen, alpha=False):            # Ruta para las imagenes
    # Encontramos la ruta completa de la imagen
    ruta = os.path.join(dir_imagen, nombre)
    try:
        image = pygame.image.load(ruta)
    except:
        print("Error, no se puede cargar la imagen: " + ruta)
        sys.exit(1)
    # Comprobar si la imagen tiene "canal alpha" (como los png)  ¿?¿?¿?¿?
    if alpha is True:
        image = image.convert_alpha()
    else:
        image = image.convert()
    return image


class botonVSIA(pygame.sprite.Sprite):
    def __init__(self):
        self.VSIA_image = load_image("boton_vsIA.png","E:\Phyton\pygame", alpha=True)
        self.rect = self.VSIA_image.get_rect()
        self.rect.centerx = 160
        self.rect.centery = 460


class duplicar(pygame.sprite.Sprite):
    def __init__(self):
        self.speed = [0, 0]
        self.powerup_image = load_image("powerup_duplicar.png", "E:\Phyton\pygame", alpha=True)
        self.rect = self.powerup_image.get_rect()
        self.rect.centerx = random.randrange(20, width-20)
        self.rect.centery = random.randrange(10, heigh-10)


class barra(pygame.sprite.Sprite):
    def __init__(self,x):
        self.speed = [0, 2]
        pygame.sprite.Sprite.__init__(self)
        self.barra_image = load_image("pad.png", "E:\Phyton\pygame", alpha=True)
        self.rect = self.barra_image.get_rect()                                     # Posicion de los pads
        self.rect.centerx = x
        self.rect.centery = heigh/2

    def manejo(self):                           # Control de limites del pad
        if self.rect.bottom >= heigh:
            self.rect.bottom = heigh
        elif self.rect.top <= 0:
            self.rect.top = 0

    def cpu(self, pelota):                   # Codigo de la cpu

        if pelota.speed[0] >= 0 and pelota.rect.centerx >= width / 2:
            if self.rect.centery > pelota.rect.centery:
                self.rect.centery -= self.speed[1]
            if self.rect.centery < pelota.rect.centery:
                self.rect.centery += self.speed[1]

        if self.rect.bottom >= heigh:
            self.rect.bottom = heigh
        elif self.rect.top <= 0:
            self.rect.top = 0


class pelota(pygame.sprite.Sprite):    # Comportamiento de la pelota
    def __init__(self, level):
        self.pelota_image = load_image("pelota.png", "E:\Phyton\pygame", alpha=True)
        self.rect = self.pelota_image.get_rect()
        self.rect.centerx = width/2
        self.rect.centery = heigh/2
        self.speed = [random.choice([-level, level]), random.choice([-level, level])]  # Direccion inicial de la pelotaç
        self.PJ1 = 0
        self.PIA = 0
        self.powerup = bool(False)

    def move(self):
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0] = -self.speed[0]
            if self.rect.left < 0:
                self.PJ1 += 1
                time.sleep(2)
                self.rect.centerx = width / 2
                self.rect.centery = heigh / 2
            else:
                self.PIA += 1
                self.rect.centerx = width / 2
                self.rect.centery = heigh / 2
                time.sleep(1)

        if self.rect.top < 0 or self.rect.bottom > heigh:
            self.speed[1] = -self.speed[1]
        self.rect.move_ip((self.speed[0], self.speed[1]))

    def colision(self, objetivo, objetivo2, objetivo3):        # Control de la colision
        if self.rect.colliderect(objetivo.rect) or self.rect.colliderect(objetivo2.rect):
            self.speed[0] = -self.speed[0]
        if self.rect.colliderect(objetivo3.rect):
            self.powerup = bool(True)


def main():

    pygame.init()

    fuente = pygame.font.Font(None, 80)

    screen = pygame.display.set_mode((width, heigh))
    cuentatras = fuente.render("HOLA", 1, (110, 110, 100))
    pygame.display.set_caption("PONG! By Elías López")
    background = pygame.image.load("fondo_pong.bmp")
    backgroundfin = pygame.image.load("fondo_pongfinal.png")
    dupli = duplicar()
    bola = pelota(3)  # Aqui iria la level
    bola_pow = pelota(3)
    pad1 = barra(20)
    pad2 = barra(620)
    botonia = botonVSIA()
    clock = pygame.time.Clock()
    pygame.key.set_repeat(1, 20)
    cont = 0

    while bola.PJ1 < 5 and bola.PIA < 5:

Здесь screen.fill не работает, но печать работает и не знаю почему.

        print("try");
        screen.fill((250, 0, 0))
        # screen.blit(background, (0, 0))
        # screen.blit(botonia.VSIA_image, (160, 460))
        pos = pygame.mouse.get_pos()

В любом случае, я не знаю, хорошо ли я пользуюсь colliderect() функция

        if botonia.rect.colliderect(pos) and pygame.mouse.get_pressed():

            cont += 1
            clock.tick(60)
            bola.move()
            pad1.manejo()
            pad2.cpu(bola)
            bola.colision(pad1, pad2, dupli)
            screen.blit(background, (0, 0))
            marcadorj1 = fuente.render(str(bola.PIA), 1, (255, 255, 255))
            marcadoria = fuente.render(str(bola.PJ1), 1, (255, 255, 255))
            screen.blit(marcadorj1, (240, 10))
            screen.blit(marcadoria, (360, 10))
            screen.blit(pad2.barra_image, pad2.rect)
            print(bola_pow.powerup)

            if bola_pow.powerup == bool(True):
                print("hola")
                bola_pow.move()
                bola_pow.colision(pad1, pad2, dupli)
                screen.blit(bola_pow.pelota_image, bola_pow.rect)
            else:
                screen.blit(dupli.powerup_image, dupli.rect)
            screen.blit(bola.pelota_image, bola.rect)
            screen.blit(pad1.barra_image, pad1.rect)
            pygame.display.flip()


            # Posibles entradas del teclado y mouse
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

                else:
                    if event.type == pygame.KEYDOWN:
                        if event.key == K_w or event.key == K_UP:
                            pad1.rect.centery -= 6
                        elif event.key == K_s or event.key == K_DOWN:
                            pad1.rect.centery += 6
                        elif event.key == K_ESCAPE:
                            sys.exit(0)


if __name__ == "__main__":
    main()

0 ответов

Другие вопросы по тегам