We are trying to create a program using pygame that allows preset image files to be dragged around over another PNG image as the background. Used some code from a tutorial for pygame and tweaked it to fit our specific needs. We have gotten the images to populate and are movable but only with a white background. When importing the PNG file for the background instead, the moveable image disappears behind the background. I have replace the the two filepaths with just "filepath." Still fairly new to python so sorry for ugly code.
Code:
# Import the library pygame
import pygame
from pygame.locals import *
##background
bg = pygame.image.load(filepath)
# Take colors input
YELLOW = (255, 255, 0)
BLUE = (0, 0, 255)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLACK = (0, 0, 0)
# Construct the GUI game
pygame.init()
# Set dimensions of game GUI
w, h = 1280, 720
screen = pygame.display.set_mode((w, h))
# Take image as input
img = pygame.image.load(filepath)
img.convert()
# Draw rectangle around the image
rect = img.get_rect()
rect.center = w//2, h//2
# Set running and moving values
running = True
moving = False
# Setting what happens when game
# is in running state
while running:
screen.blit(bg, (320,0))
for event in pygame.event.get():
# Close if the user quits the
# game
if event.type == QUIT:
running = False
# Making the image move
elif event.type == MOUSEBUTTONDOWN:
if rect.collidepoint(event.pos):
moving = True
# Set moving as False if you want
# to move the image only with the
# mouse click
# Set moving as True if you want
# to move the image without the
# mouse click
elif event.type == MOUSEBUTTONUP:
moving = False
# Make your image move continuously
elif event.type == MOUSEMOTION and moving:
rect.move_ip(event.rel)
# Set screen color and image on screen
screen.fill(WHITE)
screen.blit(img, rect)
# Construct the border to the image
pygame.draw.rect(screen, BLACK, rect, 2)
# Update the GUI pygame
pygame.display.update()
##trying to draw the background
# Quit the GUI game
pygame.quit()