Trying to Make Slime Bombs and clear screen with spacebar

Viewed 76
import pygame

pygame.init()
screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
isRunning = True
r = 255
g = 255
b = 255
color = (r, g, b)
gridDraw = 0
mouse_pressed = []

def grid():
    x = 50
    y = 50
    for a in range(10):
        for c in range(10):
            pygame.draw.circle(screen, color, (x, y), 10)
            x += 45
        y += 45
        x = 50

def slime(mPos):
    x = 50
    y = 50
    green = (0,255,0)
    for a in range(10):
        for c in range(10):
            if (mPos[0] <= x+10 and mPos[0] >= x-10) and\
               (mPos[1] <= y+10 and mPos[1] >= y-10):
                pygame.draw.circle(screen, green, (x, y), 10)
                if x + 45 <= 455:
                    pygame.draw.circle(screen, green, (x+45, y), 10)
                if x - 45 >= 45:
                    pygame.draw.circle(screen, green, (x-45, y), 10)
                if y + 45 <= 455:
                    pygame.draw.circle(screen, green, (x, y+45), 10)
                if y - 45 >= 45:
                    pygame.draw.circle(screen, green, (x, y-45), 10)
            x += 45
        y += 45
        x = 50

def bomb(mouse):
    for a in mouse:
        slime(a)
        pygame.draw.circle(screen, (0,200,150), a, 8)

while isRunning:
    event = pygame.event.get()
    for e in event:
        if e.type == pygame.QUIT:
            isRunning = False
        elif e.type == pygame.KEYDOWN:
            if e.type == pygame.K_ESCAPE:
                isRunning = False
        if e.type == pygame.MOUSEBUTTONDOWN:
            mpos = pygame.mouse.get_pos()
            mouse_pressed.append(mpos)
            gridDraw = 0

    if gridDraw == 0:
        grid()
        gridDraw += 1
        bomb(mouse_pressed)




    pygame.display.update()
pygame.quit()

So I have most of the first part of the assignment done except I can still press in the black spaces and I cant figure out how to not, I thought I had gotten the constraints correct but apparently not. Im thinking about making right click the medium or large bomb, would this be ideal?

This is my lab for class, I finally was able to complete the placing small bombs but I havent put in screen wipe after I press and I can still press in the black spaces.

2 Answers

First of all you have to draw the circles continuously in the application loop rather than the event loop.

Get the current position of the muse by pygame.mouse.get_pos and the state of the mouse buttons by pygame.mouse.get_pressed:

mpos = pygame.mouse.get_pos()
mpressed = pygame.mouse.get_pressed()

Evaluate if the left mouse button is pressed (mpressed[0]) and the distance of the mouse to the center of the circle is less than the radius of the circle (pygame.math.Vector2.distance_to):

if mpressed[0] and pygame.math.Vector2(x, y).distance_to(mpos) < 10:
    # [...]

See the full example:

import pygame

screen = pygame.display.set_mode((500,500))
clock = pygame.time.Clock()
isRunning = True
x, y = 50, 50
r, g, b = 255, 255, 255

while isRunning:
    #Time
    deltaTime = clock.tick() / 1000
    event = pygame.event.get()
    #USER Events
    for e in event:
        if e.type == pygame.QUIT:
            isRunning = False

    mpos = pygame.mouse.get_pos()
    mpressed = pygame.mouse.get_pressed()

    #Draws a grid
    for a in range(50):
        x = a * 50
        for c in range(50):
            y = c * 50
            color = (r, g, b)
            if mpressed[0] and pygame.math.Vector2(x, y).distance_to(mpos) < 10:
                color = (0, g, 0)
            pygame.draw.circle(screen, color, (x, y), 10)

    pygame.display.update()
pygame.quit()

I used the previous answer for the mouse position and added a board_state so that the Circle stays in your desired color.

 import pygame
 import numpy as np
 import time

 def set_color(board_state):
     if not board_state:
         return 255, 255, 255
     else:
         return 255, 0, 0


 screen = pygame.display.set_mode((500,500))
 clock = pygame.time.Clock()
 isRunning = True
 board_state = np.zeros((50, 50), dtype=np.bool)
 pressed = True

 while isRunning:
     deltaTime = clock.tick() / 1000
     event = pygame.event.get()
     for e in event:
         if e.type == pygame.QUIT:
             isRunning = False

     mpos = pygame.mouse.get_pos()
     mpressed = pygame.mouse.get_pressed()
     for a in range(board_state.shape[0]):
         x = a * 50
         for c in range(board_state.shape[1]):
             y = c * 50
             if mpressed[0] and pygame.math.Vector2(x, y).distance_to(mpos) < 10:
                 board_state[a, c] = not board_state[a, c]
                 print("Kick", a, c, board_state[a, c])
                 pressed = True
             pygame.draw.circle(screen, set_color(board_state[a, c]), (x, y), 10)

     if pressed:
         pygame.display.update()
         time.sleep(0.2)
         pressed = False
 pygame.quit()

You can change the color in the set_color function and I added a time.sleep which needs to be adjusted to the klicking length, because I had some problem with detecting a single klick with the trackpad.

Related