i am making a simulator and i need every pixel of the pygame screen to be an int so i have some data like a pixel may be like 10000000 the first digit is the indicator that the pixel is loaded the second is if the pixel is explored and goes on. while creating the main city pixels i need to let the pixel know that it is occupied so i want to update the in to 11011010 while i do that it throws this error
Here the code
## Simple Python port - You need: pip install pygame. Note the code here is not efficient but it's made to be educational and easy
import numpy as np
import pygame
import random
sims = []
window_size = 600
pix = 10000000 np.zeros((window_size,window_size))
pygame.init()
window = pygame.display.set_mode((window_size, window_size))
###
green= 1
red = 2
blue= 3
yellow=4
magenta=5
cyan= 6
white=7
###
def draw(surface, x, y, color, size):
for i in range(0, size):
pygame.draw.line(surface, color, (x, y - 1), (x, y 2), abs(size))
def px(x, y, c):
return {"x": x, "y": y, "vx": 0, "vy": 0, "color": c}
def randomxy():
return round(random.random() * window_size)
def simdoms():
tx,ty = randomxy(), randomxy()
print(pix[[ty],[tx]] 1)# here it runs ok while having the same tx and ty (stands for temp x)
print(tx,ty)
pix[[ty][tx]] = 11011010
green= px(tx,ty,(0,255,0))
tx, ty = randomxy(), randomxy()
red = px(tx,ty,(255,0,0))
pix[[ty-1][tx-1]]=11012010
tx, ty = randomxy(), randomxy()
blue= px(tx,ty,(0,10,255))
pix[[ty-1][tx-1]]=11013010
tx, ty = randomxy(), randomxy()
yellow= px(tx,ty,(255, 196, 0))
pix[[ty-1][tx-1]]=11014010
tx, ty = randomxy(), randomxy()
magenta= px(tx,ty,(242, 0, 202))
pix[[ty-1][tx-1]]=11015010
tx, ty = randomxy(), randomxy()
cyan= px(tx,ty,(0, 255, 195))
pix[[ty-1][tx-1]]=11016010
tx, ty = randomxy(), randomxy()
white= px(tx,ty,(255,255,255))
pix[[ty-1][tx-1]]=11017010
sims.append(green,red,blue,yellow,magenta,cyan,white)
print (pix)
simdoms()
run = True
while run:
window.fill(0)
for i in range(len(sims)):
draw(window, sims[i]["x"], sims[i]["y"], sims[i]["color"], 3)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.flip()
pygame.quit()
exit()
CodePudding user response:
There could be some changes done to your code to shorten it using a color table (as probably intended from the begin, but not implemented). Below the entire code with the full range of changes implementing animation and a limitation of displayed frames in order to prevent the CPU from overheating in a not paused while loop. Check it out, adjust the screen size to your desktop size and enjoy the animation (adjust the density of colored points with REPS, frame rate with FPS) in FULLSCREEN mode:
import numpy as np
import pygame
import random
FPS = 15
REPS = 250
CLOCK = pygame.time.Clock()
WINDOW_WIDTH = 1920
WINDOW_HEIGHT = 1080
pix = 10000000 np.zeros((WINDOW_HEIGHT, WINDOW_WIDTH))
pygame.init()
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), pygame.FULLSCREEN)
###
dct_colors = {
'green' : ((0,255,0) , 11011010 ),
'red' : ((255,10,0) , 11012010 ),
'blue' : ((0,0,255) , 11013010 ),
'yellow' : ((255,196,0) , 11014010 ),
'magenta': ((242, 0, 202), 11015010 ),
'cyan' : ((0, 255, 195), 11016010 ),
'white' : ((255,255,255), 11017010 ),
}
### , )
def draw(surface, x, y, color, size):
for i in range(0, size):
pygame.draw.line(surface, color, (x, y), (x 3, y 3), abs(size))
def px(x, y, c):
return {"x": x, "y": y, "vx": 0, "vy": 0, "color": c}
def randomx():
return round(random.random() * (WINDOW_WIDTH-4))
def randomy():
return round(random.random() * (WINDOW_HEIGHT-4))
def simdoms():
global sims
for color, coding in dct_colors.values():
for reps in range(REPS):
tx,ty = randomx(), randomy()
pix[ty,tx] = coding
sims.append(px(tx,ty,color))
run = True
while run:
window.fill(0)
sims = []
simdoms()
for i in range(len(sims)):
draw(window, sims[i]["x"], sims[i]["y"], sims[i]["color"], 11)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.display.flip()
CLOCK.tick(FPS)
pygame.quit()
exit()