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

IslandT: Draw an image on top of a circle with pygame

$
0
0

In this article, I am going to draw a small hat image on top of a circle with Pygame. Basically, this program has two parts, the circle part, and the image part.

I am going to draw the circle’s part with this line of code which will draw a circle in the middle of the display screen!

pygame.draw.circle(screen, pygame.Color(200,200,200), (circle_center_y, circle_center_y), circle_radius) # Draw white circle at the middle of screen

Then with these few lines of code, I load and draw the image on top of the circle…

# load hat image
pawn0 = pygame.image.load("pawn.png")
width, height = pawn0.get_size() # return width and height of the hat
# draw a hat on the top of circle
screen.blit(pawn0, (circle_center_x-width/2, circle_center_y-circle_radius-height))

The technique here is to position the image at the middle and top of the circle before drawing it!

Below is the whole code…do ignore the code with the comment placed beside it!

# 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 = 300 # set the circle initial coordinates, set its radius as well
circle_center_y = 300
circle_radius = 60

# hat image load
pawn0 = pygame.image.load("pawn.png")
width, height = pawn0.get_size() # return width and height of the hat

# 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(200,200,200), (circle_center_y, circle_center_y), circle_radius) # Draw white 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

    # draw a hat on the top of circle
    screen.blit(pawn0, (circle_center_x-width/2, circle_center_y-circle_radius-height))

    pygame.display.flip() # refresh the screen

# Quit the game
pygame.display.quit()

That is basically it, wish you will like the above program!


Viewing all articles
Browse latest Browse all 22466

Trending Articles



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