4

I want to make a game using images (the squares seen image). It is necessary that these squares overlap. The overlap is fixed. We start with the 2 red sprites which are in the single sprite group of their own. Once the two sprites are placed randomly ( but within screen bounds ) . I want the green spites to be placed at the corners of the starting sprite .The choice of corner can be 1,2,3,6,7.or 8. The program has to decide at which corner it is possible to place the block. The distance between the centre of the red clock and the incoming random green block is fixed so I will be using that parameter to prevent the green blocks from drifting away from the red ones. The green blocks must also overlap with the red ones (as shown). I am not sure how to go about detecting the "non occupied" corner, i.e. a corner that is free to be occupied by an incoming green block. Detecting a corner that is not occupied would seem to require the color of the pixel to distinguish between say white and green

#!/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

In the Image I have shown that the green blocks are just growing diagonally however they can grow at any corner as long as they touch with specified overlap. Once I figure out how to detect the corners I can figure out the overlap because I feel it is interrelated

So far I have just placed randomly the 2 initial red blocks on the screen and am trying to flesh out the free corner algorithm any inputs will be greatly appreciated. I am new to pygame so I am learning as I go.

cjhveal
  • 5,668
  • 2
  • 28
  • 38
  • 1
    Is this question still alive? I would look into it, but don't want to if _asker_ will not use it, which I suspect from that it was asked two years ago and _asker_ wrote he was new to python at that time. – Brambor Sep 13 '17 at 15:56

0 Answers0