Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

At a Score of 5 Player 1 does enable 2 balls bouncing separately, but when Player 2 gets to 5 they don't separate. How do

At a Score of 5 Player 1 does enable 2 balls bouncing separately,
but when Player 2 gets to 5 they don't separate.
How do I fix this?
import pygame
SCR_WID, SCR_HEI =640,480
class Player():
_counter =0
def __init__(self, x, num):
Player._counter +=1
self.id = Player._counter
self.num = num
self.x, self.y = x, SCR_HEI/2
self.speed =3
self.padWid, self.padHei =8,64
self.score =0
self.score2=0
self.scoreFont = pygame.font.Font("imagine_font.ttf",64)
print (Player._counter)
def scoring(self):
scoreBlit = self.scoreFont.render(str(self.score),1,(255,255,255))
screen.blit(scoreBlit,(32,16))
if self.score ==10:
print ("player 1 wins!")
exit()
scoreBlit = self.scoreFont.render(str(self.score2),1,(255,255,255))
screen.blit(scoreBlit,(SCR_HEI+92,16))
if self.score2==10:
print ("Player 2 wins!")
exit()
def movement(self):
keys = pygame.key.get_pressed()
if self.num ==1:
if keys[pygame.K_w]:
self.y -= self.speed
elif keys[pygame.K_s]:
self.y += self.speed
if self.y <=0:
self.y =0
elif self.y >= SCR_HEI-64:
self.y = SCR_HEI-64
if self.num ==2:
if keys[pygame.K_UP]:
self.y -= self.speed
elif keys[pygame.K_DOWN]:
self.y += self.speed
if self.y <=0:
self.y =0
elif self.y >= SCR_HEI-64:
self.y = SCR_HEI-64
def draw(self):
#doubleBall
if self.score >=5:
doubleBall.draw()
doubleBall.movement()
elif self.score2>=5:
doubleBall.draw()
doubleBall.movement()
pygame.draw.rect(screen,(255,255,255),(self.x, self.y, self.padWid, self.padHei))
class Ball():
def __init__(self):
self.x, self.y = SCR_WID/2, SCR_HEI/2
self.speed_x =-3
self.speed_y =3
self.size =8
def movement(self):
self.x += self.speed_x
self.y += self.speed_y
if self.y <=0:
self.speed_y *=-1
elif self.y >= SCR_HEI-self.size:
self.speed_y *=-1
if self.x <=0:
self.__init__()
player.score2+=1
elif self.x >= SCR_WID-self.size:
self.__init__()
self.speed_x =3
player.score +=1
for n in range(-self.size, player.padHei):
if self.y == player.y + n:
if self.x <= player.x + player.padWid:
self.speed_x *=-1
break
n +=1
for n in range(-self.size, enemy.padHei):
if self.y == enemy.y + n:
if self.x >= enemy.x - enemy.padWid:
self.speed_x *=-1
break
n +=1
def draw(self):
pygame.draw.rect(screen,(255,255,255),(self.x, self.y,8,8))
SCR_WID, SCR_HEI =640,480
screen = pygame.display.set_mode((SCR_WID, SCR_HEI))
pygame.display.set_caption("Pong")
pygame.font.init()
clock = pygame.time.Clock()
FPS =60
player = Player(16,1)
ball = Ball()
enemy = Player(624,2)
doubleBall = Ball()
def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
print ("Game exited by user")
exit()
ball.movement()
player.movement()
enemy.movement()
screen.fill((0,0,0))
ball.draw()
player.draw()
player.scoring()
enemy.draw()
pygame.display.flip()
clock.tick(FPS)
main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Spomenik Monument Database

Authors: Donald Niebyl, FUEL, Damon Murray, Stephen Sorrell

1st Edition

0995745536, 978-0995745537

More Books

Students also viewed these Databases questions

Question

What are the core features of category management?

Answered: 1 week ago

Question

Which user group is most interested in solvency ratios?

Answered: 1 week ago

Question

7. List behaviors to improve effective leadership in meetings

Answered: 1 week ago

Question

6. Explain the six-step group decision process

Answered: 1 week ago