Skip to content

Commit a669f51

Browse files
authored
Add files via upload
1 parent e8b4c48 commit a669f51

File tree

8 files changed

+282
-0
lines changed

8 files changed

+282
-0
lines changed

PONG.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
# I - IMPORT AND INITIALIZE
2+
import pygame, pySprites
3+
pygame.init()
4+
5+
def main():
6+
'''This function defines the 'mainline logic' for our pyPong game.'''
7+
8+
# D - DISPLAY
9+
screen = pygame.display.set_mode((640, 480))
10+
pygame.display.set_caption("pyPong! v1.0")
11+
12+
# E - ENTITIES
13+
#background = pygame.Surface(screen.get_size())
14+
#background = background.convert()
15+
#background.fill((255, 255, 255))
16+
background= pygame.image.load("beach.jpg").convert()
17+
screen.blit(background, (0, 0))
18+
pygame.mixer.music.load("pirate.ogg")
19+
pygame.mixer.music.set_volume(0.6)
20+
pygame.mixer.music.play(-1)
21+
22+
boing=pygame.mixer.Sound("shotgun.wav")
23+
boing.set_volume(0.8)
24+
25+
canon=pygame.mixer.Sound("canon.wav")
26+
canon.set_volume(1.0)
27+
mySystemFont = pygame.font.SysFont("Arial", 60)
28+
label1= mySystemFont.render("GAME OVER!", True, (255, 255, 0))
29+
# Initialize Joystick objects.
30+
joysticks = []
31+
for joystick_no in range(pygame.joystick.get_count()):
32+
stick = pygame.joystick.Joystick(joystick_no)
33+
stick.init()
34+
joysticks.append(stick)
35+
36+
# Sprites for: ScoreKeeper label, End Zones, Ball, and Players
37+
scoreKeeper = pySprites.ScoreKeeper()
38+
ball = pySprites.Ball(screen)
39+
player1 = pySprites.Player(screen, 1)
40+
player1Endzone = pySprites.EndZone(screen,0)
41+
player2 = pySprites.Player(screen, 2)
42+
player2Endzone = pySprites.EndZone(screen,639)
43+
allSprites = pygame.sprite.OrderedUpdates(scoreKeeper, player1Endzone, \
44+
player2Endzone, ball, player1, player2)
45+
46+
# A - ACTION
47+
# A - ASSIGN
48+
clock = pygame.time.Clock()
49+
keepGoing = True
50+
51+
# Hide the mouse pointer
52+
pygame.mouse.set_visible(False)
53+
54+
# L - LOOP
55+
while keepGoing:
56+
57+
# TIME
58+
clock.tick(30)
59+
60+
# E - EVENT HANDLING: Player 1 uses joystick, Player 2 uses arrow keys
61+
for event in pygame.event.get():
62+
if event.type == pygame.QUIT:
63+
keepGoing = False
64+
#elif event.type == pygame.JOYHATMOTION:
65+
# player1.changeDirection(event.value)
66+
elif event.type == pygame.KEYDOWN:
67+
if event.key == pygame.K_UP:
68+
player2.changeDirection((0, 1))
69+
if event.key == pygame.K_DOWN:
70+
player2.changeDirection((0, -1))
71+
if event.key == pygame.K_w:
72+
player1.changeDirection((0, 1))
73+
if event.key == pygame.K_s:
74+
player1.changeDirection((0, -1))
75+
76+
77+
# Check if player 1 scores (i.e., ball hits player 2 endzone)
78+
if ball.rect.colliderect(player2Endzone):
79+
boing.play()
80+
scoreKeeper.player1Scored()
81+
ball.changeDirection()
82+
83+
# Check if player 2 scores (i.e., ball hits player 1 endzone)
84+
if ball.rect.colliderect(player1Endzone):
85+
boing.play()
86+
scoreKeeper.player2Scored()
87+
ball.changeDirection()
88+
89+
90+
# Check for game over (if a player gets 3 points)
91+
if scoreKeeper.winner():
92+
screen.blit(label1,(30,255))
93+
keepGoing = False
94+
95+
# Check if ball hits Player 1 or 2
96+
# If so, change direction, and speed up the ball a little
97+
if ball.rect.colliderect(player1.rect) or\
98+
ball.rect.colliderect(player2.rect):
99+
canon.play()
100+
ball.changeDirection()
101+
102+
103+
# R - REFRESH SCREEN
104+
allSprites.clear(screen, background)
105+
allSprites.update()
106+
allSprites.draw(screen)
107+
pygame.display.flip()
108+
109+
# Unhide the mouse pointer
110+
pygame.mouse.set_visible(True)
111+
112+
113+
# Close the game window
114+
pygame.time.delay(3000)
115+
pygame.quit()
116+
117+
# Call the main function
118+
main()

