I'm currently using Pygame to develop a game and I've decided that I'd group all my GUI objects together in a dictionary like so:
gui_objects = {
# The GuiObject parameters define a rect (for positioning) and background colour.
"healthbar" : GuiObject((10, 10, 100, 20), Colour.BLUE),
"mini_map" : GuiObject((10, 400, 50, 50), Colour.WHITE)
}
The reason I'm grouping GUI objects like this is so I can easily modify them like:
gui_objects.get("mini_map").set_enabled(false)
Now, when I want to render my GUI objects to the screen, I simply did this:
for key, value in gui_objects.iteritems():
value.render(screen)
This works, but for some reason, the white "mini_map" GuiObject gets rendered underneath the "healthbar" GuiObject. I decided to put the "mini_map" above the "healthbar" in the dictionary, but that changed nothing. But now here's the weird part. If I render the GUI objects separately, that is, by calling their render() functions separately, like this:
gui_objects.get("healthbar").render(screen)
gui_objects.get("mini_map" ).render(screen)
Then the GuiObjects overlap properly. My question now is, why do my GuiObjects not overlap properly when I render them using the for loop? Yet they overlap just fine when rendered separately?
Unfortunately, I can't upload images because I don't have enough reputation ¬_¬ But, here's the source code:
import pygame
# Just a definition of a few colours
class Colour(object):
WHITE = (255, 255, 255)
GREY = (128, 128, 128)
BLUE = ( 64, 128, 255)
# GuiObject is just a rectangle with a colour at the moment (for testing purposes).
class GuiObject(object):
def __init__(self, rect, colour):
self.rect = rect
self.colour = colour
def render(self, screen):
pygame.draw.rect(screen, self.colour, self.rect)
def main():
############################################################################
# Initialise
pygame.init()
screen = pygame.display.set_mode((800, 600))
# if render_type = 0 then render GuiObjects using a for loop
# if render_type = 1 then render GuiObjects separately.
render_type = 1
gui_objects = {
"hpbar_bg" : GuiObject(( 0, 0, 150, 600), (Colour.BLUE)),
"enemy_hpbar" : GuiObject((10, 10, 200, 400), (Colour.WHITE)),
}
############################################################################
# Main loop
while True:
########################################################################
# Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
########################################################################
# Render
screen.fill((0, 0, 0))
# Here, I render the GuiObjects.
if render_type == 0:
# This for loop messes up overlapping.
for key, value in gui_objects.iteritems():
value.render(screen)
elif render_type == 1:
# This works fine.
gui_objects.get("hpbar_bg").render(screen)
gui_objects.get("enemy_hpbar").render(screen)
pygame.display.flip()
if __name__ == "__main__":
main()
Has anyone got a clue as to why overlapping GuiObjects doesn't work properly when using the for loop?
I hope I've explained myself clearly enough. If not, just ask and I'll try to clarify.
Because dictionaries aren't ordered and it is drawing the objects in a different order than your other method.
A dictionary is just not designed for holding objects that need to stay in order.
You can either:
Use a pygame render group instead.
This would be the pygame-recommended way to store groups of objects that are going to be drawn, however you would have to convert your GUI objects into sprites I believe. EDIT: Also, pygame render groups are still not ordered so this wouldn't solve your particular problem.
Use a list of tuples [(name,value),...] instead (this will be the most similar to your current method because that's actually what the iteritems() dictionary method returns).
Here is your code re-written using method 2:
import pygame
# Just a definition of a few colours
class Colour(object):
WHITE = (255, 255, 255)
GREY = (128, 128, 128)
BLUE = ( 64, 128, 255)
# GuiObject is just a rectangle with a colour at the moment (for testing purposes).
class GuiObject(object):
def __init__(self, rect, colour):
self.rect = rect
self.colour = colour
def render(self, screen):
pygame.draw.rect(screen, self.colour, self.rect)
def main():
############################################################################
# Initialise
pygame.init()
screen = pygame.display.set_mode((800, 600))
# if render_type = 0 then render GuiObjects using a for loop
# if render_type = 1 then render GuiObjects separately.
render_type = 0
gui_objects = [
("hpbar_bg", GuiObject(( 0, 0, 150, 600), (Colour.BLUE))),
("enemy_hpbar", GuiObject((10, 10, 200, 400), (Colour.WHITE))),
]
############################################################################
# Main loop
while True:
########################################################################
# Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
########################################################################
# Render
screen.fill((0, 0, 0))
# Here, I render the GuiObjects.
if render_type == 0:
# This for loop no longer messes up overlapping.
for key, value in gui_objects:
value.render(screen)
pygame.display.flip()
if __name__ == "__main__":
main()
Because you would like to be able to do things like gui_objects.hpbar_bg.set_enabled(False) then I would look into a third option:
Restructuring your code to contain the GUI within a class itself, and then order the drawing of its components within its draw method.
Here is an example of 3 that doesn't deviate too far from what you've got:
import pygame
# Just a definition of a few colours
class Colour(object):
WHITE = (255, 255, 255)
GREY = (128, 128, 128)
BLUE = ( 64, 128, 255)
# GuiObject is just a rectangle with a colour at the moment (for testing purposes).
class GuiObject(object):
def __init__(self, rect, colour):
self.rect = rect
self.colour = colour
self.enabled = True
def render(self, screen):
if self.enabled:
pygame.draw.rect(screen, self.colour, self.rect)
class Gui(object):
def __init__(self):
self.hpbar_bg = GuiObject(( 0, 0, 150, 600), (Colour.BLUE))
self.enemy_hpbar = GuiObject((10, 10, 200, 400), (Colour.WHITE))
self.enabled = True
def render(self, screen):
#render my gui in the order i want
if self.enabled:
self.hpbar_bg.render(screen)
self.enemy_hpbar.render(screen)
def main():
############################################################################
# Initialise
pygame.init()
screen = pygame.display.set_mode((800, 600))
gui = Gui()
#uncomment to get the enabled/disabled behavior
#gui.hpbar_bg.enabled = False
#or disable the whole gui
#gui.enabled = False
############################################################################
# Main loop
while True:
########################################################################
# Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
########################################################################
# Render
screen.fill((0, 0, 0))
# Render GUI
gui.render(screen)
pygame.display.flip()
if __name__ == "__main__":
main()
Related
I am trying to learn PyMunk and I used their basic example from the website:
import pymunk
space = pymunk.Space()
space.gravity = 0,-1000
body = pymunk.Body(1,1666)
body.position = 50,100
poly = pymunk.Poly.create_box(body)
space.add(body, poly)
while True:
space.step(0.02)
But it does not create a window, does not show anything. How to use PyGame to create the graphical window?
What that example does is create a simulation, add a box shaped object inside and then run the simulation infinitely. The code doesn't print or draw anything, so you will not actually see the output. To get a better understanding and something on screen I suggest you start with the tutorial: http://www.pymunk.org/en/latest/tutorials/SlideAndPinJoint.html
Pymunk is a 2d rigid body physics library, which means that what it does is simulate how objects move and interact with each other in 2 dimensions. Its not made for drawing to the screen or read input.
You can of course use it as is without anything else, and just print out the result of the simulation. But more common is that you want to draw to the screen, read input and so on. One way to do that is by using the game library Pygame that helps out with drawing to the screen, reading input, having a game loop and so on.
Pymunk itself does have some helper functions so that you can easily connect it with Pygame (and a couple of other libraries), but this is not the core part. Usually these helper functions are good for when you want something quick-n-dirty such as a prototype and you don't have need to customize the drawing.
Now, this said, if you want to see something you can add a print statement to the while loop, so it becomes like this:
while True:
space.step(0.02)
print(body.position)
Then it will print out the position of the ball each step of the simulation, and you can see that its changing all the time (because of the gravity that is set on the space).
There are more advanced examples included in Pymunk that are both interactive and show something on screen. These examples depends on mostly either Pygame or Pyglet, but the principle is the same in case you have a different library you want to use it with.
Here's an example that shows how I use Pymunk in combination with pygame. The Entity class is a pygame.sprite.Sprite subclass to which I attach a pymunk.Body and a pymunk.Shape as well as a reference to the pm.Space, so that the bodies and shapes can be added and removed from it. The position of the sprite's rect gets set to the self.body.position each frame, so that we get the correct blit position for the self.image and can simply draw all sprites by calling self.sprite_group.draw(self.screen).
import math
import pygame as pg
import pymunk as pm
from pymunk import Vec2d
def flipy(p):
"""Convert chipmunk coordinates to pygame coordinates."""
return Vec2d(p[0], -p[1]+600)
class Entity(pg.sprite.Sprite):
def __init__(self, pos, space):
super().__init__()
self.image = pg.Surface((46, 52), pg.SRCALPHA)
pg.draw.polygon(self.image, (0, 50, 200),
[(0, 0), (48, 0), (48, 54), (24, 54)])
self.orig_image = self.image
self.rect = self.image.get_rect(topleft=pos)
vs = [(-23, 26), (23, 26), (23, -26), (0, -26)]
mass = 1
moment = pm.moment_for_poly(mass, vs)
self.body = pm.Body(mass, moment)
self.shape = pm.Poly(self.body, vs)
self.shape.friction = .9
self.body.position = pos
self.space = space
self.space.add(self.body, self.shape)
def update(self, dt):
pos = flipy(self.body.position)
self.rect.center = pos
self.image = pg.transform.rotate(
self.orig_image, math.degrees(self.body.angle))
self.rect = self.image.get_rect(center=self.rect.center)
# Remove sprites that have left the screen.
if pos.x < 20 or pos.y > 560:
self.space.remove(self.body, self.shape)
self.kill()
def handle_event(self, event):
if event.type == pg.KEYDOWN:
if event.key == pg.K_a:
self.body.angular_velocity = 5.5
elif event.key == pg.K_w:
self.body.apply_impulse_at_local_point(Vec2d(0, 900))
class Game:
def __init__(self):
self.done = False
self.clock = pg.time.Clock()
self.screen = pg.display.set_mode((800, 600))
self.gray = pg.Color('gray68')
self.red = pg.Color('red')
# Pymunk stuff.
self.space = pm.Space()
self.space.gravity = Vec2d(0.0, -900.0)
self.static_lines = [
pm.Segment(self.space.static_body, (60, 100), (370, 100), 0),
pm.Segment(self.space.static_body, (370, 100), (600, 300), 0),
]
for lin in self.static_lines:
lin.friction = 0.8
self.space.add(self.static_lines)
# A sprite group which holds the pygame.sprite.Sprite objects.
self.sprite_group = pg.sprite.Group(Entity((150, 200), self.space))
def run(self):
while not self.done:
self.dt = self.clock.tick(30) / 1000
self.handle_events()
self.run_logic()
self.draw()
def handle_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
if event.type == pg.MOUSEBUTTONDOWN:
self.sprite_group.add(Entity(flipy(event.pos), self.space))
for sprite in self.sprite_group:
sprite.handle_event(event)
def run_logic(self):
self.space.step(1/60) # Update physics.
self.sprite_group.update(self.dt) # Update pygame sprites.
def draw(self):
self.screen.fill(pg.Color(140, 120, 110))
for line in self.static_lines:
body = line.body
p1 = flipy(body.position + line.a.rotated(body.angle))
p2 = flipy(body.position + line.b.rotated(body.angle))
pg.draw.line(self.screen, self.gray, p1, p2, 5)
self.sprite_group.draw(self.screen)
# Debug draw. Outlines of the Pymunk shapes.
for obj in self.sprite_group:
shape = obj.shape
ps = [pos.rotated(shape.body.angle) + shape.body.position
for pos in shape.get_vertices()]
ps = [flipy((pos)) for pos in ps]
ps += [ps[0]]
pg.draw.lines(self.screen, self.red, False, ps, 1)
pg.display.flip()
if __name__ == '__main__':
pg.init()
Game().run()
pg.quit()
I want to draw antialiased shapes. I know you can use the pygame's gfxdraw module to accomplish this. However, it does seem to work only when drawing directly on the main window which is not suitable for me because I intend to use masks for collision checking.
Therefore a different surface is needed to create a mask that represents the circle.
How can you achieve this in pygame?
Minimal working example:
import pygame as pg
from pygame import gfxdraw
WIDHT, HEIGHT = 1200, 800
WIN = pg.display.set_mode((WIDHT, HEIGHT))
RADIUS = 80
WHITE = (255, 255, 255)
GREEN = (0, 200, 0)
RED = (200, 0, 0)
TRANS = (1, 1, 1)
class Circle(pg.sprite.Sprite):
def __init__(self,
radius: int,
pos: tuple[int, int],
color: tuple[int, int, int]):
super().__init__()
self.radius = radius
self.color = color
self.image = pg.surface.Surface((radius*2, radius*2))
self.image.fill(TRANS)
self.image.set_colorkey(TRANS)
self.rect = self.image.get_rect(center=(pos[0], pos[1]))
pg.gfxdraw.aacircle(self.image, self.rect.width//2, self.rect.height//2, radius, color)
pg.gfxdraw.filled_circle(self.image, self.rect.width//2, self.rect.height//2, radius, color)
self.mask = pg.mask.from_surface(self.image)
def draw(self):
WIN.blit(self.image, self.rect)
def main():
circle_1 = Circle(RADIUS, (500, 500), GREEN)
running = True
while running:
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
WIN.fill(WHITE)
circle_1.draw()
pg.gfxdraw.filled_circle(WIN, 700, 500, RADIUS, RED)
pg.gfxdraw.aacircle(WIN, 700, 500, RADIUS, RED)
pg.display.update()
if __name__ == "__main__":
main()
You need to create a transparent pygame.Surface with the per pixel alpha format. Use the SRCALPHA flag:
self.image = pg.surface.Surface((radius*2, radius*2))
self.image = pg.surface.Surface((radius*2, radius*2), pg.SRCALPHA)
However, for the highest quality, I suggest using OpenCV/cv2 (e.g. How to make a circular countdown timer in Pygame?)
I am working with Pygame currently, and I made a simple function to create window instances much like Windows 10 UI. the code I made doesn't give any errors or any unwanted outputs. It just seems not to be working properly, what I mean by "not working properly"; it just doesn't seem to be moving the frames that are meant to be dragged around by a master frame...
This is my code:
import pygame
from pyautogui import size
import datetime
pygame.init()
infoObject = pygame.display.Info()
surface = pygame.display.set_mode((900, 700))
run = True
clock = pygame.time.Clock()
def draw_text(text, font, text_col, x,y):
img = font.render(text, True, text_col)
rect = img.get_rect()
rect.center = (x,y)
surface.blit(img, rect)
return rect
class make_a_window():
def __init__(self,app,width=750,height=500):
self.app_name = str(app)
self.width = width
self.height = height
def run(self):
self.top_frame = pygame.draw.rect(surface, "#C0C0C0", pygame.Rect(0,0,int(self.width),40))#master frame
self.main_frame = pygame.draw.rect(surface, (255,255,255), pygame.Rect(0,40,int(self.width),int(self.height)))
self.red_frame_for_exit_btn_X = pygame.draw.rect(surface, (255,0,0), pygame.Rect(self.width-42,0,42,40))
self.exit_btn_X = draw_text("x", pygame.font.SysFont("calibri",25), "black", self.width-20, 15)
self.mouse_position = pygame.mouse.get_pos()
if pygame.mouse.get_pressed()[0] == 1:
if self.top_frame.collidepoint(self.mouse_position):
#moving the frames
self.top_frame.move(self.mouse_position[0],self.mouse_position[1])
self.main_frame.move(self.mouse_position[0]-40,self.mouse_position[1])
self.red_frame_for_exit_btn_X.move(self.mouse_position[0]-42,self.mouse_position[1])
self.exit_btn_X.move(self.mouse_position[0]-20,self.mouse_position[1])
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
run = False
app = make_a_window("hello")
app.run()
pygame.display.update()
clock.tick(60)
Sorry for my bad English. and thanks for the help, I really appreciate it 😃!
There is some logic error from line 32 to 41.
Firstly you should use the event queue by pygame.event.get() to track mouse activities(this is really important) and secondly why are you recording the mouse position before hand you are checking for its collision. Instead you should insert your
{self.mouse_position = pygame.mouse.get_pos()}
inside the collision checking if statement (rather that would not work smoothly until you use pygame.event.get())
One more thing that the function
pygame.Rect().move()
takes x and y offesets as its arguments not x and y coordinates.
So, mainly give focus on your event loop and the destination positions of your manual window. Maybe I would share the correct code later (don't wait for it.)
The method pygame.Rect.move doesn't move the rectangle itself, but it returns new rectangle that is moved. In compare, the method pygame.Rect.move_ip move the object in place.
However, these methods do not move anything that has been drawn on the screen. These methods simply move a rectangle representing an area of the screen. This rectangle can later be used to draw something on the screen at a new location.
Create the pygame.Rect objects in the class's constructor and use them to draw the objects. Use move_ip to move the rectangles:
class make_a_window():
def __init__(self,app,width=750,height=500):
self.app_name = str(app)
self.width = width
self.height = height
self.top_frame = pygame.Rect(0,0,int(self.width),40)
self.main_frame = pygame.Rect(0,40,int(self.width),int(self.height))
self.red_frame_for_exit_btn_X = pygame.Rect(self.width-42,0,42,40)
self.exit_btn_X = pygame.Rect(self.width-20, 15, 0, 0)
def run(self):
pygame.draw.rect(surface, "#C0C0C0", self.top_frame)
pygame.draw.rect(surface, (255,255,255), self.main_frame)
pygame.draw.rect(surface, (255,0,0), self.red_frame_for_exit_btn_X)
draw_text("x", pygame.font.SysFont("calibri",25), "black", self.exit_btn_X.topleft)
self.mouse_position = pygame.mouse.get_rel()
if pygame.mouse.get_pressed()[0] == 1:
if self.top_frame.collidepoint(self.mouse_position):
#moving the frames
move_rel = pygame.mouse.get_rel()
self.top_frame.move_ip(*move_rel)
self.main_frame.move_ip(*move_rel)
self.red_frame_for_exit_btn_X.move_ip(*move_rel)
self.exit_btn_X.move_ip(*move_rel)
I am trying to learn PyMunk and I used their basic example from the website:
import pymunk
space = pymunk.Space()
space.gravity = 0,-1000
body = pymunk.Body(1,1666)
body.position = 50,100
poly = pymunk.Poly.create_box(body)
space.add(body, poly)
while True:
space.step(0.02)
But it does not create a window, does not show anything. How to use PyGame to create the graphical window?
What that example does is create a simulation, add a box shaped object inside and then run the simulation infinitely. The code doesn't print or draw anything, so you will not actually see the output. To get a better understanding and something on screen I suggest you start with the tutorial: http://www.pymunk.org/en/latest/tutorials/SlideAndPinJoint.html
Pymunk is a 2d rigid body physics library, which means that what it does is simulate how objects move and interact with each other in 2 dimensions. Its not made for drawing to the screen or read input.
You can of course use it as is without anything else, and just print out the result of the simulation. But more common is that you want to draw to the screen, read input and so on. One way to do that is by using the game library Pygame that helps out with drawing to the screen, reading input, having a game loop and so on.
Pymunk itself does have some helper functions so that you can easily connect it with Pygame (and a couple of other libraries), but this is not the core part. Usually these helper functions are good for when you want something quick-n-dirty such as a prototype and you don't have need to customize the drawing.
Now, this said, if you want to see something you can add a print statement to the while loop, so it becomes like this:
while True:
space.step(0.02)
print(body.position)
Then it will print out the position of the ball each step of the simulation, and you can see that its changing all the time (because of the gravity that is set on the space).
There are more advanced examples included in Pymunk that are both interactive and show something on screen. These examples depends on mostly either Pygame or Pyglet, but the principle is the same in case you have a different library you want to use it with.
Here's an example that shows how I use Pymunk in combination with pygame. The Entity class is a pygame.sprite.Sprite subclass to which I attach a pymunk.Body and a pymunk.Shape as well as a reference to the pm.Space, so that the bodies and shapes can be added and removed from it. The position of the sprite's rect gets set to the self.body.position each frame, so that we get the correct blit position for the self.image and can simply draw all sprites by calling self.sprite_group.draw(self.screen).
import math
import pygame as pg
import pymunk as pm
from pymunk import Vec2d
def flipy(p):
"""Convert chipmunk coordinates to pygame coordinates."""
return Vec2d(p[0], -p[1]+600)
class Entity(pg.sprite.Sprite):
def __init__(self, pos, space):
super().__init__()
self.image = pg.Surface((46, 52), pg.SRCALPHA)
pg.draw.polygon(self.image, (0, 50, 200),
[(0, 0), (48, 0), (48, 54), (24, 54)])
self.orig_image = self.image
self.rect = self.image.get_rect(topleft=pos)
vs = [(-23, 26), (23, 26), (23, -26), (0, -26)]
mass = 1
moment = pm.moment_for_poly(mass, vs)
self.body = pm.Body(mass, moment)
self.shape = pm.Poly(self.body, vs)
self.shape.friction = .9
self.body.position = pos
self.space = space
self.space.add(self.body, self.shape)
def update(self, dt):
pos = flipy(self.body.position)
self.rect.center = pos
self.image = pg.transform.rotate(
self.orig_image, math.degrees(self.body.angle))
self.rect = self.image.get_rect(center=self.rect.center)
# Remove sprites that have left the screen.
if pos.x < 20 or pos.y > 560:
self.space.remove(self.body, self.shape)
self.kill()
def handle_event(self, event):
if event.type == pg.KEYDOWN:
if event.key == pg.K_a:
self.body.angular_velocity = 5.5
elif event.key == pg.K_w:
self.body.apply_impulse_at_local_point(Vec2d(0, 900))
class Game:
def __init__(self):
self.done = False
self.clock = pg.time.Clock()
self.screen = pg.display.set_mode((800, 600))
self.gray = pg.Color('gray68')
self.red = pg.Color('red')
# Pymunk stuff.
self.space = pm.Space()
self.space.gravity = Vec2d(0.0, -900.0)
self.static_lines = [
pm.Segment(self.space.static_body, (60, 100), (370, 100), 0),
pm.Segment(self.space.static_body, (370, 100), (600, 300), 0),
]
for lin in self.static_lines:
lin.friction = 0.8
self.space.add(self.static_lines)
# A sprite group which holds the pygame.sprite.Sprite objects.
self.sprite_group = pg.sprite.Group(Entity((150, 200), self.space))
def run(self):
while not self.done:
self.dt = self.clock.tick(30) / 1000
self.handle_events()
self.run_logic()
self.draw()
def handle_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
if event.type == pg.MOUSEBUTTONDOWN:
self.sprite_group.add(Entity(flipy(event.pos), self.space))
for sprite in self.sprite_group:
sprite.handle_event(event)
def run_logic(self):
self.space.step(1/60) # Update physics.
self.sprite_group.update(self.dt) # Update pygame sprites.
def draw(self):
self.screen.fill(pg.Color(140, 120, 110))
for line in self.static_lines:
body = line.body
p1 = flipy(body.position + line.a.rotated(body.angle))
p2 = flipy(body.position + line.b.rotated(body.angle))
pg.draw.line(self.screen, self.gray, p1, p2, 5)
self.sprite_group.draw(self.screen)
# Debug draw. Outlines of the Pymunk shapes.
for obj in self.sprite_group:
shape = obj.shape
ps = [pos.rotated(shape.body.angle) + shape.body.position
for pos in shape.get_vertices()]
ps = [flipy((pos)) for pos in ps]
ps += [ps[0]]
pg.draw.lines(self.screen, self.red, False, ps, 1)
pg.display.flip()
if __name__ == '__main__':
pg.init()
Game().run()
pg.quit()
I have come as far as drawing a rectangle in pygame however I need to be able to get text like "Hello" into that rectangle. How can I do this? (If you can explain it as well that would be much appreciated. Thank-you)
Here is my code:
import pygame
import sys
from pygame.locals import *
white = (255,255,255)
black = (0,0,0)
class Pane(object):
def __init__(self):
pygame.init()
pygame.display.set_caption('Box Test')
self.screen = pygame.display.set_mode((600,400), 0, 32)
self.screen.fill((white))
pygame.display.update()
def addRect(self):
self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
pygame.display.update()
def addText(self):
#This is where I want to get the text from
if __name__ == '__main__':
Pan3 = Pane()
Pan3.addRect()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit();
Thank you for your time.
You first have to create a Font (or SysFont) object. Calling the render method on this object will return a Surface with the given text, which you can blit on the screen or any other Surface.
import pygame
import sys
from pygame.locals import *
white = (255,255,255)
black = (0,0,0)
class Pane(object):
def __init__(self):
pygame.init()
self.font = pygame.font.SysFont('Arial', 25)
pygame.display.set_caption('Box Test')
self.screen = pygame.display.set_mode((600,400), 0, 32)
self.screen.fill((white))
pygame.display.update()
def addRect(self):
self.rect = pygame.draw.rect(self.screen, (black), (175, 75, 200, 100), 2)
pygame.display.update()
def addText(self):
self.screen.blit(self.font.render('Hello!', True, (255,0,0)), (200, 100))
pygame.display.update()
if __name__ == '__main__':
Pan3 = Pane()
Pan3.addRect()
Pan3.addText()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit();
Note that your code seems a little bit strange, since usually you do all the drawing in the main loop, not beforehand. Also, when you make heavy use of text in your program, consider caching the result of Font.render, since it is a very slow operation.
Hi!
To be honestly there is pretty good ways to write text in any place of current rect.
And now i'll show how to do it pretty easy.
First of all we need to create object of rect instance:
rect_obj = pygame.draw.rect(
screen,
color,
<your cords and margin goes here>
)
Now rect_obj is object of pygame.rect instance. So, we are free to manipulate with this methods. But, beforehand lets create our rendered text object like this:
text_surface_object = pygame.font.SysFont(<your font here>, <font size here>).render(
<text>, True, <color>
)
Afterall we are free to manipulate with all methods, as i mensioned before:
text_rect = text_surface_object.get_rect(center=rect_obj.center)
What is this code about?
We've just got center cords of out current rect, so easy!
Now, you need to blit ur screen like this:
self.game_screen.blit(text_surface_object, text_rect)
Happy coding! :)