【问题标题】:How put static initial sprite and after give a random movement on pygame?如何放置静态初始精灵并在pygame上随机移动?
【发布时间】:2014-05-28 15:06:41
【问题描述】:

我需要创建一个可以通过滚动技术移动的平台。 这个平台在开始时应该有 8 个静态基地,其中将包含最初的 8 个机器人。在按下 F1 后,这些机器人必须在任何方向(上、下、右、左)上随机移动。 现在我让系统运行并通过读取精灵来生成滚动平台。 但我不知道要做的是把8个基地放在最开始,机器人在基地上初始化,当我按F1时,机器人开始向平台的任何方向移动。

示例:机器人在按下 F1 后移动

http://i.stack.imgur.com/zGnGL.png

这是精灵:

http://i.stack.imgur.com/2zAL0.png

这是我的滚动平台代码。

import os, pygame
from pygame.locals import *

# game constants
SCREENRECT = Rect(0, 0, 1760, 880)

def imgcolorkey(image, colorkey):
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0, 0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image

def load_image(filename, colorkey = None):
    filename = os.path.join(filename)
    image = pygame.image.load(filename).convert()
    return imgcolorkey(image, colorkey)

class SpriteSheet:
    def __init__(self, filename):
        self.sheet = load_image(filename)
    def imgat(self, rect, colorkey = None):
        rect = Rect(rect)
        image = pygame.Surface(rect.size).convert()
        image.blit(self.sheet, (0, 0), rect)
        return imgcolorkey(image, colorkey)
    def imgsat(self, rects, colorkey = None):
        imgs = []
        for rect in rects:
            imgs.append(self.imgat(rect, colorkey))
        return imgs

class Arena:
    speed = 2
    def __init__(self):
        w = SCREENRECT.width
        h = SCREENRECT.height
        self.tileside = self.oceantile.get_height()
        self.horizontal = self.oceantile.get_width()
        self.counter = 0
        self.ocean = pygame.Surface((w, h + self.tileside)).convert()
        self.oceantwo = pygame.Surface((w + self.horizontal, h)).convert()
        for x in range(w/self.tileside):
            for y in range(h/self.tileside + 1):
                self.ocean.blit(self.oceantile, (x*self.tileside, y*self.tileside))
        for y in range(h/self.horizontal):
            for x in range(h/self.horizontal):
                self.oceantwo.blit(self.oceantile, (x*self.horizontal, y*self.horizontal))        
    def increment(self):
        self.counter = (self.counter - self.speed) % self.tileside
    def decrement(self):
        self.counter = (self.counter + self.speed) % self.tileside

    def rightmove(self):
        self.counter = (self.counter + self.speed) % self.horizontal

    def leftmove(self):
        self.counter = (self.counter - self.speed) % self.horizontal


def main():
    pygame.init()

    # set the display mode
    winstyle = 0
    bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32)
    screen = pygame.display.set_mode((792, 440), winstyle, bestdepth)

    # load images, assign to sprite classes
    # (do this before the classes are used, after screen setup)
    spritesheet = SpriteSheet('sprite.png')

    Arena.oceantile = spritesheet.imgat((10, 37, 44, 44))

    # decorate the game window
    pygame.display.set_caption('press UP, DOWN, ESC, or Q')

    # initialize game groups
    all = pygame.sprite.RenderPlain()

    # initialize our starting sprites
    arena = Arena()

    # to help understand how scrolling works, press ESC
    clip = True

    clock = pygame.time.Clock()

    while 1:

        # get input
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    screen.fill((0, 0, 0))
                    pygame.display.flip()
                    clip = not clip
                elif event.key == K_q:
                    return

        if pygame.key.get_pressed()[K_UP]:
            arena.decrement()
        if pygame.key.get_pressed()[K_DOWN]:
            arena.increment()

        if pygame.key.get_pressed()[K_RIGHT]:
            arena.rightmove()
        if pygame.key.get_pressed()[K_LEFT]:
            arena.leftmove()




        # update all the sprites
        all.update()

        # draw the scene
        if clip:
            screen.blit(arena.ocean, (0, 0), (0, arena.counter, SCREENRECT.width, SCREENRECT.height))
        else:
            screen.fill((0, 0, 0))
            screen.blit(arena.ocean, (0, arena.tileside-arena.counter))
        all.draw(screen)
        pygame.display.flip()
        clock.tick(30)

if __name__ == '__main__': main()

我该怎么做? 有人可以帮助我吗? 非常感谢!

【问题讨论】:

    标签: python image pygame sprite


    【解决方案1】:

    咳咳,python 的random 模块呢? https://docs.python.org/2/library/random.html

    如何将静态初始图像(基地和机器人)放在场地上

    我建议为从pygame.sprite.Sprite 继承的它们创建单独的类。所以每个机器人和基地都会有一个rect 属性来描述精灵的尺寸和位置。然后你可以为这些矩形设置随机坐标。

    robot = Robot()
    robot.rect = (random.randint(0, MAX_X), random.randint(0, MAX_Y), robot_width, robot_height)
    

    关于使用精灵请参考pygame的“黑猩猩”教程。

    【讨论】:

    • 感谢 Anton Melnikov 提供的随机指南,但我如何才能将静态初始图像(基地和机器人)放在场上??
    • 你得到一个随机随机数或随机整数,然后你把它们放在那些位置上。所以你创建了一个新的 sprite,x 等于 random.randint(1, SCREENRECT.width),y 等于 random.randint(1, SCREENRECT.height)
    猜你喜欢
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多