pygame blitting - center - python

I am trying to make a script in python for pygame to draw a button with text centered, but when I blit onto the screen, it blits to the x and y I give it, not a proportionally centered location. I want to be able to center it to a set of (x,y,w,h). How would I do this? Here's my code:
# Imports
import pygame
class Text:
'Centered Text Class'
# Constructror
def __init__(self, text, (x,y,w,h), color = (0,0,0)):
self.x = x
self.y = y
self.w = w
self.h = h
# Start PyGame Font
pygame.font.init()
font = pygame.font.SysFont("sans", 20)
self.txt = font.render(text, True, color)
# Draw Method
def Draw(self, screen):
coords = (self.x, self.y)
screen.blit(self.txt, coords)
Edit: Comments, yes I know but I only used x and y as temporary variables because I have no idea what the centered x and y would be to center the text. (I want to know how to center its CENTER to a rect, not its top left corner)

You'll want to use the font.size() method to determine how large the rendered text will be.
Something like:
class Text:
"""Centered Text Class"""
# Constructror
def __init__(self, text, (x,y), color = (0,0,0)):
self.x = x #Horizontal center of box
self.y = y #Vertical center of box
# Start PyGame Font
pygame.font.init()
font = pygame.font.SysFont("sans", 20)
self.txt = font.render(text, True, color)
self.size = font.size(text) #(width, height)
# Draw Method
def Draw(self, screen):
drawX = self.x - (self.size[0] / 2.)
drawY = self.y - (self.size[1] / 2.)
coords = (drawX, drawY)
screen.blit(self.txt, coords)

I think something like the following does what you want. It uses pygame.font.Font.size() to determine the amount of space needed to render the text, and then centers that within rectangular region defined by CenteredText instance.
class CenteredText(object):
""" Centered Text Class
"""
def __init__(self, text, (x,y,w,h), color=(0,0,0)):
self.x, self.y, self.w, self.h = x,y,w,h
pygame.font.init()
font = pygame.font.SysFont("sans", 20)
width, height = font.size(text)
xoffset = (self.w-width) // 2
yoffset = (self.h-height) // 2
self.coords = self.x+xoffset, self.y+yoffset
self.txt = font.render(text, True, color)
def draw(self, screen):
screen.blit(self.txt, self.coords)
# for testing purposes, draw the rectangle too
rect = Rect(self.x, self.y, self.w, self.h)
pygame.draw.rect(screen, (0,0,0), rect, 1)
Given:
text = CenteredText('Hello world', (200,150,100,100))
Here's the results from calling text.draw(screen) in a 500x400 pixel window.

If you want to perfectly centre an object:
When you give Pygame coordinates for an object it takes them to be the coordinates for the upper left corner. Thus we have to halve the x and y coordinates.
coords = (self.x/2, self.y/2)
screen.blit(self.txt, coords)
Other than that your question is unclear.

Let pygame do the maths for you by using Rects and assigning the text's center to the destination center:
# Start PyGame Font
pygame.font.init()
font = pygame.font.SysFont("sans", 20)
class Text:
'Centered Text Class'
# Constructror
def __init__(self, text, (x,y,w,h), color = (0,0,0)):
self.rect = pygame.Rect(x, y, w, h)
self.txt = font.render(text, True, color)
# Draw Method
def Draw(self, screen):
coords = self.txt.get_rect()
coords.center = self.rect.center
screen.blit(self.txt, coords)

Related

The particle is not moving so what is the problem?

