Unable to make simple 2d map in Pygames - python

I am trying to make a simple 2d game map in Pygame but have run into a problem with the tile layout. I tried to make the map with a 2d array but it is not working. They are all appearing on one line. My goal is to make a simple 2d map via an array with just the rects. Here is the code.
import pygame
pygame.init()
root = pygame.display.set_mode((500,500))
pygame.display.set_caption("Game")
surface = pygame.Surface((60, 60), pygame.SRCALPHA)
rect = pygame.draw.rect(surface, (255, 0, 0), (50, 50, 50, 50)) , (50, 50)
surface_one = pygame.Surface((60, 60), pygame.SRCALPHA)
rect_one = pygame.draw.rect(surface_one, (0, 255, 0), (50, 50, 50, 50)) , (50, 50)
def main():
tileX = 0
tileY = 0
map = [[0,0,0,0],
[1,1,1,1],
[1,0,1,0]]
for x in map:
for x in x:
if x == 0:
root.blit(surface, [tileX, tileY])
tileX = tileX+16
if x == 1:
root.blit(surface_one, [tileX, tileY])
tileX = tileX+16
tileX = 0
tileY += 30
pygame.display.update()
run = True
while run:
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
main()

You must set tileX = 0 at the begin of the outer loop and increment tileY at the end of the outer loop and:
def main():
map = [[0,0,0,0],
[1,1,1,1],
[1,0,1,0]]
tileY = 0
for row in map:
tileX = 0
for x in row:
if x == 0:
root.blit(surface, [tileX, tileY])
if x == 1:
root.blit(surface_one, [tileX, tileY])
tileX += 16
tileY += 16
pygame.display.update()
However, you can simplify the code using enumerate:
def main():
map = [[0,0,0,0],
[1,1,1,1],
[1,0,1,0]]
for y, row in enumerate(map):
tileX = 0
for x, cell in enumerate(row):
image = surface_one if cell == 1 else surface
root.blit(image, [x*16, y*16])
pygame.display.update()

Related

How to I make multiple rects in different places? [duplicate]

Right now, my game blits all the images in random positions correctly and also gets the rect of the images correctly, but I can´t figure out how to use colliderect to make sure the images don´t overlap. How could it work for my code?
Also I´m trying to make the first text fade out and I don´t know why it doesn´t work for me.
Here is the code:
class GAME1:
def __init__(self, next_scene):
self.background = pygame.Surface(size)
# Create an array of images with their rect
self.images = []
self.rects = []
self.imagenes1_array = ['autobus.png','coche.png','barco.png','autobus2.png','grua.png','bici.png']
for i in self.imagenes1_array:
# We divide in variables so we can then get the rect of the whole Img (i2)
i2 = pygame.image.load(i)
self.images.append(i2)
s = pygame.Surface(i2.get_size())
r = s.get_rect()
# Trying to use colliderect so it doesnt overlap
if pygame.Rect.colliderect(r,r) == True:
x = random.randint(300,1000)
y = random.randint(200,700)
self.rects.append(r)
def start(self, gamestate):
self.gamestate = gamestate
for rect in self.rects:
# Give random coordinates (we limit the dimensions (x,y))
x = random.randint(300,1000)
y = random.randint(200,700)
rect.x = x
rect.y = y
def draw(self,screen):
self.background = pygame.Surface(size)
font = pygame.font.SysFont("comicsansms",70)
# First half (Show image to remember)
text1 = font.render('¡A recordar!',True, PURPLE)
text1_1 = text1.copy()
# This surface is used to adjust the alpha of the txt_surf.
alpha_surf = pygame.Surface(text1_1.get_size(), pygame.SRCALPHA)
alpha = 255 # The current alpha value of the surface.
if alpha > 0:
alpha = max(alpha-4, 0)
text1_1 = text1.copy()
alpha_surf.fill((255, 255, 255, alpha))
text1_1.blit(alpha_surf, (0,0), special_flags = pygame.BLEND_RGBA_MULT)
screen.blit(text1_1, (600,50))
# Second half (Show all similar images)
text2 = font.render('¿Cuál era el dibujo?',True, PURPLE)
#screen.blit(text2, (500,50))
for i in range(len(self.images)):
#colliding = pygame.Rect.collidelistall(self.rects)
screen.blit(self.images[i], (self.rects[i].x, self.rects[i].y))
def update(self, events, dt):
for event in events:
if event.type == pygame.MOUSEBUTTONDOWN:
for rect in self.rects:
if rect.collidepoint(event.pos):
print('works!')
Use collidelist() to test test if one rectangle in a list intersects:
for i in self.imagenes1_array:
s = pygame.image.load(i)
self.images.append(s)
r = s.get_rect()
position_set = False
while not position_set:
r.x = random.randint(300,1000)
r.y = random.randint(200,700)
margin = 10
rl = [rect.inflate(margin*2, margin*2) for rect in self.rects]
if len(self.rects) == 0 or r.collidelist(rl) < 0:
self.rects.append(r)
position_set = True
See the minimal example, that uses the algorithm to generate random not overlapping rectangles:
import pygame
import random
pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()
def new_recs(rects):
rects.clear()
for _ in range(10):
r = pygame.Rect(0, 0, random.randint(30, 40), random.randint(30, 50))
position_set = False
while not position_set:
r.x = random.randint(10, 340)
r.y = random.randint(10, 340)
margin = 10
rl = [rect.inflate(margin*2, margin*2) for rect in rects]
if len(rects) == 0 or r.collidelist(rl) < 0:
rects.append(r)
position_set = True
rects = []
new_recs(rects)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
new_recs(rects)
window.fill(0)
for r in rects:
pygame.draw.rect(window, (255, 0, 0), r)
pygame.display.flip()
pygame.quit()
exit()

