【问题标题】:appear random in a game with Pygame在 Pygame 的游戏中随机出现
【发布时间】:2018-05-09 12:28:34
【问题描述】:

我有一个问题,事实上,我希望我能一个人做这个,但我想我不知道该怎么做,我有一个像你知道的游戏打地鼠,我的问题是:

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

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()

【问题讨论】:

  • 阅读random 模块的一些文档,您可能应该使用random.choice,希望对您有所帮助

标签: python random pygame


【解决方案1】:

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

import random

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

【讨论】:

    猜你喜欢
    • 2015-10-26
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多