In this chapter, I am going to show you a simple python program that will move the image across the screen by clicking and then dragging it within the screen. While the image is in the dragging state I will make it semi-opaque. The game plan here is to set a few flags and basically make the image follow our mouse cursor.
Below is the entire code, why don’t you try it yourself?
# 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())) circle_center_x = 300 # set the circle initial coordinates, set its radius as well circle_center_y = 300 circle_radius = 60 # set the mouse press coordinates mouse_press = False mouse_coordinate_x = 0 mouse_coordinate_y = 0 # hat image load pawn0 = pygame.image.load("pawn.png") width, height = pawn0.get_size() # return width and height of the hat # the initial position of the hat image original_image_x = circle_center_x-width/2 original_image_y = circle_center_y-circle_radius-height # 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)) # check the left mouse button press state left_mouse_button_press_state = pygame.mouse.get_pressed(num_buttons=3)[0] if (left_mouse_button_press_state == True and mouse_press == False): mouse_coordinate_x, mouse_coordinate_y = pygame.mouse.get_pos() if (mouse_coordinate_x >= original_image_x and mouse_coordinate_x <= original_image_x + width and mouse_coordinate_y >= original_image_y and mouse_coordinate_y <= original_image_y + height): mouse_press = True elif (left_mouse_button_press_state == True and mouse_press == True): original_image_x, original_image_y = pygame.mouse.get_pos() pawn0.set_alpha(100) elif (left_mouse_button_press_state == False and mouse_press == True): mouse_press = False original_image_x, original_image_y = pygame.mouse.get_pos() pawn0.set_alpha(255) pygame.draw.circle(screen, pygame.Color(255,255,255), (circle_center_y, circle_center_y), circle_radius) # Draw white circle at the middle of screen screen.blit(pawn0, (original_image_x, original_image_y)) # drawing the hat pygame.display.flip() # refresh the display screen # Quit the game pygame.display.quit()
Here are a few outcomes, you can do it better by slightly adjusting the mathematical part of the equation!
Now it is time for me to get back to continue research on Pygame!