Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Need help making random player movement move more and be smoother in Pong Game Code: import pygame import random SCR _ WID, SCR _ HEI
Need help making random player movement move more and be smoother in Pong Game
Code:
import pygame
import random
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
#Random Player Movement Option
else:
if random.random:
self.y random.choice self.speed
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
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
for n in rangeself.size, player.padHei:
if self.y player.y n:
if self.x player.x player.padWid:
self.speedx
break
n
for n in rangeself.size, enemy.padHei:
if self.y enemy.y n:
if self.x enemy.x enemy.padWid:
self.speedx
break
n
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
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
ball.draw
player.draw
player.scoring
enemy.draw
pygame.display.flip
clock.tickFPS
main
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