Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I ' d like a breakdown of this Pong code to get a better understanding of how it all works. In terms of functions, _

I'd like a breakdown of this Pong code to get a better understanding of how it
all works. In terms of functions, __init__, self, etc.
Thank You!! Code:
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):
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
#wall col
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
##wall col
#paddle col
#player
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
#enemy
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
##paddle col
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)
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((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

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

It would have cost more to complain.

Answered: 1 week ago