0

如何让 Pacman 中的幽灵随机移动?我想出了如何移动自己的播放器。我尝试使用 random.randiant 命令,但它一直出现空白屏幕。我尝试对所有图像进行 bliting,但仍然出现空白屏幕。我只是想先对幽灵进行实验,然后再对其进行编程以杀死玩家。我正在运行 Window 7、Python 3.1 和 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()

注意:我不会为我的吃豆人游戏设置界限。幽灵可以在 Pygame 屏幕显示周围自由移动。

4

2 回答 2

0

我想你要的是

class Ghost():
    def __init__(self):
        self.x, self.y = 0, 0
        self.movex, self.movey = 0, 0

    def move(self):
        # move in random directions.

        self.movex = random.randint(-1,1)
        self.movey = random.randint(-1,1)
        self.x += movex
        self.y += movey
于 2016-08-05T15:10:03.333 回答
0

所以这是我的吃豆人游戏,我希望它有所帮助

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
于 2016-08-05T13:51:21.827 回答