Pygame:重叠自定位精灵

时间:2015-08-25 20:30:47

标签: python numpy pygame computational-geometry

我想用图像制作游戏(正方形看图像)。这些正方形必须重叠。重叠是固定的。我们从2个红色精灵开始,这些精灵属于他们自己的单个精灵组。一旦两个精灵随机放置(但在屏幕范围内)。我希望绿色的spites被放置在起始精灵的角落。角落的选择可以是1,2,3,6,7或8.该程序必须决定在哪个角落可以放置块。红色时钟的中心与输入的随机绿色块之间的距离是固定的,因此我将使用该参数来防止绿色块偏离红色块。绿色块也必须与红色块重叠(如图所示)。我不确定如何检测“未占用”的角落,即一个可以被进入的绿色区块自由占据的角落。检测未占用的角落似乎需要像素的颜色来区分说白色和绿色

#!/usr/bin/python

#This version works till printing the blit positions of 2 ryr's on the screen 

from __future__ import print_function 
import random
import pygame
import numpy


WHITE = (255,255,255)
BLACK = (0  ,0  ,0  )


class Player(pygame.sprite.Sprite):
    def __init__(self, image, x=0, y=0):
        ##calling the parent constructor we want to initialize the sprite class check super later 
        pygame.sprite.Sprite.__init__(self)
        super(Player,self).__init__()

        self.image = pygame.image.load(image).convert_alpha()
        self.sprite_list = []

        #rect of the image  with the image dimensions as arguments
        #self.frame = self.image
        self.rect = self.image.get_rect()

        self.x = self.rect.left
        self.y = self.rect.top   


   def update_tworyr(self):
       self.rect.topleft = random.randint(100,874), random.randint( 100, 618)


class Game():
    def __init__(self):
        pygame.init()

        self.screen = pygame.display.set_mode((1024,768))
        self.screen_rect = self.screen.get_rect()

        self.background = pygame.image.load("background.png").convert()

        # Make 2 groups 
        #1st group contains the 2 sprites to be placed together but at a random position on the screen
        #2nd group contains the other sprites that grow from the corners of the 1st placement
        self.ryr_list = pygame.sprite.Group()
        self.tworyr_group = pygame.sprite.GroupSingle()

        # create 3 balls 1...n number of the same image 
        self.tworyr_sprite  = Player('tworyr.png')

        for i in range(0,2):
            a_ryr = Player("ryr1.png")

        #calling an instance of player 
        # Create one sprite n number of sprites and put them in a random position on the screen
        #Now add each of the n sprites into the sprite group ryr_list
        self.tworyr_group.add(self.tworyr_sprite)

        # a_Ryr belongs to the Player () sprite class 
        #Next is to loop over that group of 4 sprites


    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
                self.tworyr_sprite.update_tworyr()
                print ("length of tworyr_sprite group is "+ str(len(self.tworyr_group)) )

            #DRAW
            #ryr sprites in self.ryr_list draw() draws all of them at once 
            self.screen.blit(self.background, self.background.get_rect())
            self.tworyr_group.draw(self.screen)

            pygame.display.flip()

            # --- FPS ---
            clock.tick(30)

        # --- quit ---
        pygame.quit()


Game().run()

enter image description here

在图像中我已经表明,绿色块只是对角线增长,但是只要它们与指定的重叠接触,它们就可以在任何角落生长。一旦我弄清楚如何检测角落,我就可以找出重叠,因为我觉得它是相互关联的

到目前为止,我刚刚在屏幕上随机放置了2个初始红色块,并试图充实自由角算法,任何输入都将非常感激。我是pygame的新手,所以我正在学习。

0 个答案:

没有答案