pygame随机显示多个​​图像 [英] Pygame display multiple images randomly

查看:102
本文介绍了pygame随机显示多个​​图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pygame,并且能够在屏幕上绘制多条线.我用以下命令制作的:

I am working with pygame and I was able to draw multiple lines on a screen. I made with this command:

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

我现在需要在屏幕上绘制多个图像.我能够通过执行以下操作绘制一张图像:

I now need to draw multiple images on the screen. I was able to draw one image by doing:

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] #OR范围内在相同范围内但已设置坐标的情况下随机绘制此对象的多个.这两个实例都需要这些图像在x和/或y方向上在#范围内随机移动#图像大小(跳跃).

I need to draw multiple of this object EITHER randomly within a range of [60, 0], [220, 475] #OR within the same range but with set coordinates. BOTH instances I need these images to move #randomly within that range in either x and/or y direction with a distance of the size of the #image (jump).

推荐答案

创建多人游戏

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

您更改玩家位置的方法相同.

The same way you can change players position.

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

But better way is to make Player class with all needed function.

(它不是完整的代码-因此未经测试)

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除外),图像将更改位置.

Full working example - if you press any key (except ESC) images change position.

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

I use transparent PNG ball1.png, ball2.png, ball3.png so I don't use .convert() and .set_colorkey().

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

Images you can find (and download) in answer to questions:

  • Space invaders project
  • Pygame- window and sprite class - python

.

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

这篇关于pygame随机显示多个​​图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