Pygame Display Position

How to center an image in the middle of the window in pygame?

You can get the center of the window through the window rectangle (pygame.Surface.get_rect):

screen = pygame.display.set_mode((800, 600))
center = screen.get_rect().center

Use this to blit an image (pygame.Surface object) in the center of the screen:

screen.blit(image, image.get_rect(center = screen.get_rect().center))

pygame.Surface.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0). However, the position of the rectangle can be specified with a keyword argument. In this case, the center of the rectangle is set by the center of the screen.

When the 2nd argument of blit is a rectangle (pygame.Rect), the upper left corner of the rectangle will be used as the position for the blit.

How to designate where pygame creates the game window

Positioning of windows is not handled by the client application. It's handled by the Window manager (metacity etc.).

The SDL library on which PyGame is based does have a few environment variables which can be used to give hints to the Window manager. These are hints which the WM may ignore but it's the best you can do.

The comments over here have an example.

python box2d pygame screen coordinates to box2d grid

While not familiar with Box2d for Python, but if what you are looking for is to convert mouse X,Y to world X,Y
just converting the mouse x,y with the Box2d conversion factor, PIXEL_PER_METERS or whatever you use, so f.ex
float PIXEL_PER_METER = 100.f;
thus screenpos 1200, 600 is 12,6 in "box2d units / meters"

  • and you've done that and are somewhat off, have you accounted for floating point / integer conversion?
    Python will after all call a C++ Box2d function, so make sure you're not converting any floats to integers , but on closer inspection it doesnt look like you are doing that.
  • Camera ScreenToWorld or WorldToScreen -> how are they implemented?
    I would think you have something along those lines somewhere,
    this problem is a fairly typical symptom of an issue in the Camera function.

But the answer to how to convert mouse to world is:
-if you want world in Box2d units: convert with the scaling factor
-add the Camera.Offset value to the mouse x,y to get world position

 Camera.MinWorld  <- update this depending on what camera is focused on

Mouse.x + Camera.MinWorld.x , Mouse.y + Camera.MinWorld.y = world position

world pos to screenpos:
pos.x - Camera.MinWorld.x , pos.y - Camera.MinWorld.y

x position of bullet in pygame

The line

blast = pygame.Rect(player.x, player.y , 10, 10)

assigns a Rect to blast, with the position of the player at this moment. This blast will stay still until you edit its position. You only change the y position here:

blast.y -= bl

But you don't update the x position when you launch the bullet. You could do this like that:

def launch_blast(): # call this function when you create the bullet.
# At this point in the program, just call it at the beginning.
blast.x = player.x + player.width / 2 - blast.width / 2

def blast1():
blast.y -= bl

To launch several bullets, you can use this code:

import pygame
from pygame.locals import *
pygame.init()

class Bullet:
def __init__(self):
self.rect = Rect(player.rect.x - 5 + player.rect.width / 2, player.rect.y , 10, 10)

def move(self):
self.rect.y -= 10 # move up
pygame.draw.ellipse(screen, [0, 255, 0], self.rect) # draw on screen

class Player:
def __init__(self):
self.rect = pygame.Rect(250, 450, 50, 50)

def move(self):
# move
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.rect.x -= 5
if keys[K_RIGHT]:
self.rect.x += 5

# don't go out of the screen
self.rect.x = min(max(self.rect.x, 0), 500 - self.rect.width)

# draw on screen
pygame.draw.ellipse(screen, [0, 0, 255], self.rect)

player = Player()
bullets = []

clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 500))
running = True

while running:
for event in pygame.event.get():
if event.type == QUIT:
running = False
if event.type == KEYDOWN and event.key == K_SPACE:
bullets.append(Bullet()) # generate a new bullet when space pressed

screen.fill((255, 255, 255)) # white

for bullet in bullets: # draw each bullet generated
bullet.move()
if bullet.rect.x < bullet.rect.height: # if out of the screen,
bullets.remove(bullet) # then remove it
player.move() # draw the player

pygame.display.flip()
clock.tick(30)


Related Topics



Leave a reply



Submit