pyglet window.flip() not updating - python

I'm drawing a circle and some text with pyglet. I'd like it to update after a keypres, but instead on calling window.flip() the second time, the window is just empty. Any suggestions are appreciated.
from math import *
from pyglet.gl import *
window = pyglet.window.Window()
def make_circle(x_pos, y_pos, radius, numPoints):
verts = []
glMatrixMode(GL_PROJECTION)
glOrtho(0, 640, 0, 480, -1, 1)
glMatrixMode(GL_MODELVIEW)
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
glColor3f(1, 1, 0)
for i in range(numPoints):
angle = radians(float(i) / numPoints * 360.0)
x = radius * cos(angle) + x_pos
y = radius * sin(angle) + y_pos
verts += [x, y]
circle = pyglet.graphics.vertex_list(numPoints, ('v2f', verts))
circle.draw(GL_LINE_LOOP)
def text(x, y, text):
label = pyglet.text.Label(text, font_size=36,
x=x, y=y, anchor_x='left', anchor_y='bottom',
color=(255, 123, 255, 255))
label.draw()
make_circle(5, 5, 100, 10)
text(10, 10, 'Test1')
text(30, 30, 'Text2')
window.flip()
input()
make_circle(5, 5, 22, 10)
text(23, 10, 'This text does not appear')
text(30, 23, 'Neither does this one')
window.flip()
input()

If you don't use the default event loop, then you've to do the steps of the event loop your self. It is not sufficient to call window.flip().
Befor the drawing you've to call pyglet.clock.tick() to signify that one frame has passed and window.dispatch_events() to poll the operating system event queue for new events and call attached event handlers.
Note, Since the events are dispatched manually, it is not necessary to set the projection matrix.
See also Dispatching events manually
e.g.:
from math import *
from pyglet.gl import *
def make_circle(x_pos, y_pos, radius, numPoints):
verts = []
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
glColor3f(1, 1, 0)
for i in range(numPoints):
angle = radians(float(i) / numPoints * 360.0)
x = radius * cos(angle) + x_pos
y = radius * sin(angle) + y_pos
verts += [x, y]
circle = pyglet.graphics.vertex_list(numPoints, ('v2f', verts))
circle.draw(GL_LINE_LOOP)
def text(x, y, text):
label = pyglet.text.Label(text, font_size=36,
x=x, y=y, anchor_x='left', anchor_y='bottom',
color=(255, 123, 255, 255))
label.draw()
window = pyglet.window.Window()
# make OpenGL context current
window.switch_to()
# signify that one frame has passed
pyglet.clock.tick()
# poll the operating system event queue
window.dispatch_events()
make_circle(5, 5, 100, 10)
text(10, 10, 'Test1')
text(30, 30, 'Text2')
# swap buffers
window.flip()
input()
# signify that one frame has passed
pyglet.clock.tick()
# poll the operating system event queue
window.dispatch_events()
make_circle(5, 5, 22, 10)
text(23, 10, 'This text does not appear')
text(30, 23, 'Neither does this one')
# swap buffers
window.flip()
input()

Related

No shape display pyopengl

So i'm trying to make a function for displaying a circle, here's the function :
def drawCircle(x,y,size):
glColor3f(1,1,1)
glBegin(GL_TRIANGLE_STRIP)
glVertex2f(0,0)
glVertex2f(100,0)
glVertex2f(100,100)
glVertex2f(0,100)
But when i try it, nothing display. If you have the solution, please explain me in detail, I'm not a profesional in opengl or in pyopengl.
Here's the code :
import glfw
from OpenGL.GL import *
from OpenGL.GLUT import *
import numpy as np
import pyrr
from random import *
from math import *
buffer = bytearray(800 * 600 * 3)
display = (800,600)
if not glfw.init():
raise Exception("glfw can not be initialized!")
def CreateWindow(title="Baguette game",width=800,height=600):
display = (width,height)
window = glfw.create_window(width,height,title, None, None)
glfw.set_window_pos(window,400,200)
glfw.make_context_current(window)
return window
def DrawTriangle(pointA=[-0.5, -0.5, 0.0],pointB=[0.5, -0.5,0.0],
pointC=[-0.5, 0, 0.0],color=[1.0,1.0,1.0]):
vertices = [pointA[0], pointA[1], pointA[2],
pointB[0], pointB[1], pointB[2],
pointC[0], pointC[1], pointC[2]]
colors = [color[0], color[1], color[2],
color[0], color[1], color[2],
color[0], color[1], color[2] ]
v = np.array(vertices,dtype=np.float32)
c = np.array(colors, dtype=np.float32)
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT,0,v)
glEnableClientState(GL_COLOR_ARRAY)
glColorPointer(3, GL_FLOAT,0,c)
glDrawArrays(GL_TRIANGLES,0,3)
def DrawRectangle(size=[0.5,0.5],position=[0,0],color=[1.0,1.0,1.0]):
DrawTriangle([position[0]-size[0]/2,position[1]+size[1]/2,0],
[position[0]+size[0]/2,position[1]+size[1]/2,0],
[position[0]+size[0]/2,position[1]-size[1]/2,0],
color)
DrawTriangle([position[0]-size[0]/2,position[1]-size[1]/2,0],
[position[0]+size[0]/2,position[1]-size[1]/2,0],
[position[0]-size[0]/2,position[1]+size[1]/2,0],
color)
def Pixel(x,y):
buffer_data = [randint(0,255), 0, 0] * (x * y)
buffer = (GLubyte * (x * y * 3))(*buffer_data)
glDrawPixels(x, y, GL_RGB, GL_UNSIGNED_BYTE, buffer)
def drawCircle(x,y,size):
glColor3f(1,1,1)
glBegin(GL_TRIANGLE_STRIP)
glVertex2f(0,0)
glVertex2f(100,0)
glVertex2f(100,100)
glVertex2f(0,100)
if __name__=="__main__":
window = CreateWindow()
initialPosition = (0,0,0)
z=1
while not glfw.window_should_close(window):
glfw.poll_events()
#glClear(GL_COLOR_BUFFER_BIT)
DrawRectangle([1,1],[-0.5,0],[1,0,0])
DrawRectangle([1,1],[0.5,0],[0,1,0])
glfw.swap_buffers(window)
glfw.terminate()
You don't use the x, y and size arguments at all. A glBegin/glEnd sequence must be delimited with glEnd. e.g.:
def drawCircle(cx, cy, radius):
glColor3f(1,1,1)
glBegin(GL_TRIANGLE_FAN)
glVertex2f(cx, cy)
for a in range(361):
x = cx + radius * cos(radians(a))
y = cy + radius * sin(radians(a))
glVertex2f(x, y)
glEnd()
For example draw a circle in the center of the viewport:
drawCircle(0, 0, 0.5)
I recommend reading a good OpenGL tutorial. e.g.: LearnOpenGL

pyglet when using draw() instead of eventloop

I'm trying to draw a circle with pyglet. But it is not visible when I use the draw() function instad of the app.run() loop. Any suggestions what I can do? thanks
from math import *
from pyglet.gl import *
window = pyglet.window.Window()
def makeCircle(x_pos, y_pos, radius, numPoints):
verts = []
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
glColor3f(1,1,0)
for i in range(numPoints):
angle = radians(float(i)/numPoints * 360.0)
x = radius *cos(angle) + x_pos
y = radius *sin(angle) + y_pos
verts += [x,y]
circle = pyglet.graphics.vertex_list(numPoints, ('v2f', verts))
circle.draw(GL_LINE_LOOP)
input()
makeCircle(5,5, 100, 10)
You've to call window.flip() to update the window.
Since you don't have set a projection matrix, the geometry has to be draw in normalized device coordinates, which are in range [-1, 1] for all 3 components (x, y, z). Note, pyglet set a projection matrix by default when the application is started by pyglet.app.run().
Call window.flip() and change the geometry:
from math import *
from pyglet.gl import *
window = pyglet.window.Window()
def makeCircle(x_pos, y_pos, radius, numPoints):
verts = []
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
glColor3f(1,1,0)
for i in range(numPoints):
angle = radians(float(i)/numPoints * 360.0)
x = radius *cos(angle) + x_pos
y = radius *sin(angle) + y_pos
verts += [x,y]
circle = pyglet.graphics.vertex_list(numPoints, ('v2f', verts))
circle.draw(GL_LINE_LOOP)
window.flip() # <--------
input()
makeCircle(0, 0, 0.5, 10) # <--------
Alternatively you can set an orthographic projection on your own, by glOrtho. e.g.:
from math import *
from pyglet.gl import *
window = pyglet.window.Window()
def makeCircle(x_pos, y_pos, radius, numPoints):
verts = []
glMatrixMode(GL_PROJECTION)
glOrtho(0, 640, 0, 480, -1, 1)
glMatrixMode(GL_MODELVIEW)
glClear(pyglet.gl.GL_COLOR_BUFFER_BIT)
glColor3f(1,1,0)
for i in range(numPoints):
angle = radians(float(i)/numPoints * 360.0)
x = radius *cos(angle) + x_pos
y = radius *sin(angle) + y_pos
verts += [x,y]
circle = pyglet.graphics.vertex_list(numPoints, ('v2f', verts))
circle.draw(GL_LINE_LOOP)
text = 'This is a test but it is not visible'
label = pyglet.text.Label(text, font_size=36,
x=10, y=10, anchor_x='left', anchor_y='bottom',
color=(255, 123, 255, 255))
label.draw()
window.flip()
input()
makeCircle(5,5, 100, 10)

how to redraw a line on a line which is already drawn in tkinter canvas smoothly?

I have some lines in tinter canvas, and also have their code. I want to make them red but not at a same time I want to draw another line(red line) go on them but it should take different time. for example fo one specific line it should take 3 seconds that line get red for another one it should take 7 seconds to make that red. it is like drawing another red line on the previous one.
def activator(self, hexagon, duration_time):
if not hexagon.is_end:
self.canvas.itemconfigure(hexagon.drawn, fill="tomato")
self.canvas.itemconfigure(hexagon.hex_aspects.outputs.drawn, fill="tomato")
for example I want my hexagon which created by createpolygon method of tinter get red but not immediately. It should do regarding to duration_time which is the a second variable. I mean it should be done within duration_time second(let say 3 second). is there any way for doing this? I have lots of object in my canvas which should get red during an specific time. line, circle, polygon..
A line on a tk.canvas is defined by a start and an end point; in order to access points on the line, we need to first create an affine line by first generating many points at an interval on the line, then join them with line segments.
This affine line is created upon clicking on an item on the canvas, but is hidden at first, and progressively revealed over a short time interval.
Once the redraw is completed, the affine line is hidden again, and the item being redrawn set to its new color.
This "simple" redraw requires quite a bit of machinery to implement. You can try it by clicking on a line to redraw it, and see the animation of the redraw.
Code:
import random
import tkinter as tk
WIDTH, HEIGHT = 500, 500
class AffinePoint:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return AffinePoint(self.x + other.x, self.y + other.y)
def __sub__(self, other):
return AffinePoint(self.x - other.x, self.y - other.y)
def __mul__(self, scalar):
return AffinePoint(self.x * scalar, self.y * scalar)
def __iter__(self):
yield self.x
yield self.y
def draw(self, canvas):
offset = AffinePoint(2, 2)
return canvas.create_oval(*(self + offset), *self - offset, fill='', outline='black')
def create_affine_points(canvas, num_points):
"""sanity check"""
for _ in range(num_points):
AffinePoint(random.randrange(0, WIDTH), random.randrange(0, HEIGHT)).draw(canvas)
class AffineLineSegment:
def __init__(self, start, end, num_t=100):
self.start = AffinePoint(*start)
self.end = AffinePoint(*end)
self.num_t = num_t
self.points = []
self._make_points()
self.segments = []
def _make_points(self):
for _t in range(self.num_t):
t = _t / self.num_t
self.points.append(self.start + (self.end - self.start) * t)
def __iter__(self):
for point in self.points:
yield point
def draw(self, canvas):
for p0, p1 in zip(self.points[:-1], self.points[1:]):
self.segments.append(canvas.create_line(*p0, *p1, width=5, state='hidden', fill='red'))
def hide(self, canvas):
for seg in self.segments:
canvas.itemconfigure(seg, state='hidden')
def create_affine_line(canvas, num_lines):
"""sanity check"""
for _ in range(num_lines):
start = random.randrange(0, WIDTH), random.randrange(0, HEIGHT)
end = random.randrange(0, WIDTH), random.randrange(0, HEIGHT)
AffineLineSegment(start, end).draw(canvas)
def select_and_redraw(event):
item = canvas.find_closest(event.x, event.y)[0]
x0, y0, x1, y1 = canvas.coords(item)
canvas.itemconfigure(item, fill='grey25')
canvas.itemconfigure(item, width=1)
a = AffineLineSegment((x0, y0), (x1, y1))
a.draw(canvas)
gen = (segment for segment in a.segments)
redraw(gen, a, item)
def redraw(gen, a, item):
try:
segment = next(gen)
canvas.itemconfigure(segment, state='normal')
root.after(10, redraw, gen, a, item)
except StopIteration:
a.hide(canvas)
canvas.itemconfigure(item, state='normal')
canvas.itemconfigure(item, fill='red')
canvas.itemconfigure(item, width=3)
finally:
root.after_cancel(redraw)
root = tk.Tk()
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="cyan")
canvas.pack()
canvas.bind('<ButtonPress-1>', select_and_redraw)
# sanity checks
# create_affine_points(canvas, 500)
# create_affine_line(canvas, 100)
for _ in range(10):
start = random.randrange(0, WIDTH), random.randrange(0, HEIGHT)
end = random.randrange(0, WIDTH), random.randrange(0, HEIGHT)
canvas.create_line(*start, * end, activefill='blue', fill='black', width=3)
root.mainloop()
Screen capture showing a line in the process of being redrawn
Try something like this
from tkinter import *
import numpy as np
root = Tk()
def lighter(color, percent):
color = np.array(color)
white = np.array([255, 255, 255])
vector = white-color
return tuple(color + vector * percent)
def Fade(line, start_rgb, percentage, times, delay):
'''assumes color is rgb between (0, 0, 0) and (255, 255, 255) adn percentage a value between 0.0 and 1.0'''
new_color = lighter(start_rgb, percentage)
red, blue, green = new_color
red = int(red)
blue = int(blue)
green = int(green)
new_hex = '#%02x%02x%02x' % (red, blue, green)
canvas.itemconfigure(line, fill=new_hex)
if times > 0:
root.after(delay, lambda: Fade(line, new_color, percentage, times - 1, delay))
canvas = Canvas(root, bg="black")
canvas.pack()
line = canvas.create_line(0, 0, 100, 100, width=10)
Fade(line, (0, 0, 50), 0.01, 1000, 10)
root.mainloop()

How to draw outline on the font(Pygame)

How can I draw an outline on the font?
I want to use black font, but the background has to be blackish so it's hard to see the font.
I assume myfont.render doesn't support drawing outline on the font.
Is there other way?
Pygame doesn't support this out of the box, but one way to do it is render the text in the outline color and blit it to the result surface shifted multiple times, then render the text in the desired color on top of it.
pgzero uses this technique; a trimmed down version of its code is shown below:
import pygame
_circle_cache = {}
def _circlepoints(r):
r = int(round(r))
if r in _circle_cache:
return _circle_cache[r]
x, y, e = r, 0, 1 - r
_circle_cache[r] = points = []
while x >= y:
points.append((x, y))
y += 1
if e < 0:
e += 2 * y - 1
else:
x -= 1
e += 2 * (y - x) - 1
points += [(y, x) for x, y in points if x > y]
points += [(-x, y) for x, y in points if x]
points += [(x, -y) for x, y in points if y]
points.sort()
return points
def render(text, font, gfcolor=pygame.Color('dodgerblue'), ocolor=(255, 255, 255), opx=2):
textsurface = font.render(text, True, gfcolor).convert_alpha()
w = textsurface.get_width() + 2 * opx
h = font.get_height()
osurf = pygame.Surface((w, h + 2 * opx)).convert_alpha()
osurf.fill((0, 0, 0, 0))
surf = osurf.copy()
osurf.blit(font.render(text, True, ocolor).convert_alpha(), (0, 0))
for dx, dy in _circlepoints(opx):
surf.blit(osurf, (dx + opx, dy + opx))
surf.blit(textsurface, (opx, opx))
return surf
def main():
pygame.init()
font = pygame.font.SysFont(None, 64)
screen = pygame.display.set_mode((350, 100))
clock = pygame.time.Clock()
while True:
events = pygame.event.get()
for e in events:
if e.type == pygame.QUIT:
return
screen.fill((30, 30, 30))
screen.blit(render('Hello World', font), (20, 20))
pygame.display.update()
clock.tick(60)
if __name__ == '__main__':
main()
If you are a beginer and are not looking for very complex code, and you are only using a simple font such as arial, helvetica, calibri, etc...
You can just blit the text in the outline colour 4 times in the 'corners', then blit the actual text over it:
white = pygame.Color(255,255,255)
black = pygame.Color(0,0,0)
def draw_text(x, y, string, col, size, window):
font = pygame.font.SysFont("Impact", size )
text = font.render(string, True, col)
textbox = text.get_rect()
textbox.center = (x, y)
window.blit(text, textbox)
x = 300
y = 300
# TEXT OUTLINE
# top left
draw_text(x + 2, y-2 , "Hello", black, 40, win)
# top right
draw_text(x +2, y-2 , "Hello", black, 40, win)
# btm left
draw_text(x -2, y +2 , "Hello", black, 40, win)
# btm right
draw_text(x-2, y +2 , "Hello", black, 40, win)
# TEXT FILL
draw_text(x, y, "Hello", white, 40, win)
You can use masks to add an outline to your text. Here is an example:
import pygame
def add_outline_to_image(image: pygame.Surface, thickness: int, color: tuple, color_key: tuple = (255, 0, 255)) -> pygame.Surface:
mask = pygame.mask.from_surface(image)
mask_surf = mask.to_surface(setcolor=color)
mask_surf.set_colorkey((0, 0, 0))
new_img = pygame.Surface((image.get_width() + 2, image.get_height() + 2))
new_img.fill(color_key)
new_img.set_colorkey(color_key)
for i in -thickness, thickness:
new_img.blit(mask_surf, (i + thickness, thickness))
new_img.blit(mask_surf, (thickness, i + thickness))
new_img.blit(image, (thickness, thickness))
return new_img
And here is how I use this function to create a white text with a 2px black outline:
text_surf = myfont.render("test", False, (255, 255, 255)).convert()
text_with_ouline = add_outline_to_image(text_surf, 2, (0, 0, 0))

pygame class structure

My goal is to make a module that will make a grid on a pygame canvas and allow you to highlight boxes by their x and y coords.
Here is a simple example usage.
from grid import Grid
g = Grid(100, 100, 10) # width and height in cells, cell width in pixels
g.highlightBox(2, 2, (0, 255, 0)) # cell x and y, rgb color tuple
g.clearGrid()
Here is the code I have so far. The problem is, I need to have an event loop to keep the window open and make the close button functional, but I also need to allow the other functions to draw to the screen.
import pygame
import sys
class Grid:
colors = {"blue":(0, 0, 255), "red":(255, 0, 0), "green":(0, 255, 0), "black":(0, 0, 0), "white":(255, 255, 255)}
def __init__(self, width, height, cellSize, borderWidth=1):
self.cellSize = cellSize
self.borderWidth = borderWidth
self.width = width * (cellSize + borderWidth)
self.height = height * (cellSize + borderWidth)
self.screen = pygame.display.set_mode((self.width, self.height))
running = True
while running:
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = False
def clearGrid(self):
pass
def highlightBox(self, x, y, color):
xx = x * (self.cellSize + self.borderWidth)
yy = y * (self.cellSize + self.borderWidth)
pygame.draw.rect(self.screen, color, (xx, yy, self.cellSize, self.cellSize), 0)
When I run the first sample, the code will be stuck in the loop, not allowing me to run the highlightBox function until the loop is done(the exit button is pressed).
For starters, I wouldn't put the game loop inside the initialization function; find some other place for it. To solve this, simply put the code you want to execute in the game loop, next to the code for handling events:
running = True
while running:
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = False
# Print your screen in here
# Also do any other stuff that you consider appropriate
I think what you need is to disconnect the Grid class from the display of it. You sould make it generate surfaces, that would be printed to the screen Surface by the main game-loop. Your init , highlight_cell, and clear_grid methods could return Surfaces for example, or make a get_surface method that would be called once every game-loop
This would give much more flexibility
I got a working version with the multiprocessing library and pipes. It seems kinda unpythonic but it will work for this project.
import pygame
import sys
from multiprocessing import Process, Pipe
class Grid:
colors = {"blue":(0, 0, 255), "red":(255, 0, 0), "green":(0, 255, 0), "black":(0, 0, 0), "white":(255, 255, 255)}
def __init__(self, width, height, cellSize, borderWidth=1):
self.cellSize = cellSize
self.borderWidth = borderWidth
self.width = width * (cellSize + borderWidth)
self.height = height * (cellSize + borderWidth)
#pygame.draw.rect(self.screen, todo[1], (todo[2], todo[3], todo[4], todo[5]), 0)
self.parent_conn, self.child_conn = Pipe()
self.p = Process(target=self.mainLoop, args=(self.child_conn, self.width, self.height,))
self.p.start()
def close():
self.p.join()
def clearGrid(self):
pass
def highlightBox(self, x, y, color):
xx = x * (self.cellSize + self.borderWidth)
yy = y * (self.cellSize + self.borderWidth)
self.parent_conn.send(["box", color, xx, yy, self.cellSize, self.cellSize])
def mainLoop(self, conn, width, height):
#make window
screen = pygame.display.set_mode((self.width, self.height))
running = True
while running:
# is there data to read
if conn.poll():
#read all data
todo = conn.recv()
print("Recived " + str(todo))
#do the drawing
if todo[0] == "box":
print("drawing box")
pygame.draw.rect(screen, todo[1], (todo[2], todo[3], todo[4], todo[5]), 0) #color, x, y, width, height
todo = ["none"]
#draw to screen
pygame.display.flip()
#get events
event = pygame.event.poll()
if event.type == pygame.QUIT:
running = False

Categories