我正在制作一個模擬器,我需要 pygame 螢屏的每個像素都是一個 int,所以我有一些資料,比如像素可能像 10000000 第一個數字是像素已加載的指示第二個是像素是否被探索和繼續。在創建主要城市像素時,我需要讓像素知道它已被占用,所以我想將 in 更新為 11011010,而我這樣做會引發此錯誤
這里的代碼
## 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()
uj5u.com熱心網友回復:
這是一個
可能會對您的代碼進行一些更改以使用顏色表縮短它(可能從一開始就打算這樣做,但未實作)。下面是整個代碼,其中包含實作影片的全部更改以及顯示幀的限制,以防止 CPU 在未暫停的 while 回圈中過熱。檢查一下,將螢屏尺寸調整為您的桌面尺寸,然后在全屏模式下欣賞影片(用 REPS 調整色點的密度,用 FPS 調整幀速率):
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507392.html
下一篇:洗掉行并找到特定列的平均值