[Tutor] Hello

dn PyTutor at DancesWithMice.info
Mon Jan 11 19:11:13 EST 2021


On 12/01/2021 11.34, Chukwunonso Oranyeli via Tutor wrote:
> I am going to send my code. Right now I have a ball moving fast and bumping on the edges of my screen. I want the code to be set up in a way that when the ball hits the bottom of my screen, it resets back to the center. Could you help me with this?

We are not here to do 'homework' for you, but are happy to help you learn.


Is this code working correctly? ie when the ball reaches the left or
right side of the screen does it bounce?
NB the code-provided is incomplete. If the 'board' Rect is square, it
may appear to work correctly - but when you make it a rectangle (width
!= height) then it probably will not!


>     def move(self):
> 
>        self.rect.x += self.x_speed
>        self.rect.y += self.y_speed
> 
>        if self.rect.top < 0 or self.rect.bottom > self.windowWidth:
>            self.y_speed = -self.y_speed
> 
>        if self.rect.left< 0 or self.rect.right > self.windowHeight:
>            self.x_speed= -self.x_speed

The code appears to be handling a 'bounce'.

Here are some questions to answer, which will help you answer your own
question and to progress towards a solution:-

When we look at the left or right edges, are we considering the window's
height or its width?
(same question for top/bottom edges)

Which condition, or part of one of the conditions (above) detects when
the ball has hit the bottom of the screen?

NB This needs to become two separate conditions if touching the top of
the window should still result in a 'bounce'!

Once the bottom-bounce condition has been identified and separated, the
then-clause should be altered to re-position the ball, as desired.

Don't forget that as well as changing the ball's position, it may be
appropriate to think about its speed and direction (unless the ball
simply stops/returns to its starting position).


Try working through the above. Then let us know if you have a further
question, and post updated code/the final result.
-- 
Regards =dn


More information about the Tutor mailing list