Hi everyone! I'm new here, so just letting you know that.

Anyway. I'm currently writing somewhat of a game in pygame and had a few questions for anyone who would be able to help me. I have created a python program that moves and object in a window, and I have made one that creates a window with a background (Both are pretty basic scripts, but I'm new to pygame. (Both of these programs run)). I was wondering how I would merge the two together without screwing up my program.

Here is the code for the moving object:

import os, pygame
from pygame.locals import *

def main():

    screen = pygame.display.set_mode( (640,480) )
    
    background = pygame.Surface( screen.get_size() )

    background.fill( (255,255,255) )

    sprite = pygame.image.load( "down.bmp" )

    spriteRect = sprite.get_rect()

    spriteRect.centerx = (640 / 2)
    spriteRect.centery = (480 / 2)

    screen.blit( background, (0,0) )
    screen.blit( sprite, spriteRect )

    pygame.display.flip()

    while 1:
        pygame.event.pump()
        keyinput = pygame.key.get_pressed()
        
        if keyinput[K_ESCAPE] or pygame.event.peek(QUIT):
            break

        if keyinput[K_LEFT]:
            sprite = pygame.image.load( "left.bmp" )
            spriteRect.centerx -= 1
        
        if keyinput[K_RIGHT]:
            sprite = pygame.image.load( "right.bmp" )
            spriteRect.centerx += 1

        if keyinput[K_UP]:
            sprite = pygame.image.load( "up.bmp" )
            spriteRect.centery -= 1

        if keyinput[K_DOWN]:
            sprite = pygame.image.load( "down.bmp" )
            spriteRect.centery += 1

        screen.blit( background, (0,0) )
        screen.blit( sprite, spriteRect )

        pygame.display.flip()

if __name__ == '__main__': main()

and here is the code for the background program:

import pygame
from pygame.locals import *

def main():
    pygame.init()	

    screen = pygame.display.set_mode( (640,480) )

    background = pygame.Surface( screen.get_size() )

    background.fill( (0,0,0) )

    image = pygame.image.load( "background2.bmp" )

    imagePosition = image.get_rect()
    
    imagePosition.bottom = 480
    imagePosition.left = 0

    screen.blit( background, (0,0) )
    screen.blit( image, imagePosition )

    pygame.display.flip()

    while 1:
        pygame.event.pump()
    
        keyinput = pygame.key.get_pressed()

        if keyinput[K_ESCAPE] or pygame.event.peek(QUIT):
            break

and there you have it! Anyone out there that could help me?

sorry, I forgot to include that when I said "merge", I meant have my program that moves the object have the background of my other program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.