How to make Pacman's ghost randomly move around in Python?
Question: How do I make a ghost in Pacman move around randomly? I figured out how to
move your own player. I tried using the random.randiant command but instead it
kept coming up with a blank screen. I tried bliting all the images but it
still keeps coming up with a blank screen. I just want to experiment with the
ghost first before I program it to kill the player. I'm running Window 7,
Python 3.1 and Pygame 3.1.

    
    
    import pygame, sys
    from pygame.locals import *
    
    pygame.init()
    
    windowSurface = pygame.display.set_mode((640,480), 0, 32)
    pygame.display.set_caption('Pacman')
    
    background = pygame.image.load('back.jpg').convert()
    pacman = pygame.image.load('aimball.png').convert_alpha()
    ghost = pygame.image.load('ghosts.png').convert_alpha()
    food = pygame.image.load('food.png').convert_alpha()
    
    windowSurface.blit(background, (0,0))
    windowSurface.blit(pacman, (0,0))
    windowSurface.blit(pacman,(x,y))
    windowSurface.blit(ghost, (100,100))
    windowSurface.blit(ghost, (x,y ))
    windowSurface.blit(food, (0,0))
    
    def pacman():
        x, y = 0,0
        movex, movey = 0,0
    
        while True:
            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()
    
                elif event.type == KEYDOWN:
                    if event.key == K_q:
                        pygame.quit()
                        sys.exit()
                    elif event.key == K_LEFT:
                        movex = -1
                    elif event.key == K_RIGHT:
                         movex = +1
                    elif event.key == K_UP:
                        movey = -1
                    elif event.key == K_DOWN:
                          movey = +1
    
                elif event.type == KEYUP:
                    if event.key == K_LEFT:
                         movex = 0
                    elif event.key == K_RIGHT:
                         movex = 0
                    elif event.key == K_UP:
                         movey = 0
                    elif event.key == K_DOWN:
                         movey = 0
    
                x+=movex
                y+=movey
    
        def ghosts():
            x, y = 0,0
            movex, movey = 0,0
    
            while True:
                random.randiant = move(1,2,3,4)
    
                if random.randiant == 1:
                    movex=-1
                elif random.randiant == 2:
                    movex=+1
                elif random.randiant == 3:
                    movey=-1
                elif random.randiant == 4:
                    movey=+1
    
                x+=movex
                y+=movey
    
    
    
            windowSurface.blit(background,(0,0))
            windowSurface.blit(pacman,(x,y))
            windowSurface.blit(pacman, (0,0))
            windowSurface.blit(ghost, (x,y ))
            windowSurface.blit(ghost, (100,100))
            windowSurface.blit(food, (0,0))
    
            pygame.display.update()
            pygame.display.flip()
    

NOTE: I will not be making boundaries for my Pacman game. The ghost can move
freely around the Pygame screen display.


