Youssef E.
Youssef E.

Reputation: 43

python object stops moving at the boundaries

I'm experimenting, and I'm trying to get a circle to move along a line but stop once it reaches the edge of the screen. once this happens I can no longer go back the other way. There's probably a simple fix I'm not seeing, and it would be helpful to have someone point me in the right direction. Please keep in mind I am still a beginner.

from pygame import *
import random
import math
import os #Displays the pygame window at the top left of the screen
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d, %d" %(0,25)

init() #Starts pygame
font.init()
LENGTH = 1000 #Creates Screen that is 1000 X 700
WIDTH = 700
SIZE = (LENGTH, WIDTH)
Screen = display.set_mode(SIZE)

#Defines colours
BLACK = (0,0,0)
WHITE = (255,255,255)
RED = (255,0,0)

running = True
CaptainY = 350
Key = 0

while running:
    for evnt in event.get(): # checks all events that happen
        if evnt.type == QUIT: # if event type is quit the program stops running
            running = False
        if evnt.type == KEYDOWN:
            Key = evnt.key
        if evnt.type == KEYUP:
            Key = 0

    if 20 < CaptainY < 680:
        if Key == K_UP:
            CaptainY -= 5
        if Key == K_DOWN:
            CaptainY += 5    

    draw.rect(Screen, BLACK, (0,0, LENGTH, WIDTH))
    draw.circle(Screen, WHITE, (950, CaptainY), 15)

    if Key == K_ESCAPE:
        print(CaptainY)

    display.flip()

quit()

Upvotes: 1

Views: 238

Answers (1)

skrx
skrx

Reputation: 20438

The program is doing what you told it: Move only if the y-position is between 20 and 680. If it's less than 20, this condition won't be True and the circle won't be able move anymore.

# You only move if this is True.
if 20 < CaptainY < 680:

Instead of stopping the movement, you should just move the position back, so that the circle ends up on the screen. Here's a complete example with a few more changes:

import pygame  # Avoid * imports, since they make code harder to read and cause bugs.

pygame.init()
screen = pygame.display.set_mode((1000, 700))
HEIGHT = screen.get_height()

BLACK = (0,0,0)
WHITE = (255,255,255)
clock = pygame.time.Clock()  # Use a clock to limit the frame rate.
running = True
captain_y = 350
captain_radius = 15

while running:
    # Handle events.
    for evnt in pygame.event.get():
        if evnt.type == pygame.QUIT:
            running = False
        elif evnt.type == pygame.KEYDOWN:
            if evnt.key == pygame.K_ESCAPE:
                running = False

    # To see if a key is being held down, use `pygame.key.get_pressed()`.
    pressed_keys = pygame.key.get_pressed()
    # Move if up or down keys are pressed.
    if pressed_keys[pygame.K_UP]:
        captain_y -= 5
    elif pressed_keys[pygame.K_DOWN]:
        captain_y += 5

    # Update the game.
    # Reset the position if the circle is off screen.
    if captain_y - captain_radius <= 0:
        captain_y = 0 + captain_radius
    elif captain_y + captain_radius >= HEIGHT:
        captain_y = HEIGHT - captain_radius

    # Draw everything.
    screen.fill(BLACK)
    pygame.draw.circle(screen, WHITE, (950, captain_y), captain_radius)

    pygame.display.flip()
    clock.tick(60)  # Cap the frame rate at 60 fps.

pygame.quit()

Upvotes: 2

Related Questions