Untitled

 avatar
unknown
python
2 years ago
4.1 kB
3
Indexable
import pygame
import sys
import random
import numpy
import math

s_hight = 1000
s_lenght = 1000
pygame.init()
screen = pygame.display.set_mode((s_lenght, s_hight))
pygame.display.set_caption("Grid using Rect Objects")

ROWS = 10
COLS = 10
MARGIN = 1
CELL_SIZE_X = (s_hight + MARGIN)/ ROWS 
CELL_SIZE_Y = (s_lenght+  MARGIN) / COLS 
count_enemy = random.randint(20,50)
res = 0
score = 0
sc_plus = 1


s = [(0, 0, 0), (0, 204, 0), (0, 204, 0),(255,0,0),(120,70,30)]
space = (0, 0, 0)
field = (0, 150, 0)
enemy = ()
destroyed = ()

df = numpy.ones((ROWS, COLS), int)

for i in range(count_enemy):
    x = random.randint(0, ROWS - 1)
    y = random.randint(0, COLS - 1)
    if df[x][y] == 1:
        rand1 = random.randint(0,100)
        rand2 = random.randint(0,1) #gor/vert
        if rand1 <= 50:
            df[x][y] = 2
            res += 1
        

        elif rand1 > 50 and rand1 <= 80:
            if rand2 == 0:
                if x < 1:
                    df[x][y] = 2
                    df[x+1][y] = 2
                    
                else:
                    df[x][y] = 2
                    df[x-1][y] = 2
                    
            else:
                if y < 1:
                    df[x][y] = 2
                    df[x][y+1] = 2
                    
                else:
                    df[x][y] = 2
                    df[x][y-1] = 2
                   

        elif rand1 > 80 and rand1 <= 95:
            if rand2 == 0:
                if x < 2:
                    df[x][y] = 2
                    df[x+1][y] = 2
                    df[x+2][y] = 2
                
                else:
                    df[x][y] = 2
                    df[x-1][y] = 2
                    df[x-2][y] = 2
                
            else:
                if y < 2:
                    df[x][y] = 2
                    df[x][y+1] = 2
                    df[x][y+2] = 2
                
                else:
                    df[x][y] = 2
                    df[x][y-1] = 2
                    df[x][y-2] = 2
                
        else:
            if rand2 == 0:
                if x < 3:
                    df[x][y] = 2
                    df[x+1][y] = 2
                    df[x+2][y] = 2
                    df[x+3][y] = 2
                 
                else:
                    df[x][y] = 2
                    df[x-1][y] = 2
                    df[x-2][y] = 2
                    df[x-3][y] = 2
                
            else:
                if y < 3:
                    df[x][y] = 2
                    df[x][y+1] = 2
                    df[x][y+2] = 2
                    df[x][y+3] = 2
               
                else:
                    df[x][y] = 2
                    df[x][y-1] = 2
                    df[x][y-2] = 2
                    df[x][y-3] = 2
                

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            # print(event.pos)
            pos = event.pos
            x, y = math.floor(pos[0] // (CELL_SIZE_X + MARGIN)), math.floor(pos[1] // (CELL_SIZE_Y + MARGIN))
            if df[x, y] == 3:
                1 == 1
            if df[y, x] == 2:    
                score += sc_plus
                df[y, x] = 3
                print('Your score is', score)
            elif df[y, x] != 3:
                df[y, x] = 4


            


    grid = []
    for row in range(ROWS):
        grid.append([])
        for col in range(COLS):
            rect = pygame.Rect(col * (CELL_SIZE_X + MARGIN), row * (CELL_SIZE_Y + MARGIN), CELL_SIZE_X, CELL_SIZE_Y)
            grid[row].append(rect)

    for row in range(ROWS):
        for col in range(COLS):
            pygame.draw.rect(screen, s[df[row, col]], grid[row][col])
            pygame.draw.rect(screen, s[0], grid[row][col], 1)
    pygame.display.flip()
Editor is loading...
Leave a Comment