Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import pygame
- import sys
- import random
- import math
- from pong import Pong
- from pygame.locals import *
- # Create an instance
- game = Pong()
- # Ball stuff
- x_vel = random.randint(2,4)
- y_vel = (math.fabs(2 - x_vel)**2) ** .5
- x = 294
- y = 300
- # Initalize the game
- pygame.init()
- screen = pygame.display.set_mode((900,600),0,32)
- screen.blit(screen, (0,0))
- # Hide mr. mouse
- pygame.mouse.set_visible(False)
- paddle = pygame.Surface((20,120)).convert()
- paddle.fill(game.paddlecolor)
- # Setup the ball
- ball_img = pygame.Surface((12,12)).convert()
- ball_img.fill(game.ballcolor)
- ball_rect = pygame.Rect(0,0,12,12)
- clock = pygame.time.Clock()
- # Setup the paddle
- paddle_pos = (0,0)
- paddle_rect = pygame.Rect(0,0,20,120)
- bounds_rect = pygame.Rect(880,1,0,900)
- while True:
- for event in pygame.event.get():
- if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE):
- pygame.quit();sys.exit()
- # Ball animation
- boundsball_rect = pygame.Rect(x,y,0,0)
- ball_rect.clamp_ip(boundsball_rect)
- x += x_vel
- y += y_vel
- if x<=0 or x>=900: x_vel*=-1
- if y<=0 or y>=600: y_vel*=-1
- paddle_rect.center = pygame.mouse.get_pos()
- paddle_rect.clamp_ip(bounds_rect)
- # Fill screen with black
- screen.fill(0)
- # Last ditch drawing of ball and paddle
- screen.blit(ball_img, ball_rect)
- screen.blit(paddle, paddle_rect)
- clock.tick(200)
- # Bounce off of the paddle
- if paddle_rect.colliderect(ball_rect):
- y_vel*=-1
- x_vel*=-1
- # Draw net
- pygame.draw.line(screen, game.lineColor, game.net1, game.net2, game.netWidth)
- pygame.display.flip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement