Pygame碰撞乒乓球

时间:2016-01-28 15:43:33

标签: python-3.x pygame

我想做一个乒乓球克隆。我遇到了碰撞问题。我搜索了很多地方,但我没有解决它。我只想看看球是如何碰撞垫的。 也许我在课上遇到了问题。

import pygame

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

pygame.init()

collision_ball = False
size = (700, 500)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("PONG")

color_shield = BLACK 
l_shield = 15
h_pads = 50
l_pads = 20
color_pads = BLACK
class Shield(pygame.sprite.Sprite):
    def __init__(self,color,width,height,location):
        super().__init__()

        self.image = pygame.Surface([width,height])
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.topleft = location

#ball class        
class Circle(pygame.sprite.Sprite):
    def __init__(self,location):
        super().__init__()
        self.image=pygame.Surface((45,45))
        self.image.fill((0,0,0))
        pygame.draw.circle(self.image,(255,0,0),(25,25),20,0)
        self.rect=self.image.get_rect()
        self.image.set_colorkey(BLACK)
        self.rect.topleft = location
    def update(self):
        self.rect.x +=1

class Pad(pygame.sprite.Sprite):
    def __init__(self,color,location,widht,height):
        super().__init__()
        self.image = pygame.Surface((widht,height))
        self.image.fill(color)
        self.rect = self.image.get_rect()
        self.rect.topleft = location
    def update(self):

        self.update()

pad_left = Pad(color_pads,(30,30),l_pads,h_pads)
pad_right = Pad(color_pads,(650,30),l_pads,h_pads)
pad_group = pygame.sprite.Group()
pad_group.add(pad_left,pad_right)
ball = Circle((350,220))       
#the edge of the screen
balls = pygame.sprite.Group()
balls.add(ball)
shield_up = Shield(color_shield,700,l_shield,(0,0))
shield_down = Shield(color_shield,700,l_shield,(0,485))
shield_right = Shield(color_shield,l_shield,500,(0,0))
shield_left = Shield(color_shield,l_shield,500,(685,0))
all_sprites = pygame.sprite.Group()
shield = pygame.sprite.Group()
shield.add(shield_up,shield_down,shield_right,shield_left)
all_sprites.add(shield,ball,pad_group)

done = False

pygame.key.set_repeat(500,30)
clock = pygame.time.Clock()


while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.KEYDOWN:
        #pad movement
            if event.key == pygame.K_DOWN:
                if (pad_right.rect.y<440):
                    pad_right.rect.y +=10
                else:
                    pass
            if event.key == pygame.K_UP:
                if(pad_right.rect.y>10):
                    pad_right.rect.y -=10
                else:
                    pass
            if event.key == pygame.K_s:
                if (pad_left.rect.y<440):
                    pad_left.rect.y +=10
                else:
                    pass                    
            if event.key == pygame.K_w:
                if (pad_left.rect.y>10):
                    pad_left.rect.y -=10
                else:
                    pass 

我认为这是问题所在:

        collision_ball== ball.rect.colliderect(pad_right.rect)
        if collision_ball== True:
            print("ball hit the pad")



    screen.fill(WHITE)
    ball.update()
    all_sprites.draw(screen)

    pygame.display.flip()


    clock.tick(60)


pygame.quit() 

0 个答案:

没有答案