SHOW:
|
|
- or go back to the newest paste.
| 1 | #!/usr/bin/python | |
| 2 | import pygame | |
| 3 | import sys | |
| 4 | import random | |
| 5 | import math | |
| 6 | from pong import Pong | |
| 7 | from pygame.locals import * | |
| 8 | ||
| 9 | # Create an instance | |
| 10 | game = Pong() | |
| 11 | ||
| 12 | # Ball stuff | |
| 13 | x_vel = random.randint(2,4) | |
| 14 | y_vel = (math.fabs(2 - x_vel)**2) ** .5 | |
| 15 | x = 294 | |
| 16 | y = 300 | |
| 17 | ||
| 18 | # Initalize the game | |
| 19 | pygame.init() | |
| 20 | screen = pygame.display.set_mode((900,600),0,32) | |
| 21 | screen.blit(screen, (0,0)) | |
| 22 | ||
| 23 | # Hide mr. mouse | |
| 24 | pygame.mouse.set_visible(False) | |
| 25 | ||
| 26 | paddle = pygame.Surface((20,120)).convert() | |
| 27 | paddle.fill(game.paddlecolor) | |
| 28 | ||
| 29 | # Setup the ball | |
| 30 | ball_img = pygame.Surface((12,12)).convert() | |
| 31 | ball_img.fill(game.ballcolor) | |
| 32 | ball_rect = pygame.Rect(0,0,12,12) | |
| 33 | clock = pygame.time.Clock() | |
| 34 | ||
| 35 | # Setup the paddle | |
| 36 | paddle_pos = (0,0) | |
| 37 | paddle_rect = pygame.Rect(0,0,20,120) | |
| 38 | bounds_rect = pygame.Rect(880,1,0,900) | |
| 39 | ||
| 40 | while True: | |
| 41 | for event in pygame.event.get(): | |
| 42 | if event.type == QUIT or (event.type == KEYDOWN and event.key == K_ESCAPE): | |
| 43 | pygame.quit();sys.exit() | |
| 44 | ||
| 45 | # Ball animation | |
| 46 | boundsball_rect = pygame.Rect(x,y,0,0) | |
| 47 | ball_rect.clamp_ip(boundsball_rect) | |
| 48 | x += x_vel | |
| 49 | y += y_vel | |
| 50 | if x<=0 or x>=900: x_vel*=-1 | |
| 51 | if y<=0 or y>=600: y_vel*=-1 | |
| 52 | paddle_rect.center = pygame.mouse.get_pos() | |
| 53 | paddle_rect.clamp_ip(bounds_rect) | |
| 54 | ||
| 55 | # Fill screen with black | |
| 56 | screen.fill(0) | |
| 57 | ||
| 58 | # Last ditch drawing of ball and paddle | |
| 59 | screen.blit(ball_img, ball_rect) | |
| 60 | screen.blit(paddle, paddle_rect) | |
| 61 | clock.tick(200) | |
| 62 | ||
| 63 | # Bounce off of the paddle | |
| 64 | if paddle_rect.colliderect(ball_rect): | |
| 65 | - | boundsball_rect = pygame.Rect(x,y,0,0) |
| 65 | + | |
| 66 | - | ball_rect.clamp_ip(boundsball_rect) |
| 66 | + | |
| 67 | ||
| 68 | # Draw net | |
| 69 | pygame.draw.line(screen, game.lineColor, game.net1, game.net2, game.netWidth) | |
| 70 | ||
| 71 | pygame.display.flip() |