beach.jpg

519 KB
Loading

canon.wav

85.2 KB
Binary file not shown.

pirate.ogg

1.06 MB
Binary file not shown.

pySprites.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import pygame
2+
3+
class Ball(pygame.sprite.Sprite):
4+
'''This class defines the sprite for our Ball.'''
5+
def __init__(self, screen):
6+
'''This initializer takes a screen surface as a parameter, initializes
7+
the image and rect attributes, and x,y direction of the ball.'''
8+
# Call the parent __init__() method
9+
pygame.sprite.Sprite.__init__(self)
10+
11+
# Set the image and rect attributes for the Ball
12+
self.image = pygame.Surface((20, 20))
13+
self.image.fill((0, 0, 0))
14+
self.image.set_colorkey((0,0,0))
15+
pygame.draw.circle(self.image, (255, 0, 0), (10, 10), 10, 0)
16+
#self.image = pygame.image.load("ball.jpg")
17+
self.rect = self.image.get_rect()
18+
self.rect.center = (screen.get_width()/2,screen.get_height()/2)
19+
20+
# Instance variables to keep track of the screen surface
21+
# and set the initial x and y vector for the ball.
22+
self.window = screen
23+
self.dx = 5 #5
24+
self.dy = -3 #-3
25+
26+
def changeDirection(self):
27+
'''This method causes the x direction of the ball to reverse.'''
28+
if self.dx < 0:
29+
self.dx-=1
30+
elif self.dx > 1:
31+
self.dx+=1
32+
elif self.dy < 0:
33+
self.dx-=1
34+
elif self.dy > 1:
35+
self.dy+=1
36+
37+
self.dx = -self.dx
38+
39+
40+
def update(self):
41+
'''This method will be called automatically to reposition the
42+
ball sprite on the screen.'''
43+
# Check if we have reached the left or right end of the screen.
44+
# If not, then keep moving the ball in the same x direction.
45+
if ((self.rect.left > 0) and (self.dx < 0)) or\
46+
((self.rect.right < self.window.get_width()) and (self.dx > 0)):
47+
self.rect.left += self.dx
48+
# If yes, then reverse the x direction.
49+
else:
50+
self.dx = -self.dx
51+
52+
# Check if we have reached the top or bottom of the court.
53+
# If not, then keep moving the ball in the same y direction.
54+
if ((self.rect.top-40 > 0) and (self.dy > 0)) or\
55+
((self.rect.bottom+40 < self.window.get_height()) and (self.dy < 0)):
56+
self.rect.top -= self.dy
57+
# If yes, then reverse the y direction.
58+
else:
59+
self.dy = -self.dy
60+
61+
class Player(pygame.sprite.Sprite):
62+
'''This class defines the sprite for Player 1 and Player 2'''
63+
def __init__(self, screen, playerNum):
64+
'''This initializer takes a screen surface, and player number as
65+
parameters. Depending on the player number it loads the appropriate
66+
image and positions it on the left or right end of the court'''
67+
# Call the parent __init__() method
68+
pygame.sprite.Sprite.__init__(self)
69+
70+
# Define the image attributes for a black rectangle.
71+
#self.image = pygame.Surface((10, 100))
72+
#self.image = self.image.convert()
73+
#self.image.fill((0, 0, 0))
74+
self.image = pygame.image.load("twig.jpg")
75+
self.rect = self.image.get_rect()
76+
77+
# If we are initializing a sprite for player 1,
78+
# position it 10 pixels from screen left.
79+
if playerNum == 1:
80+
self.rect.left = 10
81+
# Otherwise, position it 10 pixels from the right of the screen.
82+
else:
83+
self.rect.right = screen.get_width()-10
84+
85+
# Center the player vertically in the window.
86+
self.rect.top = screen.get_height()/2 + 50
87+
self.window = screen
88+
self.dy = 0
89+
90+
def changeDirection(self, xyChange):
91+
'''This method takes a (x,y) tuple as a parameter, extracts the
92+
y element from it, and uses this to set the players y direction.'''
93+
self.dy = xyChange[1]
94+
95+
def update(self):
96+
'''This method will be called automatically to reposition the
97+
player sprite on the screen.'''
98+
# Check if we have reached the top or bottom of the screen.
99+
# If not, then keep moving the player in the same y direction.
100+
if ((self.rect.top > 0) and (self.dy > 0)) or\
101+
((self.rect.bottom < self.window.get_height()) and (self.dy < 0)):
102+
self.rect.top -= (self.dy*7) #5
103+
# If yes, then we don't change the y position of the player at all.
104+
105+
class EndZone(pygame.sprite.Sprite):
106+
'''This class defines the sprite for our left and right end zones'''
107+
def __init__(self, screen, xPosition):
108+
'''This initializer takes a screen surface, and x position as
109+
parameters. For the left (player 1) endzone, x_position will = 0,
110+
and for the right (player 2) endzone, x_position will = 639.'''
111+
# Call the parent __init__() method
112+
pygame.sprite.Sprite.__init__(self)
113+
114+
# Our endzone sprite will be a 1 pixel wide black line.
115+
self.image = pygame.Surface((1, screen.get_height()))
116+
self.image = self.image.convert()
117+
self.image.fill((0, 0, 0))
118+
119+
# Set the rect attributes for the endzone
120+
self.rect = self.image.get_rect()
121+
self.rect.left = xPosition
122+
self.rect.top = 0
123+
124+
class ScoreKeeper(pygame.sprite.Sprite):
125+
'''This class defines a label sprite to display the score.'''
126+
def __init__(self):
127+
'''This initializer loads the system font "Arial", and
128+
sets the starting score to 0:0'''
129+
# Call the parent __init__() method
130+
pygame.sprite.Sprite.__init__(self)
131+
132+
# Load our custom font, and initialize the starting score.
133+
self.font=pygame.font.Font("walt.ttf", 30)
134+
#self.font = pygame.font.SysFont("Arial", 30)
135+
self.player1Score = 0
136+
self.player2Score = 0
137+
138+
def player1Scored(self):
139+
'''This method adds one to the score for player 1'''
140+
self.player1Score += 1
141+
142+
def player2Scored(self):
143+
'''This method adds one to the score for player 1'''
144+
self.player2Score += 1
145+
146+
def winner(self):
147+
'''There is a winner when one player reaches 3 points.
148+
This method returns 0 if there is no winner yet, 1 if player 1 has
149+
won, or 2 if player 2 has won.'''
150+
if self.player1Score == 5:
151+
return 1
152+
elif self.player2Score == 5:
153+
return 2
154+
else:
155+
return 0
156+
157+
def update(self):
158+
'''This method will be called automatically to display
159+
the current score at the top of the game window.'''
160+
message = "Player 1: " + str(self.player1Score) +\
161+
" vs. Player 2: " + str(self.player2Score)
162+
self.image = self.font.render(message, 1, (0, 0, 0))
163+
self.rect = self.image.get_rect()
164+
self.rect.center = (320, 15)

shotgun.wav

338 KB
Binary file not shown.

twig.jpg

2.07 KB
Loading

walt.ttf

64.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)