1

我正在使用 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] #OR 的范围内随机绘制多个此对象,但在同一范围内但设置坐标。两个实例我都需要这些图像在 x 和/或 y 方向上在该范围内移动 #randomly,距离为 #image 的大小(跳跃)。

4

1 回答 1

1

创建多人游戏

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.png, ball2.pngball3.png所以我不使用.convert()and .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()

在此处输入图像描述

于 2013-11-13T20:37:02.847 回答