summaryrefslogtreecommitdiff
path: root/pong/pong.py
blob: 0363f3c76b8f59bbcece18591265d01e89772ebe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/python3
# Import the pygame library and initialise the game engine
import pygame, sys
from paddle import Paddle
from ball import Ball
from pygame.locals import *

# Open a new window was 700x500
WIDTH=640
HEIGHT=480

#               R    G    B
WHITE       = (255, 255, 255)
GRAY        = (185, 185, 185)
BLACK       = (  0,   0,   0)
RED         = (155,   0,   0)
LIGHTRED    = (175,  20,  20)
GREEN       = (  0, 155,   0)
LIGHTGREEN  = ( 20, 175,  20)
BLUE        = (  0,   0, 155)
LIGHTBLUE   = ( 20,  20, 175)
YELLOW      = (155, 155,   0)
LIGHTYELLOW = (175, 175,  20)

BORDERCOLOR = BLUE
BGCOLOR = BLACK
TEXTCOLOR = WHITE
TEXTSHADOWCOLOR = GRAY
COLORS      = (     BLUE,      GREEN,      RED,      YELLOW)
LIGHTCOLORS = (LIGHTBLUE, LIGHTGREEN, LIGHTRED, LIGHTYELLOW)
assert len(COLORS) == len(LIGHTCOLORS) # each color must have light color


def makeTextObjs(text, font, color):
    surf = font.render(text, True, color)
    return surf, surf.get_rect()

def terminate():
    pygame.quit()
    sys.exit()

def showTextScreen(text):
    # This function displays large text in the
    # center of the screen until a key is pressed.
    # Draw the text drop shadow
    titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTSHADOWCOLOR)
    titleRect.center = (int(WIDTH / 2), int(HEIGHT / 2))
    screen.blit(titleSurf, titleRect)

    # Draw the text
    titleSurf, titleRect = makeTextObjs(text, BIGFONT, TEXTCOLOR)
    titleRect.center = (int(WIDTH / 2) - 3, int(HEIGHT / 2) - 3)
    screen.blit(titleSurf, titleRect)

    # Draw the additional "Press a key to play." text.
    pressKeySurf, pressKeyRect = makeTextObjs('Press a key to play. \'Select\' to quit. \'B\' to reset.', BASICFONT, TEXTCOLOR)
    pressKeyRect.center = (int(WIDTH / 2), int(HEIGHT / 2) + 100)
    screen.blit(pressKeySurf, pressKeyRect)

    while checkForKeyPress() == None:
        pygame.display.update()
        clock.tick() 

def checkForKeyPress():
    global scoreA, scoreB
    for event in pygame.event.get():
        if event.type == QUIT:      #event is quit 
            terminate()
        elif event.type == KEYDOWN:
            if event.key == K_ESCAPE:   #event is escape key
                terminate()
            else:
                return event.key   #key found return with it
        elif event.type == JOYBUTTONDOWN or event.type == JOYAXISMOTION:
            if (js1.get_button(8) == 1 or js2.get_button(8) == 1):
                terminate()
            elif (js1.get_button(0) == 1 or js2.get_button(0) == 1):
                scoreA=0
                scoreB=0
            return 1
                                                                                                                                                                     # no quit or key events in queue so return None    
    return None

