-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
117 lines (92 loc) · 3.24 KB
/
main.py
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
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# driver code containing background, paddles, ball and scores
# Import the pygame library and initialise the game engine
import pygame
from paddle import Paddle
from ball import Ball
pygame.init()
# Define some colors
BLACK = (0,0,0)
WHITE = (255,255,255)
# Open a new window
size = (1280, 720)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")
paddleA = Paddle(WHITE, 10, 100)
paddleA.rect.x = 20
paddleA.rect.y = 360
paddleB = Paddle(WHITE, 10, 100)
paddleB.rect.x = 1250
paddleB.rect.y = 360
ball = Ball(WHITE,10,10)
ball.rect.x = 345
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
scoreA = 0
scoreB = 0
# -------- 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)
# --- 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>=1270:
scoreA+=1
ball.velocity[0] = -ball.velocity[0]
if ball.rect.x<=0:
scoreB+=1
ball.velocity[0] = -ball.velocity[0]
if ball.rect.y>710:
ball.velocity[1] = -ball.velocity[1]
if ball.rect.y<0:
ball.velocity[1] = -ball.velocity[1]
#Detect collisions between the ball and the paddles
if pygame.sprite.collide_mask(ball, paddleA) or pygame.sprite.collide_mask(ball, paddleB):
ball.bounce()
# --- Drawing code should go here
# First, clear the screen to black.
screen.fill(BLACK)
#Draw the net
pygame.draw.line(screen, WHITE, [639, 0], [639, 720], 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, (457,10))
text = font.render(str(scoreB), 1, WHITE)
screen.blit(text, (768,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()