Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:31:33+00:00 2026-05-29T11:31:33+00:00

I teach Computer Programming. For first course students I’m looking for most easy algorithm

  • 0

I teach Computer Programming. For first course students I’m looking for most easy algorithm to change direction on collide (with window frame). This is my actual code:

import sys, pygame
pygame.init()
speed_x = 1
speed_y = 1

black = (0, 0, 0)
width, height = 320, 240
size = ( width, height )

screen = pygame.display.set_mode(size)
display_surface = pygame.display.get_surface()
display_rectangle = display_surface.get_rect()

ball_img = pygame.image.load("data/ball.gif")
ball = ball_img.convert_alpha()
ballrect = ball.get_rect()

while 1:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()

        if not display_rectangle.contains( ballrect.move([speed_x, 0]) ):
            speed_x *= -1
        else:
            ballrect = ballrect.move([speed_x, 0])

        if not display_rectangle.contains( ballrect.move([0, speed_y]) ):
            speed_y *= -1
        else:
            ballrect = ballrect.move([0, speed_y])  

        screen.fill(black)
        screen.blit(ball, ballrect)
        pygame.display.flip()
        pygame.time.delay(30)

This code works fine, my question is if someone know about a easy algorithm or clear algorithm to reverse direction using contains or any other collide pygame test.

Some times I think that an approach is most easy for students and a new clear approach appears.

All suggestions are welcome

Some suggestions? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:31:34+00:00Added an answer on May 29, 2026 at 11:31 am

    I think your approach is fine. Your code models the velocity of the ball using the 2-vector [speed_x, speed_y]. Bouncing off a (horizontal or vertical) wall involves negating the appropriate component of the vector.

    As long as your students have a background in introductory physics (velocity, momentum, collisions) then your code should be understandable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm teaching (or trying to teach) computer programming to a grad-student. Her previous experience
I teach a C++ course using Visual Studio. One of my students has a
first post ever on stack overflow! Anyway...I'm trying to teach myself networking programming in
I have just started to study computer sciences at my university where they teach
I teach the third required intro course in a CS department. One of my
I teach a one semester University course in systems analysis and design. Topics include
We don't teach children calculus first. We first teach them arithmetic, then algebra, then
I am trying to teach ASP.NET MVC to students (undergrads) that have been studying
hi im 17 and trying to teach myself c++. For one of my first
I am teaching (with others) a relatively introductory course in computer science for IT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.