Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- #----------------------------------------------
- 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):
- pass
- #self.update()
- #----------------------------------------------
- size = (700, 500)
- # --- init ---
- pygame.init()
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("PONG")
- # --- other ---
- collision_ball = False
- color_shield = BLACK
- l_shield = 15
- h_pads = 50
- l_pads = 20
- color_pads = BLACK
- 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)
- # --- mainloop ---
- 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
- if event.key == pygame.K_UP:
- if(pad_right.rect.y>10):
- pad_right.rect.y -=10
- if event.key == pygame.K_s:
- if (pad_left.rect.y<440):
- pad_left.rect.y +=10
- if event.key == pygame.K_w:
- if (pad_left.rect.y>10):
- pad_left.rect.y -=10
- # --- updates ---
- # smaller indentations and `=` instead of `==`
- collision_ball = ball.rect.colliderect(pad_right.rect)
- # smaller indentations
- if collision_ball == True:
- print("ball hit the pad")
- ball.update()
- # --- draws ---
- screen.fill(WHITE)
- all_sprites.draw(screen)
- pygame.display.flip()
- clock.tick(60)
- # --- the end ---
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement