python - Surfaces must not be locked during blit -
i trying draw text screen in space above bunch of rects in 2d array square. when try , draw text screen says "surfaces must not locked during blit."
i have tried unlocking, makes no difference.
import sys, time, random, pygame pygame import * time import time,sleep pygame.init() global isdragging, indrag, rightdrag, lastdrag, indragright, mousex,mousey, borderheight isdragging = false isauto = false indrag= false indragright = false isfilling = false done = false lastdrag = none rightdrag = none ticktime = 300 font = pygame.font.sysfont(none, 12,false,false) dragdelay = 2 black = ( 0, 0, 0) white = ( 255, 255, 255) green = ( 0, 255, 0) red = ( 255, 0, 0) pink = ( 255, 150, 200) blue = ( 0, 0, 255) red = green width, height, margin = 100, 100, 1 nowidth, noheight = 6,6 speed = [2, 2] borderheight = 50 size = (width*nowidth,height*noheight + borderheight) screen = pygame.display.set_mode(size) pygame.display.set_caption("piestone") clock = pygame.time.clock() pixarray = pygame.pixelarray(screen) font = pygame.font.sysfont("monospace", 15) word = font.render("test", 1, (0, 0, 0)) class null: pass square = [[null() in range(noheight)]for in range(nowidth)] d1 in range(nowidth): d2 in range(noheight): square[d1][d2].man = false square[d1][d2].visits = 0 square[d1][d2].colour = black square[d1][d2].margin = 1 = 1 = 1 def text(word,size,pos1,pos2,pos3): word = font.render(str(word),size,(pos1,pos2,pos3)) return word def colide(obj,mousex,mousey): if obj.collidepoint(mousex,mousey) == 1: return true else: return false def click(mousex,mousey): #rint("left") global indrag,lastdrag d1 in range(nowidth): d2 in range(noheight): if colide(square[d1][d2].rect,mousex,mousey): square[d1][d2].man = true #print("square",d1,d2,"has been clicked") try: if square[d1][d2] != lastdrag: indrag = true lastdrag = square[d1][d2] else: indrag = false except unboundlocalerror: print("error") lastdrag = square[d1][d2] #print(indrag) if square[d1][d2].margin == 0 , indrag: square[d1][d2].margin = 1 square[d1][d2].visits = square[d1][d2].visits + 1 elif square[d1][d2].margin == 1 , indrag: square[d1][d2].margin = 0 square[d1][d2].visits = square[d1][d2].visits + 1 break '''if square[d1][d2].visits >= 5 , square[d1][d2].colour == black: square[d1][d2].visits = 0 square[d1][d2].colour = red elif square[d1][d2].visits >= 5 , square[d1][d2].colour == red: square[d1][d2].visits = 0 square[d1][d2].colour = black''' def rightclick(mousex,mousey): #print("right") global indragright, lastright d1 in range(nowidth): d2 in range(noheight): if colide(square[d1][d2].rect,mousex,mousey): square[d1][d2].man = true #print("colide") try: if square[d1][d2] != lastright: indragright = true lastright = square[d1][d2] else: indragright = false except: print("error") lastright = square[d1][d2] #print(indragright) if square[d1][d2].colour == red , indragright: square[d1][d2].colour = black #print("black") elif square[d1][d2].colour == black , indragright: square[d1][d2].colour = red #print("red") break while not done: screen.blit(word, (0, 0)) (mousex,mousey) = pygame.mouse.get_pos() #print(str(mousex)+",",mousey) event in pygame.event.get(): # user did if event.type == pygame.quit: # if user clicked close done = true # flag done exit loop elif event.type == pygame.mousebuttondown: #print(mousex,mousey) if pygame.mouse.get_pressed()[0] == 1: isdragging = true if pygame.mouse.get_pressed()[2] == 1: isfilling = true elif pygame.mouse.get_pressed()[1] == 1: isauto = true #print(pygame.mouse.get_pressed()) elif event.type == pygame.mousebuttonup: #print("up") isdragging = false isfilling = false lastdrag = none lastright = false if pygame.mouse.get_pressed()[0] == 1: isdragging = true if pygame.mouse.get_pressed()[2] == 1: isfilling = true if isauto: rnd1 = random.randint(0,1) if rnd1 == 1: isdragging = true isfilling = false else: isdragging = false isfilling = true (mousex,mousey) = (random.randint(0,width*nowidth),random.randint(0,height*noheight)+borderheight) #print(mousex,mousey) if isdragging: click(mousex,mousey) if isfilling: rightclick(mousex,mousey) screen.fill(white) # --- game logic should go here = 0 d1 in range(nowidth): if noheight % 2 == 0: '''if == 0: = 1 else: = 0''' d2 in range(noheight): #if % == 0: try: if == 0: pass elif == 1: pass except attributeerror: print("attributeerror") square[d1][d2].margin = random.randint(0,1) #print(square[d1][d2].visits) square[d1][d2].rect = pygame.draw.rect(screen, square[d1][d2].colour, [(d1*width), ((d2*height)+borderheight),width,(height)],square[d1][d2].margin) #print("d1*width:",d1*width,"d2*height:",d2*height,"d1*width+width:",d1*width+width,"d2*height+height:",d2*height+height) pygame.display.flip() clock.tick(ticktime) # close window , quit. # if forget line, program 'hang' # on exit if running idle. pygame.quit()
you're using pixelarray:
pixarray = pygame.pixelarray(screen) but using pixelarray locks surface, stated in documentation:
during lifetime, pixelarray locks surface, explicitly have delete once not used anymore , surface should perform operations in same scope.
just remove line, since you're not using anyway.
Comments
Post a Comment