Pygame - How to stop an image from leaving the edge of the screen?
Question: A section of jetfighterx leaves the screen when the mouse hovers over the edge
of the window, this causes tarantula to explode from time to time as soon as
it respawns to the top of the window, how can I stop this from happening
(without the use of classes)?

Code:

    
    
    import pygame, sys, pygame.mixer
    from pygame.locals import *
    import random 
    pygame.init()
    
    bif = "space.jpg"
    jf = "spacefightersprite.png"
    enemy = "TarantulaSpaceFighter.png"
    
    laser = pygame.mixer.Sound("LaserBlast.wav")
    explosionsound = pygame.mixer.Sound("Explosion.wav") 
    screen = pygame.display.set_mode((1000,900),0,32)
    caption = pygame.display.set_caption("Jet Fighter X") 
    background = pygame.image.load(bif).convert()
    
    jetfighterx = pygame.image.load(jf)
    jetfighterx = pygame.transform.scale(jetfighterx, (400,400)) 
    tarantula = pygame.image.load(enemy)
    tarantula = pygame.transform.scale(tarantula, (100,100)) 
    laserblast = pygame.image.load("C:\Python27\laser.png")
    explosion=pygame.image.load("C:\Python27\explosion.png")
    explosion=pygame.transform.scale(explosion, (150,150))
    
    ex,ey = 450,0
    movex,movey = 0,0
    clock = pygame.time.Clock()
    speed = 300
    shoot_y = 0
    laser_fired = False
    collision = False
    alive = True 
    explo_timer = 25
    
    while True:
        pygame.mouse.set_visible(False) 
        mx,my = pygame.mouse.get_pos() 
        jetfighterx_rect = jetfighterx.get_rect(center=(mx, my))
        jetfighterx_rect = jetfighterx_rect.inflate(-200,-200) 
        tarantula_rect = tarantula.get_rect(center=(ex, ey))
        tarantula_rect = tarantula_rect.inflate(-180,-200) 
        # Check for player inputs
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE or event.key == K_q:
                    sys.exit() 
            if event.type == MOUSEBUTTONDOWN:
                laser_fired = True
                laser.play()
                shoot_y = my-200
                shoot_x = mx-16
    
        # Update Game 
        milli = clock.tick()
        seconds = milli/1000. 
        dmy = seconds * speed
        ey += dmy
    
        if ey > 900:
            explo_timer = 25 
            collision = False
            alive = True 
            ey = 0
            ex = random.randint(50,900)
        if laser_fired:
            shoot_y -= 10
            if shoot_y < 0:
                laser_fired = False
            else:
                laserblast_rect = laserblast.get_rect(center=(shoot_x, shoot_y)) 
                if laserblast_rect.colliderect(tarantula_rect):
                    explosionsound.play()
                    collision = True
                    alive = False 
    
        if jetfighterx_rect.colliderect(tarantula_rect) and alive:
            explosionsound.play()
            collision = True
            alive = False 
    
      # Draw on screen
        screen.blit(background, (0,0))
        screen.blit(jetfighterx,(mx-200,my-200))
        if not collision:
            screen.blit(tarantula, (ex, ey))
        elif collision:
            explo_timer-=2
            if explo_timer > 0 and alive == False:
                screen.blit(explosion, (ex, ey-50))
        if laser_fired:
            screen.blit(laserblast, (shoot_x, shoot_y))
    
        pygame.display.update()
    


Answer: Just add a limit that does not allow the fighter to move within x pixels of
the border.

Assuming that the x,y coordinates of the centre of your fighter are
jetfighter_x, jetfighter_y (You will need to change the variable names to
whatever your code has) then write something like this:

    
    
    LBuffer = 16
    RBuffer = 1000 - 16
    TBuffer = 900 - 16
    BBuffer = 16
    
    if jetfighter_x > RBuffer:
        jetfighter_x = RBuffer
    
    if jetfighter_x < LBuffer:
        jetfighter_x = LBuffer
    
    if jetfighter_y > TBuffer:
        jetfighter_y = TBuffer
    
    if jetfighter_y < BBuffer:
        jetfighter_y = BBuffer
    

This should prevent the center of the ship from getting closer than 16 pixels
from the edge. Obviously you will need to tweak this to accommodate the size
of your ship. (The buffer for the sides would be the width of the image/2
.Respectively the buffer for the top and bottom would be the height of the
image/2).

