SHOW:
|
|
- or go back to the newest paste.
1 | #!/usr/bin/python | |
2 | import pygame | |
3 | import sys | |
4 | import math | |
5 | from random import random | |
6 | from pong import Pong | |
7 | from pygame.locals import * | |
8 | ||
9 | x_vel = random() | |
10 | y_vel = (2 - x_vel**2) ** .5 | |
11 | x = 294 | |
12 | y = 294 | |
13 | ||
14 | # Create an instance | |
15 | game = Pong() | |
16 | ||
17 | # Initalize the game | |
18 | pygame.init() | |
19 | screen = pygame.display.set_mode((900,600),0,32) | |
20 | screen.blit(screen, (0,0)) | |
21 | ||
22 | pygame.mouse.set_visible(False) # Hide mr. mouse | |
23 | beeper = pygame.image.load('paddle.png') # Paddle | |
24 | bpaddle = pygame.image.load('blank_paddle.png') # Paddle that is drawn over the green one to ensure "motion" | |
25 | ||
26 | ball_img = pygame.image.load('ball.png') | |
27 | ball_cover = pygame.image.load('blank_ball.png') | |
28 | ball_rect = ball_img.get_rect() | |
29 | clock = pygame.time.Clock() | |
30 | ||
31 | # Setup the paddle | |
32 | paddle_pos = (0,0) | |
33 | paddle_rect = beeper.get_rect() | |
34 | bounds_rect = pygame.Rect(880,1,0,900) | |
35 | ||
36 | while True: | |
37 | for event in pygame.event.get(): | |
38 | if event.type == QUIT: | |
39 | sys.exit() | |
40 | ||
41 | # Draw the net | |
42 | pygame.draw.line(screen, game.lineColor, game.net1, game.net2, game.netWidth) | |
43 | ||
44 | # Ball animation | |
45 | boundsball_rect = pygame.Rect(x,y,0,0) | |
46 | ball_rect.clamp_ip(boundsball_rect) | |
47 | x += x_vel | |
48 | y += y_vel | |
49 | if x<=0 or x>=900: x_vel*=-1 | |
50 | if y<=0 or y>=600: y_vel*=-1 | |
51 | screen.blit(ball_img, ball_rect) | |
52 | pygame.display.flip() | |
53 | - | clock.tick(20) |
53 | + | clock.tick(200) |
54 | screen.blit(ball_cover, ball_rect) | |
55 | ||
56 | # Player paddle animation | |
57 | paddle_rect.center = pygame.mouse.get_pos() | |
58 | paddle_rect.clamp_ip(bounds_rect) | |
59 | screen.blit(beeper, paddle_rect) | |
60 | pygame.display.flip() | |
61 | screen.blit(bpaddle, paddle_rect) |