我需要创建一个可以通过滚动技术移动的平台。 这个平台应该在开始时有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的
random
模块呢?https://docs.python.org/2/library/random.html我建议为从
pygame.sprite.Sprite
继承的类创建单独的类。所以每个机器人和基地都会有一个rect
属性来描述精灵的尺寸和位置。然后你可以设置这些矩形的随机坐标。请参考pygame的“黑猩猩”教程,了解如何使用精灵。
相关问题 更多 >
编程相关推荐