Answered step by step
Verified Expert Solution
Link Copied!

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
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
#Random Player Movement Option
else:
if random.random()<0.2:
self.y += random.choice([-1,1])* self.speed
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
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

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions

Question

How is PHP code designated in a Web page?

Answered: 1 week ago