I follow a youtube video 'https://www.youtube.com/watch?v=cCiXqK9c18g' and in the video he make a class that represent a ball and he used pymunk to make a body and added it to the space and after that he created a method inside the ball class that will use pygame to draw the ball and I did almost like him
import pygame
import pymunk
pygame.init()
fps = 60
dt = 1/fps
dsX = 800 # screen width
dsY = 500 # screen height
display = pygame.display.set_mode((dsX, dsY))
space = pymunk.Space()
clock = pygame.time.Clock()
def convert_cor(point): # convet the coordinates from pymunk to pygame coordinates
return point[0], dsY - point[1]
class Particle: # v: velocity, pos: position[x, y], r: radius of particle(Circle)
def __init__(self, pos = [0, 0], v = [0, 0], r = 10, color = (255, 0, 0)):
self.pos = pos
self.v = v
self.r = r
self.color = color
self.body = pymunk.Body()
self.body.position = self.pos
self.body.velocity = self.v # this is the veclocity
self.shape = pymunk.Circle(self.body, self.r)
self.shape.dencity = 1
self.shape.elasticity = 1
space.add(self.body, self.shape)
def draw(self):
pygame.draw.circle(display, self.color, convert_cor(self.pos), self.r)
class Box: # thickness of the sides of the box and L1, L2, L3, L4 are the sides of the box
def __init__(self, thickness, color):
self.thickness = thickness
self.color = color
L1 = pymunk.Body(body_type = pymunk.Body.STATIC)
L2 = pymunk.Body(body_type = pymunk.Body.STATIC)
L3 = pymunk.Body(body_type = pymunk.Body.STATIC)
L4 = pymunk.Body(body_type = pymunk.Body.STATIC)
L1_shape = pymunk.Segment(L1, (0, 0), (dsX, 0), self.thickness)
L2_shape = pymunk.Segment(L2, (dsX, 0), (dsX, dsY), self.thickness)
L3_shape = pymunk.Segment(L3, (dsX, dsY), (0, dsY), self.thickness)
L4_shape = pymunk.Segment(L4, (0, dsY), (0, 0), self.thickness)
space.add(L1, L1_shape)
space.add(L2, L2_shape)
space.add(L3, L3_shape)
space.add(L4, L4_shape)
def draw(self):
pygame.draw.line(display, self.color, convert_cor((0, 0)), convert_cor((dsX, 0)), self.thickness * 2)
pygame.draw.line(display, self.color, convert_cor((dsX, 0)), convert_cor((dsX, dsY)), self.thickness * 2)
pygame.draw.line(display, self.color, convert_cor((dsX, dsY)), convert_cor((0, dsY)), self.thickness * 2)
pygame.draw.line(display, self.color, convert_cor((0, dsY)), convert_cor((0, 0)), self.thickness * 2)
def Sim(): # the infinite while loop as a function
box = Box(2, (0, 255, 255))
particle = Particle(pos =[dsX/2, dsY/2], v = [-200, 500]) # here i gave the position and the velocity
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
display.fill((255, 255, 255))
box.draw()
particle.draw()
clock.tick(fps)
space.step(dt)
pygame.display.update()
Sim()
pygame.quit()
The thing is, I did also a class that will add a rigid sides for the display and i drew the sides from the Box class using the method 'draw' The problem is in the time 5:58 in the video he gave the ball velocity and it start moving and in my code it does not move. any idea why it doen't move?
note: I called the ball particle in my code
You error is both a typo and using the wrong variable.
Inside your particles draw function...
# OLD
def draw(self):
pygame.draw.circle(display, self.color, convert_cor(self.pos), self.r)
# New
def draw(self):
pygame.draw.circle(display, self.color, convert_cor(self.body.position), self.r)
You have to use the body's position cause that is the position of the physics body in pymunk's space.
Secondly...
class Particle: # v: velocity, pos: position[x, y], r: radius of particle(Circle)
def __init__(self, pos, v, r=10, color=(255, 0, 0)):
...
# Old
self.shape.dencity = 1
# New
self.shape.density = 1
Since density was not set to anything Pymunk was having a divide by zero error so it wouldn't update the body's position.
self.body.position = self.pos
To be clear about the problem:
From what I could find in the documentation, pymunk.Body.position is a property; it expects you to pass either an ordinary Python tuple or a Vec2d (not a list, although anything it's relying on internally to handle a tuple probably handles a list just fine), and calls some internal code written in another programming language. The effect is that, instead of storing your list object and then making changes to the list (which would be visible in your class, since it would be the same object), it just gets the initial values out of your list, and then doesn't use the list object.
This means that when Pymunk applies physics calculations, self.body.position changes, but self.pos does not. So the current self.pos is useless; we can't use it to check the object position.
If you don't need to do that, then there is no need to create a self.pos at all - just feed self.body.position = pos directly, and make sure to use self.body.position when drawing.
If you do, I recommend using your own property instead of trying to set self.pos. Do the above, and then add to the class:
#property
def pos(self):
return self.body.position
And if you want to change the position from your code (but you probably shouldn't! Why else are you using the physics engine in the first place?), also add:
#pos.setter
def pos(self, value):
self.body.position = value

How Can I Rotate My Sprite Towards The Player Even If Player Moves Position

so I have a sprite that shoots projectiles the projectiles shoot at the player I was wondering how I could make the sprite rotate to the player? VIDEO the player bullets attack the player what ever position he is at but how could I make the cannon sprite do the same?
my cannon class
shotsright = pygame.image.load("canss.png")
class enemyshoot:
def __init__(self,x,y,height,width,color):
self.x = x
self.y =y
self.height = height
self.width = width
self.color = color
self.shootsright = pygame.image.load("canss.png")
self.shootsright = pygame.transform.scale(self.shootsright,(self.shootsright.get_width()-150,self.shootsright.get_height()-150))
self.rect = pygame.Rect(x,y,height,width)
self.health = 10
self.hitbox = (self.x + -20, self.y + 30, 31, 57)
def draw(self):
self.rect.topleft = (self.x,self.y)
window.blit(self.shootsright,self.rect)
self.hits = (self.x + 20, self.y, 28,60)
pygame.draw.rect(window, (255,0,0), (self.hitbox[0], self.hitbox[1] - 60, 50, 10)) # NEW
pygame.draw.rect(window, (0,255,0), (self.hitbox[0], self.hitbox[1] - 60, 50 - (5 * (10 - self.health)), 10))
self.hitbox = (self.x + 100, self.y + 200, 81, 87)
black = (0,0,0)
enemyshoots1 = enemyshoot(1100,10,100,100,black)
enemyshooting = [enemyshoots1]
my full code: script
Basically you can rotate the image to point to some co-ordinate quite simply. You create a vector of the distance between your cannon and the player. This is then converted to an angle with the Vector.as_polar() function. The angle is used to rotate an original copy of the bitmap to the desired angle. Image rotation can be fairly CPU-consuming.
class enemyshoot:
def __init__(self, x, y, height, width, color):
[...]
# Make a Reference Copy of the bitmap for later rotation
self.original = pygame.image.load("canss.png")
self.image = self.original
self.rect = self.image.get_rect()
self.position = pygame.math.Vector2( ( x, y ) )
def lookAt( self, coordinate ):
# Rotate image to point in the new direction
delta_vector = coordinate - self.position
radius, angle = delta_vector.as_polar()
self.image = pygame.transform.rotozoom( self.original, -angle, 1 )
# Re-set the bounding rectangle and position since
# the dimensions and centroid will have (probably) changed.
current_pos = self.rect.center
self.rect = self.image.get_rect()
self.rect.center = current_pos
So the idea is you take the original bitmap, and rotate from that each time. If you keep rotating the same bitmap the slight differences will compound and the image will loose definition.
The other thing of note is that we're rotating around the bitmap's centre-point. But the rotation changes the bitmap's dimensions, also changing the centre-point. So this needs to be re-calculated and preserved.
You may find it useful to cache the rotated images once they have been rotated to save CPU. Maybe round the angle to the nearest 10 degrees, and then see if it's already been rotated. This would also allow you to pre-rotate all images and store them in a look-up table.
Edit: Calling lookAt() the mouse position:

Rotating a rectangle (not image) in pygame

In pygame I use pygame.draw.rect(screen, color, rectangle) for all the rectangles in my program. I want to be able to rotate these rectangles to any angle. I have seen the following code to rotate IMAGES but my question is with RECTANGLES.
pygame.transform.rotate(image, angle)
But I am working with rectangles, I don't have an image or "surface" that I can rotate. When I try to rotate a rectangle with
rect = pygame.draw.rect(screen, self.color, self.get_rectang())
rotatedRect = pygame.transform.rotate(rect, self.rotation)
screen.blit(rotatedRect)
This gives TypeError: must be pygame.Surface, not pygame.Rect on the line with .rotate()
My question is, how can I rotate a and display a RECTANGLE(x,y,w,h), not an image, in pygame.
The linked post that this is a "potential duplicate" of is not a duplicate. One answer explains about the consequences of rotating a rectangle and the other uses code for rotating an image.
See the second answer here: Rotating a point about another point (2D)
I think rectangles can only be horiz or vertical in their oreintation. You need to define the corners and rotate them and then draw and fill between them.
The other way is to make a class
class myRect(pygame.Surface):
def __init__(self, parent, xpos, ypos, width, height):
super(myRect, self).__init__(width, height)
self.xpos = xpos
self.ypos = ypos
self.parent = parent
def update(self, parent):
parent.blit(self, (self.xpos, self.ypos))
def rotate(self, angle):
#(your rotation code goes here)
and use that instead, as then you will be able to rotate it using the transform function.
import pygame as py
# define constants
WIDTH = 500
HEIGHT = 500
FPS = 30
# define colors
BLACK = (0 , 0 , 0)
GREEN = (0 , 255 , 0)
# initialize pygame and create screen
py.init()
screen = py.display.set_mode((WIDTH , HEIGHT))
# for setting FPS
clock = py.time.Clock()
rot = 0
rot_speed = 2
# define a surface (RECTANGLE)
image_orig = py.Surface((100 , 100))
# for making transparent background while rotating an image
image_orig.set_colorkey(BLACK)
# fill the rectangle / surface with green color
image_orig.fill(GREEN)
# creating a copy of orignal image for smooth rotation
image = image_orig.copy()
image.set_colorkey(BLACK)
# define rect for placing the rectangle at the desired position
rect = image.get_rect()
rect.center = (WIDTH // 2 , HEIGHT // 2)
# keep rotating the rectangle until running is set to False
running = True
while running:
# set FPS
clock.tick(FPS)
# clear the screen every time before drawing new objects
screen.fill(BLACK)
# check for the exit
for event in py.event.get():
if event.type == py.QUIT:
running = False
# making a copy of the old center of the rectangle
old_center = rect.center
# defining angle of the rotation
rot = (rot + rot_speed) % 360
# rotating the orignal image
new_image = py.transform.rotate(image_orig , rot)
rect = new_image.get_rect()
# set the rotated rectangle to the old center
rect.center = old_center
# drawing the rotated rectangle to the screen
screen.blit(new_image , rect)
# flipping the display after drawing everything
py.display.flip()
py.quit()
a more complex version of the quick replacement, in which you can define an arbitrary rotation center point for your rectangle - even outside of it (tested in python3):
def rectRotated( surface, color, pos, fill, border_radius, rotation_angle, rotation_offset_center = (0,0), nAntialiasingRatio = 1 ):
"""
- rotation_angle: in degree
- rotation_offset_center: moving the center of the rotation: (-100,0) will turn the rectangle around a point 100 above center of the rectangle,
if (0,0) the rotation is at the center of the rectangle
- nAntialiasingRatio: set 1 for no antialising, 2/4/8 for better aliasing
"""
nRenderRatio = nAntialiasingRatio
sw = pos[2]+abs(rotation_offset_center[0])*2
sh = pos[3]+abs(rotation_offset_center[1])*2
surfcenterx = sw//2
surfcentery = sh//2
s = pg.Surface( (sw*nRenderRatio,sh*nRenderRatio) )
s = s.convert_alpha()
s.fill((0,0,0,0))
rw2=pos[2]//2 # halfwidth of rectangle
rh2=pos[3]//2
pg.draw.rect( s, color, ((surfcenterx-rw2-rotation_offset_center[0])*nRenderRatio,(surfcentery-rh2-rotation_offset_center[1])*nRenderRatio,pos[2]*nRenderRatio,pos[3]*nRenderRatio), fill*nRenderRatio, border_radius=border_radius*nRenderRatio )
s = pygame.transform.rotate( s, rotation_angle )
if nRenderRatio != 1: s = pygame.transform.smoothscale(s,(s.get_width()//nRenderRatio,s.get_height()//nRenderRatio))
incfromrotw = (s.get_width()-sw)//2
incfromroth = (s.get_height()-sh)//2
surface.blit( s, (pos[0]-surfcenterx+rotation_offset_center[0]+rw2-incfromrotw,pos[1]-surfcentery+rotation_offset_center[1]+rh2-incfromroth) )
You cannot rotate a rectangle drawn by pygame.draw.rect. You have to create a transparent pygame.Surface and rotate the Surface:
rect_surf = pygame.Surface((widht, height), pygame.SRCLAPHA)
rect_surf.fill(color)
See How do I rotate an image around its center using PyGame?, to rotate the Surface.
I made a class which handles the rotation for you...
Extended from Ashish's design
from pygame import Surface, transform
from consts import screen
class BaseEntity:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
class Rectangle(BaseEntity):
def __init__(self, x: int, y: int, width: int, height: int, color: tuple):
super().__init__(x, y)
self.width = width
self.height = height
self.color = color
self.rotatation = 0
# the rectangle is a surface itself
self.surface = Surface((width, height))
self.surface.set_colorkey((0, 0, 0))
self.surface.fill(color)
self.rect = self.surface.get_rect()
def display(self, angle=None):
# updating values
self.surface.fill(
self.color
) # refill the surface color if you change it somewhere in the program
self.rect = self.surface.get_rect()
self.rect.center = (self.x, self.y)
# renderer
if angle is not None:
self.rotatation = angle
old_center = self.rect.center
new = transform.rotate(self.surface, self.rotatation)
self.rect = new.get_rect()
self.rect.center = old_center
screen.blit(new, self.rect)
Using a bit of trigonometry and the polygon function, I'm able to draw a rotated rectangle.
import math
import pygame.draw
def draw_rectangle(x, y, width, height, color, rotation=0):
"""Draw a rectangle, centered at x, y.
Arguments:
x (int/float):
The x coordinate of the center of the shape.
y (int/float):
The y coordinate of the center of the shape.
width (int/float):
The width of the rectangle.
height (int/float):
The height of the rectangle.
color (str):
Name of the fill color, in HTML format.
"""
points = []
# The distance from the center of the rectangle to
# one of the corners is the same for each corner.
radius = math.sqrt((height / 2)**2 + (width / 2)**2)
# Get the angle to one of the corners with respect
# to the x-axis.
angle = math.atan2(height / 2, width / 2)
# Transform that angle to reach each corner of the rectangle.
angles = [angle, -angle + math.pi, angle + math.pi, -angle]
# Convert rotation from degrees to radians.
rot_radians = (math.pi / 180) * rotation
# Calculate the coordinates of each point.
for angle in angles:
y_offset = -1 * radius * math.sin(angle + rot_radians)
x_offset = radius * math.cos(angle + rot_radians)
points.append((x + x_offset, y + y_offset))
pygame.draw.polygon(screen, color, points)
https://replit.com/#TimSwast1/RotateARectanlge?v=1
a quick replacement of the base pygame function adding rotation:
def rectRotated( surface, color, pos, fill, border_radius, angle ):
"""
- angle in degree
"""
max_area = max(pos[2],pos[3])
s = pg.Surface((max_area,max_area))
s = s.convert_alpha()
s.fill((0,0,0,0))
pg.draw.rect(s, color,(0,0,pos[2],pos[3]),fill, border_radius=border_radius)
s = pygame.transform.rotate(s,angle)
surface.blit( s, (pos[0],pos[1]) )
This code simulates rotating rectangles falling towards the ground. I used it in one of my games to make the background look awesome
import pygame
import random
class Square(pygame.sprite.Sprite):
def __init__(self, x, y):
super(Square, self).__init__()
self.win = win
self.color = (128, 128, 128)
self.speed = 3
self.angle = 0
self.side = random.randint(15, 40)
self.surface = pygame.Surface((self.side, self.side), pygame.SRCALPHA)
self.surface.set_colorkey((200,200,200))
self.rect = self.surface.get_rect(center=(x, y))
def update(self, win):
center = self.rect.center
self.angle = (self.angle + self.speed) % 360
image = pygame.transform.rotate(self.surface , self.angle)
self.rect = image.get_rect()
self.rect.center = center
self.rect.y += 1.5
if self.rect.top >= HEIGHT:
self.kill()
pygame.draw.rect(self.surface, self.color, (0,0, self.side, self.side), 4)
win.blit(image, self.rect)
if __name__ == '__main__':
pygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512
win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
clock = pygame.time.Clock()
FPS = 60
count = 0
square_group = pygame.sprite.Group()
running = True
while running:
win.fill((200,200,200))
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
count += 1
if count % 100 == 0:
x = random.randint(40, WIDTH-40)
y = 0
square = Square(x, y)
square_group.add(square)
count = 0
square_group.update(win)
pygame.draw.rect(win, (30,30,30), (0, 0, WIDTH, HEIGHT), 8)
clock.tick(FPS)
pygame.display.update()
pygame.quit()
Here's the output, it's not an gif though
Now if you want color filled rectangle instead of bordered only, update this line on line 31
pygame.draw.rect(self.surface, self.color, (0,0, self.side, self.side))
and if you don't want the rectangle to fall down comment line 26
A concise and fast function to draw a rotated rectangle. Uses NumPy
def rectRotated(self, surface, rect, color, rotation):
"""
Draws a rotated Rect.
surface: pygame.Surface
rect: pygame.Rect
color: pygame.Color
rotation: float (degrees)
return: np.ndarray (vertices)
"""
# calculate the rotation in radians
rot_radians = -rotation * pi / 180
# calculate the points around the center of the rectangle, taking width and height into account
angle = atan2(rect.height / 2, rect.width / 2)
angles = [angle, -angle + pi, angle + pi, -angle]
radius = sqrt((rect.height / 2)**2 + (rect.width / 2)**2)
# create a numpy array of the points
points = np.array([
[rect.x + radius * cos(angle + rot_radians), rect.y + radius * sin(angle + rot_radians)]
for angle in angles
])
# draw the polygon
pygame.draw.polygon(surface, color, points)
# return the vertices of the rectangle
return points

Pygame Scrolling Map

I'm making a rogue-like and I trying to make a camera follow the player as he moves by the map.
I was able to make the Draw function only happens when the tiles are inside the camera viewport[show in gray], but I can't make the camera stay in the corner of the window.
This is how it is:
And this is How it should be:
Is there a way to 'crop' the screen Surface, or perhaps copy only whats inside the camera viewport and blit it in the screen again.
Probably I'm doing this the hard way.
I'm iterating over the whole map, creating a rectangle for each tile, and checking if it's inside the Camera Viewport Rect using the '.contains()'.
EDIT:
This is how I'm drawing the map:
for x in xrange(mymap.width):
for y in xrange(mymap.height):
lit = field_of_view.lit(x, y)
visited = field_of_view.visited(x, y)
graphic = mymap.tileAt(x, y).graphic
if lit:
color = mymap.tileAt(x, y).color
elif visited:
color = GRAY
else:
color = BLACK
renderedgraphic = myfont.render(graphic, 1, color)
screen.blit(renderedgraphic, (x*TILESIZE, y*TILESIZE))
I do the same thing for the player, monsters, items and etc, but everything in it's own classmethod.
My camera is set like this:
class Camera(Object):
def __init__(self, x, y):
graphic = ''
Object.__init__(self, graphic, x, y)
self.rect = pygame.Rect(x, y, CAMERA_WIDTH * 2 + 5, CAMERA_HEIGHT * 2 + 5)
def update(self):
self.x = PLAYER.x
self.y = PLAYER.y
startx = ((self.x * TILESIZE) - CAMERA_WIDTH) + 5
starty = ((self.y * TILESIZE) - CAMERA_HEIGHT) + 5
self.rect = pygame.Rect(startx, starty, CAMERA_WIDTH * 2 + 5, CAMERA_HEIGHT * 2 + 5)
So I tried what user3762084 said.
in short:
for x in xrange(mymap.width):
for y in xrange(mymap.height):
... # do fov stuff
tile = Tile.At(x, y) # get tile instance
if tile:
tile.update() # update it's relative position
screen.blit(renderedgraphic, (tile.relX * TILESIZE, tile.relX * TILESIZE)) # blit the postion to it's relative position
This is what happens:
It's all squished in the side of the window. And if the player moves it all goes black.
What I have done before to make a scrolling environment was to give each object its coordinates in the world, and then give your camera a position. When you are drawing, you then calculate the position of each object on the screen based on the object's actual coordinates and the camera's coordinates.
class Tile:
def __init__(self, x, y, other_variables):
self.x = x
self.y = y
# relative vars for later
self.relX = None
self.relY = None
# then your camera should have a position as well as its width and height:
class Camera:
def __init__(self, x, y, width, height):
# assign those variables here
# your drawing function:
for tile in tiles:
tile.relX = tile.x - camera.x
tile.relY = tile.y - camera.y
# blit your tiles to the screen at the relative coordinates
In addition, you could also implement checks to see if the tile is completely outside of the camera space and not draw those tiles.
Ok I got what I was looking for, I did a litte hack tho. So if anyone have a better way to do this.
I changede the way my map was drawing
I took the Camera Rect positions [topleft and bottomright];
Converted it to World Position;
Iterate over it, with a enumerator too;
Did any lit/visited FOG calcunations with X and Y;
And Blited in the screen using the enumerators 'i' and 'j'.
Here's the code:
topleft = Map.toWorld(camera.rect.topleft)
bottomright = Map.toWorld(camera.rect.bottomright)
for i, x in enumerate(xrange(topleft[0], bottomright[0])):
for j, y in enumerate(xrange(topleft[1], bottomright[1])):
tile = mymap.tileAt(x, y)
object = [obj for obj in Object.OBJECTS if obj.pos == (x,y)]
if tile:
lit = field_of_view.lit(x, y)
visited = field_of_view.visited(x, y)
graphic = tile.graphic
if lit:
color = tile.color
elif visited:
color = GRAY
else:
color = BLACK
renderedgraphic = myfont.render(ch, 1, graphic)
screen.blit(renderedgraphic, Map.toScreen((i + 1, j)))
if object:
Draw.drawObject(object[0], Map.toScreen((i + 1, j)))
The only issue now is when the player is at the sides of the map it shows a black border.

Syntax error in Python/pygame [duplicate]

This question already has an answer here:
Nested arguments not compiling
(1 answer)
Closed 8 years ago.
I'm just learning python/pygame and I'm doing a physics tutorial. Unfortunately, I think it was made in an older version of python. I copied the code in the video exactly, but when I run it it returns "Failed to run script - syntax error - invalid syntax(physics.py, line 7)". I'm sure it's just something stupid and obvious that I'm missing, but any answer would go a long way for me!
import os, sys, math, pygame, pygame.mixer
from pygame.locals import *
screen_size = screen_width, screen_height = 600, 400
class MyCircle:
def __init__(self, (x, y), size, color = (255,255,255), width = 1):
self.x = x
self.y = y
self.size = size
self.color = color
self.width = width
def display(self):
pygame.draw.circle(screen, self.color, (self.x, self.y), self.size, self.width)
screen = pygame.display.set_mode(screen_size)
my_circle = MyCircle((100,100), 10, red)
my_circle_2 = MyCircle((200,200), 30, blue)
my_circle_3 = MyCircle((300,150), 40, green, 4)
my_circle_4 = MyCircle((450,250), 120, black, 0)
fps_limit = 60
run_me = True
while run_me:
clock.tick(fps_limit)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run_me = False
my_circle.display()
my_circle_2.display()
my_circle_3.display()
my_circle_4.display()
pygame.display.flip()
pygame.quit()
sys.exit()
You're probably using Python 3. Tuple parameter unpacking was removed in 3.X, so you have to change this:
def __init__(self, (x, y), size, color = (255,255,255), width = 1):
self.x = x
self.y = y
self.size = size
self.color = color
self.width = width
To:
def __init__(self, position, size, color = (255,255,255), width = 1):
self.x, self.y = position
self.size = size
self.color = color
self.width = width
Well, line 7 is this line:
def __init__(self, (x, y), size, color = (255,255,255), width = 1):
See that (x, y) in the middle of the parameter list? This is a feature called "tuple parameter unpacking", and it was removed in Python 3.0. (It's also discouraged in Python 2.6-2.7, but it works, so some people still use it.)
PEP 3113 has the full details.
If you run the 2to3 tool on your script, it'll tell you how to fix it.
But the simple fix is to replace that tuple (x, y) with a single parameter xy, and split it inside the function:
def __init__(self, xy, size, color = (255,255,255), width = 1):
self.x, self.y = xy
self.size = size
self.color = color
self.width = width

Categories