Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22466

IslandT: Move a simple circle up and down with Pygame

$
0
0

In this article I will show you how to move a circle up and down within the display panel of Pygame, the code is basically the modification of the previous code which I have written to add a circle to the display screen of the panel!

The concept behind this is really simple, the program will move the circle from the top left corner to the bottom right corner and will reverse direction once the circle has hit the wall!

# Import and initialize the pygame library
import pygame
pygame.display.init()

# set the caption on the panel
pygame.display.set_caption("Draw Some Drawing")

# windows height and width
windowwidth = 600
windowheight = 600

# Print this line on output panel if the initialize process is successful
if(pygame.display.get_init() == True):
    print("Success initialize the game!")

# Initialize a window or screen for display
screen = pygame.display.set_mode([windowwidth, windowheight])

# Print the x, y size of the game display window

print("The windows size is " + str(pygame.display.get_window_size()))

_x = 0.5 # small increment in x and y direction
_y = 0.5
circle_center_x = 60 # set the circle initial coordinates, set its radius as well
circle_center_y = 60
circle_radius = 60

# Run the game until the user asks to quit
running = True
while running:

    # If the user clicks on the 'x' button on pygame display window then set running to false
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Fill the rectangles with blue color
    screen.fill((0,0,255))

    pygame.draw.circle(screen, pygame.Color(0,0,0), (circle_center_y, circle_center_y), circle_radius) # Draw black circle at the middle of screen
    #pygame.draw.rect(screen, pygame.Color(225, 225, 225), pygame.Rect(x,y,rectangle_width,rectangle_height)) # Draw rectangle on screen
    #pygame.draw.line(screen, pygame.Color(255, 25, 255), (250, 300), (300,300), width=3) # Draw line on screen
    #pygame.draw.arc(screen, pygame.Color(255, 255, 255), pygame.Rect(400,30,100,100), 25.0, 30.0, width=3) # Draw arc on screen
    #pygame.draw.polygon(screen, pygame.Color(200, 125, 205), [(100,100), (150, 150), (200,250), (220, 270), (500, 300), (50, 160)], width=5) # Draw polygon on screen

    circle_center_x+=_x # increase the center x and center y coordinates of the circle
    circle_center_y+=_y

    # switch the _x or _y value if the circle hits the wall
    if(circle_center_x+circle_radius >= windowwidth):
        _x= -0.5
    elif(circle_center_x - circle_radius <= 0):
        _x= 0.5

    if (circle_center_y + circle_radius >= windowwidth):
        _y = -0.5
    elif (circle_center_y - circle_radius <= 0):
        _y = 0.5

    # update the drawing and display screen
    pygame.display.flip()

# Quit the game
pygame.display.quit()

The circle will get trapped within a loop and will continue moving up and down…now it is your turn to modify this program to make the circle moves in four directions, do let me know about the outcome in the comment box below this post if possible!


Viewing all articles
Browse latest Browse all 22466

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>