Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object moves off screen

Tags:

python

pygame

import pygame
#Colors, Allways you need colors!!!!
BLACK = ( 0, 0, 0)
GREEN = ( 0, 255, 0)
WHITE = ( 255, 255, 255)
RED = ( 255, 0, 0)
ORANGE = ( 255, 115, 0)
YELLOW = ( 242, 255, 0)
BROWN = ( 115, 87, 39)
PURPLE = ( 298, 0, 247)
GRAY = ( 168, 168, 168)
PINK = ( 255, 0, 234)
BLUE = ( 0, 0 , 255)
pygame.init()
# Screen
screen = pygame.display.set_mode([700,500])
#Name of thewindow
pygame.display.set_caption("Trial to make PONG")
# Any variables!

x_speed = 0
y_speed = 0

x_coord = 10
y_coord = 250

x = 670
y = 250
other_speed = 0
other_speed2 = 0

rect_x = 50
rect_y = 50

rect_change_x = 5
rect_change_y = 5
clock = pygame.time.Clock()
#Sounds,maybe needed?

#Main Loop__________

done = False

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

             # User pressed down on a key
        elif event.type == pygame.KEYDOWN:
            # Figure out if it was an arrow key. If so
            # adjust speed.

            if event.key == pygame.K_UP:
                y_speed = -5
            elif event.key == pygame.K_DOWN:
                y_speed = 5
            elif event.key == pygame.K_w:
                other_speed2 = -5
            elif event.key == pygame.K_s:
                other_speed2 = 5

    # User let up on a key
        elif event.type == pygame.KEYUP:
        # If it is an arrow key, reset vector back to zero
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                x_speed = 0
            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                y_speed = 0
            elif event.key == pygame.K_w or event.key == pygame.K_s:
                other_speed2 = 0


# Move the object according to the speed vector.
    x_coord += x_speed
    y_coord += y_speed
    x +=  x_speed
    y +=  other_speed2


    screen.fill(BLACK)

    pygame.draw.rect(screen,BLUE,[x_coord,y_coord,20,60])
    pygame.draw.rect(screen,YELLOW,[x,y,20,60])

    if x > 650 or x < 0:

    # Draw the rectangle
    pygame.draw.ellipse(screen, BLUE, [rect_x, rect_y, 50, 50])

    # Move the rectangle starting point
    rect_x += rect_change_x
    rect_y += rect_change_y

    if rect_x > 650 or rect_x < 0:
        rect_change_x = rect_change_x * -1
    if rect_y > 450 or rect_y < 0:
        rect_change_y = rect_change_y * -1





    pygame.display.flip()
    clock.tick(60)

pygame.quit()

Ok so I have 2 paddles in this pong game called Blue and yellow rectangle. I can move them but they move off the screen. How do I prevent that. Iv looked online but nothing seems to be working. I thought about putting rectangles around the screen to make a collision point argument where when the Blue/Yellow paddle hit it they won't move any further, but im not sure how to do that with ought ruining the code? Thank you for your help..

like image 209
HALLOPEOPLE Avatar asked Mar 12 '23 18:03

HALLOPEOPLE


1 Answers

You should check to see if it's off the edge of the screen before you change the y coordinate. See

if y_coord + y_speed >= 0 and y_coord + y_speed + 60 <= 500:
    y_coord += y_speed

Though as you can see it can get a little confusing to use numbers, which is why you should avoid hard-coding. It's better to have a display_height, display_width, and y_speed variable. Basically, outside of initializing variables, you should only have 0s as numbers. Also, note what happens when you leave out + y_speed in the if statement.

like image 117
MANA624 Avatar answered Mar 24 '23 02:03

MANA624