Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Explanation of how the Classes work, reasons why you use ( self , x , num ) in the functions, and a description of self
Explanation of how the Classes work, reasons why you use self x num
in the functions, and a description of self & init would be helpful!
Code:
import pygame
SCRWID, SCRHEI
class Player:
counter
def initself x num:
Player.counter
self.id Player.counter
self.num num
self.x self.y x SCRHEI
self.speed
self.padWid, self.padHei
self.score
self.score
self.scoreFont pygame.font.Fontimaginefont.ttf
print Playercounter
def scoringself:
scoreBlit self.scoreFont.renderstrselfscore
screen.blitscoreBlit
if self.score :
print player wins!"
exit
scoreBlit self.scoreFont.renderstrselfscore
screen.blitscoreBlitSCRHEI
if self.score:
print Player wins!"
exit
def movementself:
keys pygame.key.getpressed
if self.num :
if keyspygameKw:
self.y self.speed
elif keyspygameKs:
self.y self.speed
if self.y :
self.y
elif self.y SCRHEI:
self.y SCRHEI
if self.num :
if keyspygameKUP:
self.y self.speed
elif keyspygameKDOWN:
self.y self.speed
if self.y :
self.y
elif self.y SCRHEI:
self.y SCRHEI
def drawself:
pygame.draw.rectscreenselfx self.y self.padWid, self.padHei
class Ball:
def initself:
self.x self.y SCRWID SCRHEI
self.speedx
self.speedy
self.size
def movementself:
self.x self.speedx
self.y self.speedy
#wall col
if self.y :
self.speedy
elif self.y SCRHEIself.size:
self.speedy
if self.x :
self.init
player.score
elif self.x SCRWIDself.size:
self.init
self.speedx
player.score
##wall col
#paddle col
#player
for n in rangeself.size, player.padHei:
if self.y player.y n:
if self.x player.x player.padWid:
self.speedx
break
n
#enemy
for n in rangeself.size, enemy.padHei:
if self.y enemy.y n:
if self.x enemy.x enemy.padWid:
self.speedx
break
n
##paddle col
def drawself:
pygame.draw.rectscreenselfx self.y
SCRWID, SCRHEI
screen pygame.display.setmodeSCRWID, SCRHEI
pygame.display.setcaptionPong
pygame.font.init
clock pygame.time.Clock
FPS
player Player
ball Ball
enemy Player
def main:
while True:
#process
for event in pygame.event.get:
if event.type pygame.QUIT:
print Game exited by user"
exit
##process
#logic
ball.movement
player.movement
enemy.movement
##logic
#draw
screen.fill
ball.draw
player.draw
player.scoring
enemy.draw
##draw
#
pygame.display.flip
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started