Making a clickable grid using Pygame

I'm making a minesweeper game using python. I have the game working in a text only form and I'm adding a GUI now.
I have three difficulty options with different dimensions. Is there any way to generate a grid without having to make it manually. This is how I generated a square matrix in text form:
for count in range(side):
count2 = 0
temp1 = []
temp2 = []
for count2 in range(side):
temp1.append(0)
temp2.append("x")
grid.append(temp1)
field.append(temp2)
count+=1
Is there any way to automatically generate a grid like this in pygame? I am also using custom sprites and every cell needs to be clickable. Will I just have to make the grids manually and use them according to the difficulty?
I came across a similar issue, here is the solution I found worked well and is fairly simple to implement.
gridstate = {}
class Button:
def __init__(self, x, y, w, h, *get_co, action=False):
self.x = x
self.y = y
self.w = w
self.h = h
self.get_co = get_co
self.colour = (255, 255, 255)
self.clicked = False
self.box = py.draw.rect(screen, self.colour, (x, y, w, h))
self.action = action
def draw(self):
pos = py.mouse.get_pos()
if self.box.collidepoint(pos):
if py.mouse.get_pressed()[0] == 1 and self.clicked == False:
self.clicked = True
if self.action == False:
self.action = True
self.colour = (255, 0, 0)
elif self.action == True:
self.action = False
self.colour = (255, 255, 255)
if py.mouse.get_pressed()[0] == 0:
self.clicked = False
self.box = py.draw.rect(screen, self.colour,
(self.x, self.y, self.w, self.h))
gridstate.update({self.get_co: self.action})
return self.action
I created the class for the button as above, this accepts the argument "get_co" to which I put the 'co ordinate' relative to the rest of the grid so I am able to identify which button was pressed. I pass in the data in like so - This creates a 10x10 grid
resx = 10
resy = 10
buttonsize = 25
grid = []
for x in range(resx):
for y in range(resy):
grid.append(Button((x*buttonsize), (y*buttonsize),
buttonsize, buttonsize, x, y))
Within the loop of game I simply run
activecells = []
for key, value in gridstate.items():
if value == True:
activecells.append(key)
To find the clicked buttons and add them to a list to reference later.
for row in grid:
row.draw()
To draw the grid to the screen.
This seemed to do the trick for me. Apologies ahead of time for any actual programmers who will probably be horrified by my variable naming!
There is no simple solution to draw a grid. You have to loop over each cell as you did in your example. You can find all functions to draw in the pygame wiki.
Example for grid creation:
size = (100, 100)
cell_width = 10
cell_height = 10
grid_surface = pygame.Surface(size) # here you draw
grid = [] # simple list to save your data
for x in range(side):
grid.append([]) # new column for the list
for y in range(side):
# save data to grid
grid[x][y] = "x"
# draw something
# e.g.: this draws a red rectangle in each cell
pygame.draw.rect(grid_surface, (200, 0, 0), (x * cell_width, y * cell_height, cell_width, cell_height)
Potential event loop:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = (event.pos[0] // cell_width, event.pos[1] // cell_height)
# do something
print(grid[x][y])
grid[x][y] = "a"
pygame.draw.rect(grid_surface, (0, 200, 0), (x * cell_width, y * cell_height, cell_width, cell_height)
I will give you a very simple start. You should create a class that represents a cell in the grid. Create a matrix of cells to represent your board:
class Cell:
def __init__(self):
self.clicked = False
grid_size = 10
board = [[Cell() for _ in range(grid_size)] for _ in range(grid_size)]
Calculate the index of a cell when the mouse button is clicked and change the attributes in the cell:
for event in pygame.event.get():
# [...]
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
row = event.pos[1] // 20
col = event.pos[0] // 20
board[row][col].clicked = True
Draw the board in 2 nested loops:
for iy, rowOfCells in enumerate(board):
for ix, cell in enumerate(rowOfCells):
color = (64, 64, 64) if cell.clicked else (164, 164, 164)
pygame.draw.rect(window, color, (ix*20, iy*20, 20, 20))
Minimal example:
import pygame
class Cell:
def __init__(self):
self.clicked = False
grid_size = 10
board = [[Cell() for _ in range(grid_size)] for _ in range(grid_size)]
pygame.init()
window = pygame.display.set_mode((200, 200))
clock = pygame.time.Clock()
run = True
while run:
clock.tick(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
row = event.pos[1] // 20
col = event.pos[0] // 20
board[row][col].clicked = True
window.fill(0)
for iy, rowOfCells in enumerate(board):
for ix, cell in enumerate(rowOfCells):
color = (64, 64, 64) if cell.clicked else (164, 164, 164)
pygame.draw.rect(window, color, (ix*20+1, iy*20+1, 18, 18))
pygame.display.flip()
pygame.quit()
exit()

How to generate perlin noise in pygame?

I am trying to make a survival game and I have a problem with perlin noise. My program gives me this:
But I want something like islands or rivers.
Here's my code:
#SetUp#
import pygame, sys, random
pygame.init()
win = pygame.display.set_mode((800, 600))
pygame.display.set_caption('Isom')
x = 0
y = 0
s = 0
tilel = list()
random.seed(5843)
MAP = [random.randint(0, 1) for _ in range(192)]
#Tiles#
class tile():
grass = pygame.image.load('Sprites/Images/Grass.png')
water = pygame.image.load('Sprites/Images/Water.png')
#Loop#
while True:
for key in pygame.event.get():
if key.type == pygame.QUIT:
pygame.quit()
sys.exit()
#World#
for a in range(12):
for b in range(16):
if MAP[s] == 0:
win.blit((tile.grass), (x, y))
elif MAP[s] == 1:
win.blit((tile.water), (x, y))
x += 50
s += 1
x = 0
y += 50
x = 0
y = 0
s = 0
#Update#
pygame.display.update()
image of randomly genrated terrain
code for perlin noise in pygame:
from PIL import Image
import numpy as np
from perlin_noise import PerlinNoise
import random
import pygame
pygame.init()
noise = PerlinNoise(octaves=6, seed=random.randint(0, 100000))
xpix, ypix = 500, 500
pic = [[noise([i/xpix, j/ypix]) for j in range(xpix)] for i in range(ypix)]
screen = pygame.display.set_mode ((500, 500), pygame.RESIZABLE)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
run = False
for i, row in enumerate(pic):
for j, column in enumerate(row):
if column>=0.6:
pygame.draw.rect(screen, (250, 250, 250), pygame.Rect(j, i, 1, 1))
elif column>=0.2:
pygame.draw.rect(screen, (80, 80, 80), pygame.Rect(j, i, 1, 1))
elif column>=0.09:
pygame.draw.rect(screen, (30, 90, 30), pygame.Rect(j, i, 1, 1))
elif column >=0.009:
pygame.draw.rect(screen, (10, 100, 10), pygame.Rect(j, i, 1, 1))
elif column >=0.002:
pygame.draw.rect(screen, (100, 150, 0), pygame.Rect(j, i, 1, 1))
elif column >=-0.06:
pygame.draw.rect(screen, (30, 190, 0), pygame.Rect(j, i, 1, 1))
elif column >=-0.02:
pygame.draw.rect(screen, (40, 200, 0), pygame.Rect(j, i, 1, 1))
elif column >=-0.1:
pygame.draw.rect(screen, (10, 210, 0), pygame.Rect(j, i, 1, 1))
elif column >=-0.8:
pygame.draw.rect(screen, (0, 0, 200), pygame.Rect(j, i, 1, 1))
#------------
#run the game class
pygame.display.update()
pygame.quit()
quit()
I recommend installing the noise package. Then use noise.pnoise1(x) for 1 dimensional Perlin noise, noise.pnoise2(x, y) for 2 dimensional Perlin noise, and noise.pnoise3(x, y, z) for 3 dimensional Perlin noise.
First, the critical thinking: Perlin is a popular term but the actual "Perlin" noise algorithm is old and visibly square-aligned. Better, as a general rule, to use a Simplex-type noise.
I suggest PyFastNoiseLite: https://github.com/tizilogic/PyFastNoiseLite Follow the install instructions, then mirror the C++ example in the FastNoiseLite documentation here: https://github.com/Auburn/FastNoiseLite/tree/master/Cpp Be sure to note its internal frequency multiplication, which you can change with SetFrequency(f)
You can also use the Python noise library for Simplex-type noise, with noise snoise2(x, y) though if you wish to use snoise3(x, y, z) I would first consider the info here: https://www.reddit.com/r/proceduralgeneration/comments/qr6snt/countdown_timer_simplex_patent_expiration/

What is the quickest way to draw multiple circles in pygame?

I am making an agario clone for a project and I was wondering what the quickest way to draw many dots in pygame.
from pygame import *
import random as rd
x = rd.randint(100, 700)
y = rd.randint(100, 500)
# I would like to draw about 50 dots of this type.
dot = draw.circle(screen, (0, 0, 0), (x, y), 5)
Here's some basic code to draw circles using pygame:
import pygame as pg
import random as rd
pg.init() # initialize pygame
screen = pg.display.set_mode((500,500)) # create main screen
for ctr in range(25): # 25 circles
x = rd.randint(50, 450)
y = rd.randint(50, 450)
# I would like to draw about 50 dots of this type.
dot = pg.draw.circle(screen, (100, 200, 100), (x, y), 15)
pg.display.update() # update screen
while True: # main pygame loop, always include this
for event in pg.event.get(): # required for OS events
if event.type == pg.QUIT: # user closed window
pg.quit()
Output
Any easy way to do this to use a simple python "for" loop with a "range":
As the loop iterates, it executes the content of the loop body, with the variable i incrementing from 0 to N-1 (i.e. 49). The variable i could be any valid variable name, but for simple numbered loops, i, j & k are commonly used.
from pygame import *
import random as rd
# Draw 50 dots
for i in range( 0, 50 ):
x = rd.randint(100, 700)
y = rd.randint(100, 500)
dot = draw.circle(screen, (0, 0, 0), (x, y), 5)
I made a class for dots:
class Dot():
SIZE = 5
def __init__(self, x, y):
self.x = x
self.y = y
def draw(self):
draw.circle(screen, self.color, (self.x, self.y), Dot.SIZE)
Then I made an array and generate NUMBER_OF_DOTS like this:
dots = []
for i in range(NUMBER_OF_DOTS):
x = rd.randint(100, 700)
y = rd.randint(100, 500)
dots.append(Dot(x,y))
and in the while loop and redraw after filling the whole scene by white color:
while True:
screen.fill((255, 255, 255))
...
for dot in dots:
dot.draw()
The whole source:
from pygame import *
import random as rd
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
NUMBER_OF_DOTS = 300
class Dot():
SIZE = 5
def __init__(self, x, y):
self.x = x
self.y = y
self.color = random_color()
def draw(self):
draw.circle(screen, self.color, (self.x, self.y), Dot.SIZE)
def random_color():
r = rd.randint(0, 255)
g = rd.randint(0, 255)
b = rd.randint(0, 255)
return (r, g, b)
init()
screen = display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
dots = []
# generate random dots all over the screen
for i in range(NUMBER_OF_DOTS):
x = rd.randint(100, 700)
y = rd.randint(100, 500)
dots.append(Dot(x,y))
# main while loop
while True:
screen.fill((255, 255, 255))
for dot in dots:
dot.draw()
display.update()
time.delay(1) # Speed down
If you want to draw the same circles continuously in the main application loop, then you've to generate a list of random positions:
import pygame as pg
import random as rd
pg.init()
screen = pg.display.set_mode((800,600))
cpts = []
for i in range(25):
x = rd.randint(100, 700)
y = rd.randint(100, 500)
cpts.append((x, y))
run = True
while run:
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
for cpt in cpts:
pg.draw.circle(screen, (255, 255, 255), cpt, 15)
pg.display.update()
See the answer to Pygame unresponsive display for a minimum pygame application.

Tetris pygame falling issues

I am a beginner programmer trying to build tetris in Python using Pygame. The problem is that I am unable to make my test shape fall. Only go up. When I attempt to make the shape fall, the program gives me an
IndexError: list assignment index out of range
I was wondering if I could please get help? The code is listed as follows.
import pygame, sys
from pygame.locals import *
import base64
from TetrisFont import Tetrisfont
from RGBTitle import RGBTitle
from TetrisLogo import logo
from BSOD import Cancer
from BSODFont import Death
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
DOWN = pygame.USEREVENT
class Game:
def __init__(self):
self.running = True
def run(self):
# The main gameplay loop
self.load_assets()
self.initialize_game()
clock = pygame.time.Clock()
while self.running:
self.handle_events()
self.update(clock.tick())
self.draw()
def handle_events(self):
for event in pygame.event.get():
if event.type == DOWN:
for ix, x in enumerate(self.grid):
for iy, y in enumerate(self.grid[0]):
print (y, end='')
print('') # create a new line
for x in range(len(self.grid)):
for y in range(len(self.grid[0])):
if self.grid[x][y] == True:
self.grid[x][y]= False
self.grid[x][y+1] = True
# print(x, y+1)
if event.type == QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
def draw(self):
# Handle game rendering
self.windowSurface.fill(BLACK)
pygame.draw.rect(self.windowSurface,WHITE,(550,0, 250, 600), 2)
for x in range(0,600,50):
pygame.draw.line(self.windowSurface,WHITE,(1,x),(550,x), 2)
pygame.draw.line(self.windowSurface,WHITE,(x,1),(x,550), 2)
self.title.render(self.windowSurface)
for x in range(len(self.grid)):
for y in range(len(self.grid[0])):
if self.grid[x][y] == True:
# draw a block in the right place
# render_rect(X * 50, Y * 50, )
pygame.draw.rect(self.windowSurface, RED, (x*50+2, y*50+2, 48, 48))
pygame.display.update()
def update(self, deltaTime):
pass
def load_assets(self):
# Load assets needed for the game. This should only be called once.
pygame.init()
pygame.time.set_timer(DOWN, 500)
with open('Tetrisfont.ttf', 'wb') as imagef:
imagef.write(base64.b64decode(Tetrisfont))
self.Font = pygame.font.Font('Tetrisfont.ttf', 48)
with open('Logo.png', 'wb') as imagef:
imagef.write(base64.b64decode(logo))
with open('BSODFont.ttf', 'wb') as imagef:
imagef.write(base64.b64decode(Death))
self.BSODFont = pygame.font.Font('BSODFont.ttf', 48)
with open('BSOD.png', 'wb') as imagef:
imagef.write(base64.b64decode(Cancer))
self.windowSurface = pygame.display.set_mode((800, 600), 0, 32)
pygame.display.set_caption('Tetris')
self.a = pygame.image.load('Logo.png')
pygame.display.set_icon(self.a)
def initialize_game(self):
# Set up the board for play. This can happen multiple times to restart the game.
self.score = 0
self.title = RGBTitle(self.Font, "Tetris", (200, 550), [WHITE, RED, GREEN, BLUE, GREEN, RED])
grid_width = 11
grid_height = 11
self.grid = [[0 for x in range(grid_width)] for y in range(grid_height)]
# self.grid = []
# for x in range(11):
# self.grid.append([False] * 11)
self.grid[5][5] = True
# print(self.grid)
# print(len(self.grid))
# print(len(self.grid[0]))
if __name__ == '__main__':
g = Game()
g.run()
Thank you.
The error is caused, by self.grid[x][y+1] in
for x in range(len(self.grid)):
for y in range(len(self.grid[0])):
if self.grid[x][y] == True:
self.grid[x][y]= False
self.grid[x][y+1] = True
If y is equal self.grid[0]-1, then y+1 is out of range.
Add an additional condition if y+1 < len(self.grid[0]). Furthermore, I recommend to iterate through the grid in reversed() order from the bottom to the top:
for x in range(len(self.grid)):
for y in reversed(range(len(self.grid[0]))):
if self.grid[x][y] == True:
self.grid[x][y] = False
if y+1 < len(self.grid[0]):
self.grid[x][y+1] = True

Categories

Resources