怎么把静态初始精灵和pygame后随机移动?

时间:2014-05-28 15:06:42

标签: python image pygame sprite

我需要创建一个可以通过滚动技术进行移动的平台。 这个平台应该在开始时有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()

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

1 个答案:

答案 0 :(得分:1)

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

  

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

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

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

请参阅pygame的“黑猩猩”教程,了解如何使用精灵。