0
import random
WIDTH = 500
HEIGHT = 500
bg = Actor('bg_space')
platform = Actor('platforma')
ball = Actor('ball')
a=random.randint(-2,2)
b=random.randint(3,4)
platform1=Actor("platforma1")
def draw():
    screen.clear()
    bg.draw()
    platform.draw()
    ball.draw()
    platform1.draw()
def update():
    global a
    ball.left=ball.left+a
    if (ball.left>WIDTH-35) or (ball.left<0):
        a=-a
    global b
    ball.top=ball.top+b
    if (ball.top<0):
        quit()
    if ball.left==platform.left and ball.top==platform.top:
        b=-b
        a=-a
    if (ball.colliderect(platform)):
        a=-a + random.randint(-3,3)
        b=-b
    if (ball.colliderect(platform1)):
        a=-a + random.randint(-3,3)
        b=-b
    if ball.top>HEIGHT:
        quit()
        
ball.pos=250,250
platform1.pos=250,40
platform.pos=250,485
def on_mouse_move(pos):
        platform.left=pos[0]
        platform.top=460    

Game

How can I make the upper platform move horizontally along the horizontal coordinates of the ball? I want to create a game like ping pong but endless I would like to ask you to explain the code if possible.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
SlazZzor
  • 3
  • 4
  • 1
    [Pygame Zero](https://pygame-zero.readthedocs.io/en/stable/) requires you to use the [tag:pgzero] tag. – Rabbid76 Apr 08 '21 at 19:30
  • Is it not as simple as putting `ball.left=platform.left` in `update()`? Or do you want it to behave differently? If so you should specify how you want it to behave. – Random Davis Apr 08 '21 at 19:32
  • Oh, thank you! I didnt think it was too easy, sorry, i am new at pygame and python as well so i dont really understand all – SlazZzor Apr 08 '21 at 19:39

0 Answers0