Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame:重叠自定位精灵_Python_Numpy_Pygame_Computational Geometry - Fatal编程技术网

Python Pygame:重叠自定位精灵

Python Pygame:重叠自定位精灵,python,numpy,pygame,computational-geometry,Python,Numpy,Pygame,Computational Geometry,我想做一个游戏使用图像(方块看到的图像)。这些正方形必须重叠。重叠是固定的。我们从两个红色精灵开始,它们属于各自的单个精灵组。一旦两个精灵随机放置(但在屏幕范围内)。我想把绿色的唾液放在起始雪碧的角落。角落的选择可以是1,2,3,6,7或8。程序必须决定在哪个角可以放置块。红色时钟中心和随机绿色块之间的距离是固定的,因此我将使用该参数来防止绿色块从红色块漂移。绿色块也必须与红色块重叠(如图所示)。我不知道如何检测“未占用”角,即一个可自由被进入的绿色块占用的角。检测未被占用的角点似乎需要像素的颜

我想做一个游戏使用图像(方块看到的图像)。这些正方形必须重叠。重叠是固定的。我们从两个红色精灵开始,它们属于各自的单个精灵组。一旦两个精灵随机放置(但在屏幕范围内)。我想把绿色的唾液放在起始雪碧的角落。角落的选择可以是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()

在这张图中,我展示了绿色块只是在对角线上生长,但是只要它们与指定的重叠部分接触,它们就可以在任何角落生长。一旦我知道了如何检测角点,我就可以知道重叠,因为我觉得它是相互关联的


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

这个问题还存在吗?我会研究它,但不想知道asker是否会不使用它,我怀疑两年前有人问过它,asker写道当时他是python新手。