def main():
    global clock, screen, BASICFONT, BIGFONT, js1, js2
    pygame.init()
    pygame.mouse.set_visible(False)

    # Define some colors
    BLACK = (0,0,0)
    WHITE = (255,255,255)

    BASICFONT = pygame.font.Font('freesansbold.ttf', 18)
    BIGFONT = pygame.font.Font('freesansbold.ttf', 100)

    size = (WIDTH, HEIGHT)
    screen = pygame.display.set_mode(size, FULLSCREEN)
    pygame.display.set_caption("Pong")
    effect=pygame.mixer.Sound('Pong.wav')
     
    #joystick or gamepad init
    pygame.joystick.init()
    print ("Joystics: ", pygame.joystick.get_count())
    js1 = pygame.joystick.Joystick(0)
    js1.init()
    js2 = pygame.joystick.Joystick(1)
    js2.init()
     
    paddleA = Paddle(WHITE, 10, 100)
    paddleA.rect.x = 20
    paddleA.rect.y = 200
     
    paddleB = Paddle(WHITE, 10, 100)
    paddleB.rect.x = WIDTH-30
    paddleB.rect.y = 200
     
    ball = Ball(WHITE,10,10)
    ball.rect.x = int(WIDTH/2-5)
    ball.rect.y = 195
     
    #This will be a list that will contain all the sprites we intend to use in our game.
    all_sprites_list = pygame.sprite.Group()
     
    # Add the car to the list of objects
    all_sprites_list.add(paddleA)
    all_sprites_list.add(paddleB)
    all_sprites_list.add(ball)
     
    # The loop will carry on until the user exit the game (e.g. clicks the close button).
    carryOn = True
     
    # The clock will be used to control how fast the screen updates
    clock = pygame.time.Clock()
     
    #Initialise player scores
    global scoreA, scoreB
    scoreA = 0
    scoreB = 0
     
    showTextScreen('Pong')
    # -------- Main Program Loop -----------
    while carryOn:
        # --- Main event loop
        for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT: # If user clicked close
                  carryOn = False # Flag that we are done so we exit this loop
            elif event.type==pygame.KEYDOWN:
                    if event.key==pygame.K_x: #Pressing the x Key will quit the game
                         carryOn=False
     
        #Moving the paddles when the use uses the arrow keys (player A) or "W/S" keys (player B) 
        """keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            paddleA.moveUp(5)
        if keys[pygame.K_s]:
            paddleA.moveDown(5)
        if keys[pygame.K_UP]:
            paddleB.moveUp(5)
        if keys[pygame.K_DOWN]:
            paddleB.moveDown(5)    
        """
        
        if js1.get_axis(1) < 0:
            paddleA.moveUp(5)
        if js1.get_axis(1) > 0:	
            paddleA.moveDown(5)
        if js2.get_axis(1) < 0:
            paddleB.moveUp(5)
        if js2.get_axis(1) > 0:	
            paddleB.moveDown(5)  
        if (js1.get_button(8) == 1 or js2.get_button(8) == 1):
            terminate()
        if (js1.get_button(9) == 1 or js2.get_button(9) == 1):
            showTextScreen('-Pause-')
            
        # --- Game logic should go here
        all_sprites_list.update()
        
        #Check if the ball is bouncing against any of the 4 walls:
        if ball.rect.x>=WIDTH-10:
            scoreA+=1
            ball.velocity[0] = -ball.velocity[0]
            effect.play()
        if ball.rect.x<=0:
            scoreB+=1
            ball.velocity[0] = -ball.velocity[0]
            effect.play()
        if ball.rect.y>HEIGHT-10:
            ball.velocity[1] = -ball.velocity[1]
            effect.play()
        if ball.rect.y<0:
            ball.velocity[1] = -ball.velocity[1]     
            effect.play()
            
        #Detect collisions between the ball and the paddles
        if pygame.sprite.collide_mask(ball, paddleA) or pygame.sprite.collide_mask(ball, paddleB):
          ball.bounce()
          effect.play()
        
        # --- Drawing code should go here
        # First, clear the screen to black. 
        screen.fill(BLACK)
        #Draw the net
        pygame.draw.line(screen, WHITE, [int(WIDTH/2), 0], [int(WIDTH/2), HEIGHT], 5)
        
        #Now let's draw all the sprites in one go. (For now we only have 2 sprites!)
        all_sprites_list.draw(screen) 
     
        #Display scores:
        font = pygame.font.Font(None, 74)
        text = font.render(str(scoreA), 1, WHITE)
        screen.blit(text, (int(WIDTH/2-WIDTH/4),10))
        text = font.render(str(scoreB), 1, WHITE)
        screen.blit(text, (int(WIDTH/2+WIDTH/4),10))
     
        # --- Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
         
        # --- Limit to 60 frames per second
        clock.tick(60)
     
    #Once we have exited the main program loop we can stop the game engine:
    pygame.quit()

if __name__ == '__main__':
    main()