-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpong.bak
More file actions
140 lines (114 loc) · 4 KB
/
pong.bak
File metadata and controls
140 lines (114 loc) · 4 KB
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
#python3
'''
This is a classical Pong Game
Author: XxVinr_AlfakynxX
Version: 1.0: First Version. No OOP and no collision with bars
Version: 1.1: Updated to be OOP for better collision detection. Still no collision with bars
Version: 1.2: <---- TODO ----> Updated to have working collisions
'''
# imports and initiation
import pygame
from time import sleep
# vars
''' Window '''
WIN_SIZE = WIDTH, HEIGHT = 1260, 600
win = pygame.display.set_mode(WIN_SIZE)
pygame.display.set_caption("Pong By XxVinr_AlfakynxX")
''' Colours '''
white = (255, 255, 255)
black = (0,0,0)
blue = (0, 231, 218)
red = (255, 0, 0)
''' Player Positions and Sizes '''
vel = 10 # player velocity
player_size = player_width, player_height = (8, 80)
''' Ball Stuff '''
ballPos = xB, yB = (int(WIDTH/3), int(HEIGHT/2)-int(player_height/2))
#ballVel = xVel, yVel = 5, 0
# classes
class Bar():
def __init__(self, surface, colour, x, y, player_width, player_height):
''' initiation func '''
self.surface = surface
self.colour = colour
self.x = x
self.y = y
self.player_width = player_width
self.player_height = player_height
self.score = 0
self.rect = pygame.Rect(self.x, self.y, self.player_width, self.player_height)
def hitboxRect(self):
self.rect = pygame.Rect(self.x, self.y, self.player_width, self.player_height)
def show(self):
''' draws the bar to the surface '''
pygame.draw.rect(self.surface, self.colour, self.rect)
def getScore(self):
print("Score: ", self.score)
def scoreMax(self):
if self.score >= 7:
running = False
print("Game Over!")
pygame.quit()
def move(self, option):
''' moves the bar '''
keys = pygame.key.get_pressed()
if option == "up/down":
if keys[pygame.K_UP] and self.y > 0:
self.y -= vel
if keys[pygame.K_DOWN] and self.y+self.player_height < HEIGHT:
self.y += vel
elif option == "w/s":
if keys[pygame.K_w] and self.y > 0:
self.y -= vel
if keys[pygame.K_s] and self.y+self.player_height < HEIGHT:
self.y += vel
class Ball():
def __init__(self, surface, colour, xB, yB, radius, xVel, yVel):
self.surface = surface
self.colour = colour
self.xB = xB
self.yB = yB
self.radius = radius
self.xVel = xVel
self.yVel = yVel
self.rect = pygame.Rect(self.xB, self.yB, self.radius*2, self.radius*2)
self.turn = True
def hitboxBall(self):
self.rect = pygame.Rect(self.xB, self.yB, self.radius*2+15, self.radius*2+15)
def show(self):
pygame.draw.ellipse(self.surface, self.colour, (self.rect))
def move(self):
self.xB += self.xVel
self.yB += self.yVel
def reset(self):
self.xB, self.yB = (int(WIDTH/3), int(HEIGHT/2)-int(player_height/2))
self.xVel = 6
self.yVel = 2
def simpleCollision(self, bar1, bar2):
''' Collision with Walls && SCORING + RESETTING'''
if self.xB + self.radius >= WIDTH:
bar2.score +=1
sleep(.200)
self.reset()
bar1.getScore()
bar2.getScore()
bar1.scoreMax()
bar2.scoreMax()
if self.xB <= 0:
bar1.score +=1
sleep(.200)
self.reset()
bar1.getScore()
bar2.getScore()
bar1.scoreMax()
bar2.scoreMax()
if self.yB + self.radius >= HEIGHT:
self.yVel = int(self.yVel * (-1))
if self.yB <= 0:
self.yVel = int(self.yVel * (-1))
''' Collision with Players '''
if self.turn == True:
if self.rect.colliderect(bar1.rect) or self.rect.colliderect(bar2.rect):
self.xVel *= -1
self.turn = False
elif self.turn =