python - Pygame随机显示多个​​图像

标签 python image pygame

我正在使用 pygame,我能够在屏幕上绘制多条线。我用这个命令做了:

for x in range(60,940,20):
        pygame.draw.line(screen, white, [x, 0], [x, 500], 1)

我现在需要在屏幕上绘制多个图像。我能够通过以下方式绘制一张图像:

player_two = pygame.image.load("player_two.png").convert()
player_two.set_colorkey(white)



while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        screen.blit(player_two, [60, 240])

        pygame.display.flip()

    clock.tick(20)

pygame.quit()

我需要在 [60, 0]、[220, 475] 范围内随机绘制该对象的多个#或者在相同范围内但具有设定坐标。在这两个实例中,我都需要这些图像在 x 和/或 y 方向上的该范围内随机移动,距离为 #image 的大小(跳跃)。

最佳答案

创建多人游戏

import random

multi_players = []
multi_players_pos = []

player = pygame.image.load("player_two.png").convert()
player.set_colorkey(white)

#create 10 players 0...9
for i in range(10):
    multi_players.append(player)
    pos = [random.randint(60,220+1), random.randint( 0, 475+1)]
    multi_players_pos.append(pos)

或加载player_0.png ... player_9.png:

multi_players = []
multi_players_pos = []

for i in range(10):
    temp = pygame.image.load("player_"+str(i)+".png").convert()
    temp.set_colorkey(white)
    multi_players.append(temp)
    pos = [random.randint(60,220+1), random.randint( 0, 475+1)]
    multi_players_pos.append(pos)

画出来:

while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        for i in range(len(multi_players)):
            screen.blit(multi_players[i], multi_players_pos)

        pygame.display.flip()

    clock.tick(20)

pygame.quit()

与改变玩家位置的方式相同。

但更好的方法是使 Player 类具有所有需要的功能。

(这不是完整的代码 - 所以没有测试)

import random

class Player():

    def __init__(self, image, x, y):
        self.image = pygame.image.load(image).convert()
        self.image.set_colorkey(white)
        image_rect = image.get_rect()

        self.rect = Rect(x, y, image_rect.width, image_rect.height)

    def draw(self, screen):
        screen.blit(self.image, self.rect.topleft)

    def update(self):
        # here change randomly positon

#--------------------------------------------

multi_players = []

#create 10 players 0...9
for i in range(10):
    x, y = random.randint(60,220+1), random.randint( 0, 475+1)
    multi_players.append(Player("player_"+str(i)+".png", x, y))

while done == False:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    # changes position

    for player in multi_players:
        player.update()

    # draws

    for player in multi_players:
        player.draw(screen)

    pygame.display.flip()

    clock.tick(20)

pygame.quit()
<小时/>

编辑:

完整的工作示例 - 如果按任意键(ESC 除外)图像会更改位置。

我使用透明 PNG ball1.pngball2.pngball3.png 所以我不使用 .convert ().set_colorkey()

您可以找到(并下载)用于回答问题的图像:

.

import random
import pygame

WHITE = (255,255,255)
BLACK = (0  ,0  ,0  )

#----------------------------------------------------------------------

class Player():

    def __init__(self, image, x=0, y=0):

        self.image = pygame.image.load(image)#.convert()
        #self.image.set_colorkey(WHITE)
        self.rect = self.image.get_rect()

        self.rect.centerx = x
        self.rect.centery = y

    #------------

    def draw(self, screen):
        screen.blit(self.image, self.rect)

    #------------

    def update(self):
        # here change randomly positon
        self.rect.topleft = random.randint(60,220+1), random.randint( 0, 475+1)

#----------------------------------------------------------------------

class Game():

    def __init__(self):

        pygame.init()

        self.screen = pygame.display.set_mode((800,600))

        self.background = pygame.image.load("background.jpg").convert()
        self.multi_players = []

        # create 3 balls 1...3

        for i in range(1,4):
            player = Player("ball"+str(i)+".png")
            player.update() # set random position on start
            self.multi_players.append(player)

    #------------

    def run(self):

        clock = pygame.time.Clock()
        RUNNING = True

        while RUNNING:

            # --- events ---

            for event in pygame.event.get():

                if event.type == pygame.QUIT:
                    RUNNING = False

                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_ESCAPE:
                        RUNNING = False

                    # changes position when key is pressed

                    for player in self.multi_players:
                        player.update()

            # --- updates ---- 

            # place for updates

            # --- draws ---

            self.screen.fill(BLACK)

            self.screen.blit(self.background, self.background.get_rect())

            for player in self.multi_players:
                player.draw(self.screen)

            pygame.display.flip()

            # --- FPS ---

            clock.tick(20)

        # --- quit ---

        pygame.quit()

#----------------------------------------------------------------------

Game().run()

enter image description here

关于python - Pygame随机显示多个​​图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19963271/

相关文章:

javascript - 图像的随机位置

jquery - 检查 jquery 插件启动前是否加载了图像

android - 在 Android 中获取 Google 个人资料图片

python - pygame 的问题

python-3.x - 使用 ALSA 在 RaspBerry Pi 4+ 上使用 PyGame 的音频 cdrom 没有声音

python - Pygame - 我可以让音乐有一个介绍然后是一个循环点吗?

python - 从边列表创建邻接矩阵

Python - 一次移动两个 turtle 对象

Python:如何标记此数据集

python 输入被读取错误?