Answer: so here is my pacman game, i hope it helps

    
    
    import pygame
    
    from pygame.locals import *
    
    from sys import exit
    
    import random as r
    
    import time
    
    # SET CHARECTERS POSITIONS START
    
    player_x = 400
    player_y = 200
    
    ghost_1_x = -50
    ghost_1_y = r.randint(1, 400)
    
    ghost_2_x = r.randint(1, 800)
    ghost_2_y = -50
    
    ghost_3_x = 850
    ghost_3_y = r.randint(1, 400)
    
    ghost_4_x = r.randint(1, 800)
    ghost_4_y = 450
    
    point_x = r.randint(50, 750)
    point_y = r.randint(10, 390)
    
    # SET CHARECTERS POSITIONS END
    
    # EXTRA VARIABLES START
    points = .2
    
    speed = points
    
    running = True
    
    size = 20
    
    # EXTRA VARIABLES END
    
    # START THE BEGINING OF THE MAIN PYTHON CODE START
    
    pygame.init()
    
    # START THE BEGINING OF THE MAIN PYTHON CODE END
    
    # SET SCREEN SIZE START
    
    screen = pygame.display.set_mode((800, 400))
    
    # SET SCREEN SIZE END
    
    # SET TITLE START
    
    pygame.display.set_caption('my pac_man')
    
    # SET TITLE END
    
    # LOADING THE IMAGES OF THE CHARACTARS START
    
    player = pygame.image.load ('pacman.png').convert()
    
    ghost_1 = pygame.image.load('ghost1.png').convert()
    ghost_2 = pygame.image.load('ghost2.png').convert()
    ghost_3 = pygame.image.load('ghost1.png').convert()
    ghost_4 = pygame.image.load('ghost2.png').convert()
    
    point = pygame.image.load('Star.png').convert()
    
    # LOADING THE IMAGES OF THE CHARECTERS END
    
    
    
    # DEFINING DIRECTIONS START
    
    up = 1
    down = 2
    left = 3
    right = 4
    
    # DEFINING DIRECTIONS END
    
    # DEFINING STARTING DIRECTION VARIABLE START
    
    direction = up
    
    # DEFINING STARTING DIRECTION VARIABLE END
    
    # MAIN GAME LOOP START
    
    while running:
    
    # TRANSFORMING THE IMAGES SO THAT THEY FIT ON THE SCREEN START
    
        player = pygame.transform.scale(player, (size, size))
    
        ghost_1 = pygame.transform.scale(ghost_1, (size, size))
        ghost_2 = pygame.transform.scale(ghost_2, (size, size))
        ghost_3 = pygame.transform.scale(ghost_1, (size, size))
        ghost_4 = pygame.transform.scale(ghost_2, (size, size))
    
        point = pygame.transform.scale(point, (size, size))
    
    # TRANSFORMING THE IMAGES SO THAT THEY FIT ON THE SCREEN END
    # EXTRA VARIABLES NEEDED IN GAME LOOP START
    
        speed = points
    
    # EXTRA VARIABLES NEEDED IN GAME LOOP END
    
    # LOOK FOR EVENTS IN PYGAME START
    
        for event in pygame.event.get():
    
    # CHECK IF THE MOUSE HITS THE X START     
    
            if event.type == pygame.QUIT:
    
    # IF THE MOUSE HITS THE X THEN EXIT START
    
                pygame.quit()
                exit()
    
    # IF THE MOUSE HITS THE X THEN EXIT END
    
    #CHECK IF THE MOUSE HITS THE X END
    
    # SENCE A KEY IS PRESSED START
    
            if event.type == pygame.KEYDOWN:
    
    # SENCE IF AN ARROW KEY IS PRESSED
    
                if event.key == pygame.K_LEFT:
    
                    direction = left
    
                if event.key == pygame.K_RIGHT:
    
                    direction = right
    
                if event.key == pygame.K_UP:
    
                    direction = up  
    
                if event.key == pygame.K_DOWN:
    
                    direction = down
    
    # SENCE IF AN ARROW KEY IS PRESSED END
    
    # SENCE IF A KEY IS PERSSED END
    
    # LOOK FOR EVENTS IN PYGAME END
    
    # PLAYER MOVEMENT START
    
        if direction == up: 
    
            player_y -= speed
    
        if direction == down:
    
            player_y += speed
    
        if direction == left:
    
            player_x -= speed
    
        if direction == right:
    
            player_x += speed
    
    # PLAYER MOVEMENT END
    
    # PLAYER EDGE SENCING START
    
        if player_x >= 800:
    
            running = False
    
        if player_x <= 0:
    
            running = False
    
        if player_y >= 400:
    
            running = False
    
        if player_y <= 0:
    
            running = False
    
    # PLAYER EDGE SENCING END
    
    # GHOST 1 MOVEMENT START
    
        if ghost_1_x <= player_x:
    
            ghost_1_x += speed / 2
    
        if ghost_1_x >= player_x:
    
            ghost_1_x -= speed / 2
    
        if ghost_1_y <= player_y:
    
            ghost_1_y += speed / 2
    
        if ghost_1_y >= player_y:
    
           ghost_1_y -= speed / 2
    
    # GHOST 1 MOVEMSNT END
    
    # GHOST 2 MOVEMENT START
    
        if ghost_2_x <= player_x:
    
            ghost_2_x += speed / 2
    
        if ghost_2_x >= player_x:
    
            ghost_2_x -= speed / 2
    
        if ghost_2_y <= player_y:
    
            ghost_2_y += speed / 2
    
        if ghost_2_y >= player_y:
    
            ghost_2_y -= speed / 2
    
    # GHOST 2 MOVEMSNT END
    
    # GHOST 3 MOVEMENT START
    
        if ghost_3_x <= player_x:
    
            ghost_3_x += speed / 2
    
        if ghost_3_x >= player_x:
    
            ghost_3_x -= speed / 2
    
        if ghost_3_y <= player_y:
    
            ghost_3_y += speed / 2
    
        if ghost_3_y >= player_y:
    
            ghost_3_y -= speed / 2
    
    # GHOST 3 MOVEMSNT END
    
    # GHOST 4 MOVEMENT START
    
        if ghost_4_x <= player_x:
    
            ghost_4_x += speed / 2
    
        if ghost_4_x >= player_x:
    
            ghost_4_x -= speed / 2
    
        if ghost_4_y <= player_y:
    
            ghost_4_y += speed / 2
    
        if ghost_4_y >= player_y:
    
            ghost_4_y -= speed / 2
    
    # GHOST 4 MOVEMSNT END
    
    # BACKGROUND COLOR START
    
        screen.fill((0, 0, 0))
    
    # BACKGROUND COLOR END
    
    # collision sencing format
    
    # if rect_1 x < rect_2 x + rect_1 width and rect_1 x + rect_2 width > rect_2 x and rect_1 y < rect_2 y + rect_1 height and rect_1 height + rect_1 y > rect_2 y
    
    # CHECKING FOR COLLISION START
    
        if player_x < ghost_1_x + size and player_x + size > ghost_1_x and player_y < ghost_1_y + size and size + player_y > ghost_1_y:
    
            running = False
    
        if player_x < ghost_2_x + size and player_x + size > ghost_2_x and player_y < ghost_2_y + size and size + player_y > ghost_2_y:
    
            running = False
    
        if player_x < ghost_3_x + size and player_x + size > ghost_3_x and player_y < ghost_3_y + size and size + player_y > ghost_3_y:
    
            running = False  
    
        if player_x < ghost_4_x + size and player_x + size > ghost_4_x and player_y < ghost_4_y + size and size + player_y > ghost_4_y:
    
            running = False
    
        if player_x < point_x + size and player_x + size > point_x and player_y < point_y + size and size + player_y > point_y:
    
            points += 0.1
            size += 5
            point_x = r.randint(50, 750)
            point_y = r.randint(10, 390)
    
    # CHECKING FOR COLLISION END
    
    # PLACE CHARACTERS START
    
        screen.blit(player, (player_x, player_y))
    
        screen.blit(ghost_1, (ghost_1_x, ghost_1_y))
        screen.blit(ghost_2, (ghost_2_x, ghost_2_y))
        screen.blit(ghost_3, (ghost_3_x, ghost_3_y))
        screen.blit(ghost_4, (ghost_4_x, ghost_4_y))
    
        screen.blit(point, (point_x, point_y))
    
    # PLACE CHARECTERS END
    # SHOW SCORE START
    
        font = pygame.font.Font(None, size)
    
        if size == 20:
    
            text = font.render(('20'), 1, (255, 0, 0))
    
        if size == 25:
    
            text = font.render(('25'), 1, (255, 0, 255))
    
        if size == 30:
    
            text = font.render(('30'), 1, (255, 255, 0))
    
        if size == 35:
    
            text = font.render(('35'), 1, (0, 255, 0))
    
        if size == 40:
    
            text = font.render(('40'), 1, (0, 0, 255))
    
        if size == 45:
    
            text = font.render(('45'), 1, (255, 0, 255))
    
        if size == 50:
    
            text = font.render(('50'), 1, (255, 255, 255))
    
        if size == 55:
    
            text = font.render(('55'), 1, (255, 255, 255))
    
        if size == 60:
    
            text = font.render(('YOU WIN'), 1, (255, 255, 255))
    
        screen.blit(text, (200,200))
    
    # SHOW SCORE END
    # UPDATE CHANGES IN CODE, VARIABLES, PICTURES, ECT... IN PYGAME START
    
        pygame.display.update()
    
    # UPDATE CHANGES IN CODE, VARIABLES, PICTURES, ECT... IN PYGAME END
    
    # MAIN GAME LOOP END
    

