在与Pygame的游戏中随机出现

时间:2018-05-09 12:28:34

标签: python random pygame

我有一个问题,事实上,我希望我能独自做到这一点,但我想我不知道怎么做,我有一个像你知道鼹鼠的游戏,我的问题是:

我想让它看起来随机但是在非常精确的坐标上 那是我的代码可以帮到我吗?  我的代码还没有完成,所以有可能其他东西看起来很奇怪。

import pygame    
pygame.init()    
display_width = 600
display_height = 480

gameDisplay = pygame.display.set_mode((display_width, display_height))
fond = pygame.image.load("background.jpg").convert()
gameDisplay.blit(fond, (0, 0))
pygame.display.set_caption('Tape Taupe')
clock = pygame.time.Clock()

BAMimg = pygame.image.load('Marteau.png')
gameIcon = pygame.image.load('Taupe.png').convert_alpha()
pygame.display.set_icon(gameIcon)

BAMimg_width = 73
Taupe1, Taupe2, Taupe3, Taupe4, Taupe5, Taupe6 = pygame.image.load("Taupe.png").convert_alpha()

Taupe = [Taupe1, Taupe2, Taupe3, Taupe4, Taupe5, Taupe6]
for i in range(6):
        {

        }

perso1 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso1, (160, 55))

perso2 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso2, (320, 55))

perso3 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso3, (480, 55))

perso4 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (160, 200))

perso5 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (320, 200))

perso6 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (480, 200))

perso7 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (160, 350))

perso8 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (320, 350))

perso9 = pygame.image.load("troutaup.png").convert_alpha()
gameDisplay.blit(perso4, (480, 350))

pygame.display.update()


def BAMImg(x, y):
    gameDisplay.blit(BAMImg, (x, y))
    x = (display_width * 0.45)
    y = (display_height * 0.8)

def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0
    y_change = 0

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_UP:
                    y_change = -5
                if event.key == pygame.K_DOWN:
                    y_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0

                BAMimg(x, y)
                x += x_change
                y += y_change

                fond = pygame.image.load("background.jpg").convert()
                gameDisplay.blit(fond, (0, 0))
                pygame.display.update()
                clock.tick(60)


game_loop()
pygame.quit()
quit()

1 个答案:

答案 0 :(得分:4)

如果您希望它们仅出现在特定坐标处,请列出所有坐标。然后使用random.choice。 例如:

import random

lst_of_coordinates= [[1,2], [2,1], [5,5], [6,6]]
print(random.choice(lst_of_coordinates))