Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from pygame.locals import *
- import random
- import math
- #--- Global constants ---
- BLACK = (0, 0, 0)
- WHITE = (255, 255, 255)
- GREEN = (0, 255, 0)
- RED = (255, 0, 0)
- SCREEN_WIDTH = 700
- SCREEN_HEIGHT = 500
- WALL_HEIGHT = SCREEN_HEIGHT - 25
- #set score to 0
- playerscore = 0
- aiscore = 0
- pygame.init()
- # Set the width and height of the screen [width, height]
- size = (700, 500)
- screen = pygame.display.set_mode(size)
- pygame.display.set_caption("Packet Pong")
- # Loop until the user clicks the close button.
- done = False
- # Used to manage how fast the screen updates
- clock = pygame.time.Clock()
- #Classes
- class Player(pygame.sprite.Sprite):
- """ The class is the player-controlled sprite. """
- # -- Methods
- def __init__(self, x, y):
- """Constructor function"""
- # Call the parent's constructor
- pygame.sprite.Sprite.__init__(self)
- # Set height, width
- self.image = pygame.Surface([20, 65])
- self.image.fill(WHITE)
- # Make our top-left corner the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.rect.y = y
- # -- Attributes
- # Set speed vector
- self.change_x = 0
- self.change_y = 0
- def changespeed(self, x, y):
- """ Change the speed of the player"""
- self.change_y += y
- def update(self):
- """ Find a new position for the player"""
- self.rect.y += self.change_y
- if self.rect.bottom >= (WALL_HEIGHT + 3) or self.rect.top <= 24:
- self.rect.y = self.rect.y - self.change_y
- class Ball(pygame.sprite.Sprite):
- def __init__(self):
- pygame.sprite.Sprite.__init__(self)
- self.rect = Rect(200,200,20,20)
- circ_sur = pygame.Surface((15,15))
- self.image = pygame.draw.circle(circ_sur,(RED),(15/2,15/2),15/2)
- self.image = circ_sur.convert()
- self.image.set_colorkey((0,0,0))
- self.velx, self.vely = 5, 5
- def draw(self):
- self.screen.blit(self.image, self.rect)
- def reset(self):
- # to center
- self.rect.y = WALL_HEIGHT // 2
- self.rect.x = SCREEN_WIDTH // 2
- self.velx, self.vely = 5, 5
- def update(self):
- global aiscore
- global playerscore
- # move
- self.rect.x += self.velx
- self.rect.y += self.vely
- # bounce walls
- if self.rect.top <= 25:
- self.vely *= -1
- elif self.rect.bottom >= WALL_HEIGHT:
- self.vely *= -1
- elif pygame.sprite.spritecollide(ai, ball_sprites_list, False):
- self.velx *= -1
- self.vely *= -1
- elif pygame.sprite.spritecollide(player, ball_sprites_list, False):
- self.velx *= -1
- self.vely *= -1
- # offscreen
- elif self.rect.x <= 0:
- aiscore = aiscore + 1
- self.reset()
- elif self.rect.x >= SCREEN_WIDTH:
- playerscore = playerscore + 1
- self.reset()
- class AI(pygame.sprite.Sprite):
- """ The class is the computer-controlled sprite. """
- # -- Methods
- def __init__(self, x, y):
- global vely
- """Constructor function"""
- # Call the parent's constructor
- pygame.sprite.Sprite.__init__(self)
- # Set height, width
- self.image = pygame.Surface([20, 65])
- self.image.fill(WHITE)
- # Make our top-left corner the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.rect.y = y
- def update(self, ball):
- """ Find a new position for the paddle"""
- # Set speed vector
- self.change_x = 0
- self.change_y = ball.vely
- if self.rect.y == 3.1:
- self.rect.y = 3
- if self.rect.y == -3.1:
- self.rect.y = -3
- self.rect.y += self.change_y
- if self.rect.bottom >= (WALL_HEIGHT + 3) or self.rect.top <= 24:
- self.rect.y = self.rect.y - self.change_y
- class Walltop(pygame.sprite.Sprite):
- """ The class is the wall sprite. """
- # -- Methods
- def __init__(self, x, y):
- """Constructor function"""
- # Call the parent's constructor
- pygame.sprite.Sprite.__init__(self)
- # Set height, width
- self.image = pygame.Surface([SCREEN_WIDTH, 25])
- self.image.fill(GREEN)
- # Make our top-left corner the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.rect.y = y
- def update(self):
- """ Do not move"""
- self.rect.x = self.rect.x
- self.rect.y = self.rect.y
- class Wallbottom(pygame.sprite.Sprite):
- """ The class is the wall sprite. """
- # -- Methods
- def __init__(self, x, y):
- """Constructor function"""
- # Call the parent's constructor
- pygame.sprite.Sprite.__init__(self)
- # Set height, width
- self.image = pygame.Surface([SCREEN_WIDTH, 25])
- self.image.fill(GREEN)
- # Make our top-left corner the passed-in location.
- self.rect = self.image.get_rect()
- self.rect.x = x
- self.rect.y = y
- def update(self):
- """ Do not move"""
- self.rect.x = self.rect.x
- self.rect.y = self.rect.y
- # Create sprite lists
- all_sprites_list = pygame.sprite.Group()
- walls_sprites_list = pygame.sprite.Group()
- ball_sprites_list = pygame.sprite.Group()
- # Create the sprites
- player = Player(0, 250)
- all_sprites_list.add(player)
- ai = AI(680, 250)
- all_sprites_list.add(ai)
- walltop = Walltop(0, 0)
- all_sprites_list.add(walltop)
- walls_sprites_list.add(walltop)
- wallbottom = Wallbottom(0, WALL_HEIGHT)
- all_sprites_list.add(wallbottom)
- walls_sprites_list.add(wallbottom)
- ball = Ball()
- ball_sprites_list.add(ball)
- all_sprites_list.add(ball)
- # -------- Main Program Loop -----------
- while not done:
- # --- Main event loop
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- done = True
- elif event.type == pygame.KEYDOWN:
- if event.key == pygame.K_UP:
- player.changespeed(0, -3)
- elif event.key == pygame.K_DOWN:
- player.changespeed(0, 3)
- elif event.key == pygame.K_SPACE:
- ball.reset()
- # Reset speed when key goes up
- elif event.type == pygame.KEYUP:
- if event.key == pygame.K_UP:
- player.changespeed(0, 3)
- elif event.key == pygame.K_DOWN:
- player.changespeed(0, -3)
- screen.fill(BLACK)
- #Draw player score
- font1 = pygame.font.Font(None, 50)
- text1 = font1.render("%d"%playerscore, True, WHITE)
- textpos1 = (100, 200)
- screen.blit(text1, textpos1)
- #Draw AI score
- font2 = pygame.font.Font(None, 50)
- text2 = font1.render("%d"%aiscore, True, WHITE)
- textpos2 = (600, 200)
- screen.blit(text2, textpos2)
- pygame.draw.line(screen, (GREEN), ((SCREEN_WIDTH//2), 25), (350, WALL_HEIGHT))
- #Update
- player.update()
- ai.update(ball)
- ball.update()
- all_sprites_list.draw(screen)
- pygame.display.flip()
- # --- Limit to 60 frames per second
- clock.tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement