How to create a delete function for deleting squares in pygame - pygame

I am trying to implement the diamond dash game using pygame. To be more specific, when you clicking with mouse on 3 squares with same colour in a row or column, then these squares must deleted and after new squares must take their position, randomly. My program can find the specific coordinates of a square but i am struggle on how to delete those specific squares.
Can you help me please? thank you.
import random, time, pygame, sys, copy
from pygame.locals import *
black = ( 0, 0, 0)
white = ( 255, 255, 255)
green = ( 0, 255, 0)
red = ( 255, 0, 0)
size = [700, 485]
screen=pygame.display.set_mode(size)
# This sets the width and height of each grid location
width = 64
height = 64
# This sets the margin between each cell
margin = 5
# Create a 2 dimensional array. A two dimesional
# array is simply a list of lists.
grid = []
for row in range(7):
# Add an empty array that will hold each cell
# in this row
grid.append([])
for column in range(80):
grid[row].append(0) # Append a cell
imgnum = 7
imglist = []
for i in range(1, imgnum+1):
dimge = pygame.image.load('imge%s.png' % i)
imglist.append(dimge)
grid[1][5] = 1
pygame.init()
pygame.display.set_caption("dimond dash")
done = False
for row in range(7):
for column in range(8):
screen.blit(random.choice(imglist), [(margin+width)*column+margin,
(margin+height)*row+margin,
width,
height])
while done == False:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
column = pos[0] // (width + margin)
row = pos[1] // (height + margin)
grid[row][column] = 1
print("Click ", pos, "Grid coordinates: ", row, column)
pygame.display.flip()
pygame.quit()

You need some sort of a data structure to store which cells will be deleted.
You can try this:
Generate your grid.
For each cell that is not in a group:
i. create a new group
ii. get the color of the cell
iii. if any of the neighbors have the same color, repeat the algorithm, but add the new cell to the group.
At the end you will have groups of cells.
Now when a player clicks on a cell, you look up the group list, and find the group that has been clicked. Don't know the rules, but here you can check if the group is big enough, or simply generate new cells in their places.
Now for each new cell, do a similar algorithm:
Check all neighbors - There are two possibilities.
i. If only one neighbor is the same color, add the new color to the neighbors
group.
ii. If there are more, check if they both are in the same
group, if so, add this cell, if not, you need to merge both groups and
add the cell.
EDIT:
There are 2 possibilities for getting the color
you images are one color - you can get the color at a point:
link
If you store your cells as sprites and checking the sprite image would tell you if they are the same color.
Actually now that I see it, your bliting loop makes you lose information about the cells.
It would be better if you store the results of random choice, and then blit them. This way you will know which cell is which.

Related

How can I concatenate the 4 corners of the image quickly when loading image in deep learning?

What is the most effective way to concatenate 4 corner, shown in this photo ?
(conducting in getitem())
left_img = Image.open('image.jpg')
...
output = right_img
This is how I would do it.
Firstly I would convert the image to a Tensor Image temporarily
from torchvision import transforms
tensor_image = transforms.ToTensor()(image)
Now assuming you have a 3 channel image (although similiar principles apply to any matrices of any number of channels including 1 channel gray scale images).
You can find the Red channel with tensor_image[0] the Green channel with tensor_image[1] and the the Blue channel with tensor_image[2]
You can make a for loop iterating through each channel like
for i in tensor_image.size(0):
curr_channel = tensor_image[i]
Now inside that for loop with each channel you can extract the
First corner pixel with float(curr_channel[0][0])
Last top corner pixel with float(curr_channel[0][-1])
Bottom first pixel with float(curr_channel[-1][0])
Bottom and last pixel with float(curr_channel[-1][-1])
Make sure to convert all the pixel values to float or double values before this next appending step
Now you have four values that correspond to the corner pixels of each channel
Then you can make a list called new_image = []
You can then append the above mentioned pixel values using
new_image.append([[curr_channel[0][0], curr_channel[0][-1]], [curr_channel[-1][0], curr_channel[-1][-1]]])
Now after iterating through every channel you should have a big list that contains three (or tensor_image.size(0)) number of lists of lists.
Next step is to convert this list of lists of lists to a torch.tensor by running
new_image = torch.tensor(new_image)
To make sure everything is right new_image.size() should return torch.Size([3, 2, 2])
If that is the case you now have your wanted image but it is tensor format.
The way to convert it back to PIL is to run
final_pil_image = transforms.ToPILImage()(new_image)
If everything went good, you should have a pil image that fulfills your task. The only code it uses is clever indexing and one for loop.
There is a possibility however if you look more than I can, then you can avoid using a for loop and perform operations on all the channels without the loop.
Sarthak Jain
I don't know how quick this is but here:
import numpy as np
img = np.array(Image.open('image.jpg'))
w, h = img.shape[0], image.shape[1]
# the window size:
r = 4
upper_left = img[:r, :r]
lower_left = img[h-r:, :r]
upper_right = img[:r, w-r:]
lower_right = img[h-r:, w-r:]
upper_half = np.concatenate((upper_left, upper_right), axis=1)
lower_half = np.concatenate((lower_left, lower_right), axis=1)
img = np.concatenate((upper_half, lower_half))
or short:
upper_half = np.concatenate((img[:r, :r], img[:r, w-r:]), axis=1)
lower_half = np.concatenate((img[h-r:, :r], img[h-r:, w-r:]), axis=1)
img = np.concatenate((upper_half, lower_half))

ValueError: <__main__.projectile object at 0x0000024699C2C670> is not in list

I've been trying to create a game with PyGame but sometimes it crashes due to this error:
ValueError: <main.projectile object at 0x0000024699C2C670> is not in list
Do you know any possible fixes?
As I've seen from other people it might be that I am modifying the bullets list while I'm iterating it but i can't find a solution even reading to this thread.
PS. I know that I can use a function to spawn the goblins and not just copy and paste but this is a quick project that I am trying to get over. This is the code:
def enemy():
def __init__(self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.walkCount = 0
self.vel = 3
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
self.health = 10
self.visible = True
bullets = []
goblin = enemy(100, 410, 64, 64, 450)
# mainloop
while run:
if not goblin.visible:
goblin.hitbox = 0
for bullet in bullets:
if 0 < bullet.x < 500:
bullet.x += bullet.vel
else:
if len(bullets) != 0:
bullets.pop(bullets.index(bullet))
The problem here seems to be that you iterate through the bullets in a for loop, and remove a bullet halfway through. The following line
for bullet in bullets:
will, in practice, go through the bullets 1 by 1 from the first to the last one, by index. That is, if your list is bullets = [bullet1, bullet2, bullet3] it will execute for bullets[0], then for bullets[1] and lastly for bullets[2]. The problem here appears when you remove a bullet from the list. Say bullet2 (which is bullets[1] gets removed in your loop. Now index will still jump up to 2, and bullets[2] will be accessed. However, there are now only two bullets, bullets = [bullet1, bullet3], and an error occurs.
If, on the other hand, bullet1 gets removed, bullets is now bullets = [bullet2, bullet3]. The loop continues to index 1, but this skips bullet2 altogether.
This is a very common problem in many programming languages. In many languages iterating the list from the last item to the first item resolves this, but this is not straightforward to do in python.
Have a look at this StackOverflow answer to get a better idea of your options.
A solution
One way to resolve the issue, which is not the most efficient but relatively easy to understand, is to construct a new list of bullets, while evaluating the bullets. Instead of removing bullets you'll be adding all bullets that persist in the new bullet list.
newBullets = []
for bullet in bullets:
if 0 < bullet.x < 500:
bullet.x += bullet.vel
# append bullet to keep it
newBullets.append(bullet)
# else don't do anything
# after the loop, update the bullets list
# which now contains only bullets that have an x between 0 and 500
bullets = newBullets

How to set the position of an image in pygame?

I created an object called "alien" in my game. Uploaded the image for "alien" and used "get_rect()" to set its rect attributes.
Now I want to change the x-coordinate value of "alien". Which of the following two ways is correct?
alien.x = ...... or alien.rect.x = ......
I saw in a textbook that the following codes are used:
alien.x = alien_width + 2 * alien_width * alien_number
alien.rect.x = alien.x
Why didn't the author directly use alien.rect.x to change the x-coordinate value of "alien"? Like:
alien.rect.x = alien_width + 2 * alien_width * alien_number
Why does there have to be alien.x?
Unfortunately the answer is "it depends". Some people's code maintains the position of the object at an internal x and y, using a rect to handle collisions. Other code just maintains the rect, using the rect.x and rect.y if a single position is (ever) needed.
It's up to you really, but my advice is to keep it all inside a PyGame Rect, as this has benefits of easy collision detection, should you wish to use that down the track.
class Alien:
def __init__( self, x, y, image ):
self.image = image
self.rect = image.get_rect() # copy the image dimensions
self.rect.x = x
self.rect.y = y # move to location
def draw( self, window ):
window.blit( self.image, self.rect ) # paint it
When it's time to move the Alien, you can just as easy adjust the rectangle as an x and y
class Alien:
...
def moveBy( self, by_x, by_y ):
self.rect.move_ip( by_x, by_y )
def moveTo( self, x, y ):
self.rect.x = x
self.rect.y = y
Perhaps the author thought that having a separate x and y made the code easier to understand. This is a paramount reason that effects programming style. Program code is read many times more often than it is written, so often extra variables are included to illustrate the program flow better.
For example, like checking a mouse-click-event:
for event in pygame.event.get( ):
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONUP:
handleGameClick( mouse2Board( event.pos ) ) # <-- HERE
Adding extra variables documents what's going on:
elif event.type == pygame.MOUSEBUTTONUP:
mouse_click_coord = event.pos
game_board_coord = mouse2Board( mouse_click_coord )
handleGameClick( game_board_coord )
Here it tells the reader that event.pos is a co-ordinate (so probably a pair of values), and is from the mouse. Then it reenforces that the co-ordinate is then converted into a game-board space before being passed out to handleGameClick() for processing.
The two pieces of code have exactly the same outcome (and probably execution speed), but the second is much easier to follow.
IMHO one should ideally write code such that someone unfamiliar with the language (but still a programmer) can understand it without too much trouble. This is why in my answers you wont see much "pythonic" loop-in-list creation like:
[[col +1 for col in row] for row in a] # Taken from 10 vote answer
Because unless your very familiar with python syntax, it's unreadable.

How to attribute rect to sprite

When I run this code it does not display the sprite on the screen . just a blank screen I have tried everything I can think of to get this to work .Some help would be much appreciated.
I have tried everything I can think of to get this to work. what I'm trying to do is create my sprites with a rect attribute.
import pygame
pygame.display.init()
pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
x = 300
y = 500
x1 = 100
y1 = 200
image1 = pygame.sprite.Sprite()
image1.image = pygame.image.load("picy.png").convert_alpha()
image2 = pygame.sprite.Sprite()
image2.image = pygame.image.load("picy1.png").convert_alpha()
image1_rect = image1.image.get_rect(topleft=(x,y))
image2_rect = image2.image.get_rect(topleft=(x1,y1))
screen.blit(image2_rect,(x1,y1))
screen.blit(image1_rect,(x,y))
pygame.display.update()
I expect it to put my two sprites on the screen and when they touch for them to register a hit.
From the docs:
blit()
draw one image onto another
blit(source, dest, area=None, special_flags = 0) -> Rect
Draws a source Surface onto this Surface. The draw can be positioned with the dest argument. Dest can either be pair of coordinates representing the upper left corner of the source. A Rect can also be passed as the destination and the topleft corner of the rectangle will be used as the position for the blit. The size of the destination rectangle does not effect the blit.
The blit method takes as first argument a Surface, not a Rect. The Surface is your image.
Try with:
screen.blit(image2.image, dest=image2_rect)
screen.blit(image1.image, dest=image1_rect)
By the way, you may also wish to make the rectangles attributes of the Sprite instances, instead of separate instances:
image1.rect = image1.image.get_rect(topleft=(x,y))
image2.rect = image2.image.get_rect(topleft=(x1,y1))

Moving Sprites Right and Downwards

I am working on this project to move a sprite, but I can't seem to figure out how to move a sprite to the right as well as move it downwards. Any thoughts?
Here is my program:
import pygame
import time
import sys
pygame.init()
# Set up window
screen = pygame.display.set_mode((320, 240))
# Load an image
rocket = pygame.image.load("rocket.png")
rocketrect = rocket.get_rect()
x_coord = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
screen.fill((0,0,0))
screen.blit(rocket, rocketrect)
pygame.display.flip()
x_coord += 1
rocketrect.centerx = x_coord
In your method of mving the sprite, you change the coordinatses (x) and then assign it to the centerx of the images rectangle. If you want to keep this kind of method (changing and assigning), and also move the image down, you will need to give a y value. For example
# Define y variable
y_coord = 0
# Your code
…
y_coords += 1
rocketrect.centery = y_coord
This works similarly to how you moved your x_coords, but in total, the program is a bit basic and is not how programmers (or at least me) usually code. Another person might used a tuple for the location, along with making a rocket class. There are also some other ways to move the sprite, such as the .move() or .move_ip() that I would suggest. But it's up to you.