pygame rect.move movement not functioning properly

  • Last Update :
  • Techknowledgy :
self.rect.move(xy[0], xy[1])
1._
self.rect.move(xy[0], xy[1])

doesn't change anything. You've to assign the rectangle which is returned by .move to "itself":

self.rect = self.rect.move(xy)

Alternatively you can use pygame.Rect.move_ip(), moves the rectangle "in place" and changes the Rect object:

self.rect.move_ip(xy[0], xy[1])

Suggestion : 2

Pygame uses Rect objects to store and manipulate rectangular areas. A Rect can be created from a combination of left, top, width, and height values. Rects can also be created from python objects that are already a Rect or have an attribute named "rect".,The coordinates for Rect objects are all integers. The size values can be programmed to have negative values, but these are considered illegal Rects for most operations.,Returns a new rectangle that is cropped to be completely inside the argument Rect. If the two rectangles do not overlap to begin with, a Rect with 0 size is returned.,Sets the position and size of the rectangle, in place. See parameters for pygame.Rect()pygame object for storing rectangular coordinates for the parameters of this function.

x, y
top, left, bottom, right
topleft, bottomleft, topright, bottomright
midtop, midleft, midbottom, midright
center, centerx, centery
size, width, height
w, h
rect1.right = 10
rect2.center = (20, 30)
# Example using clipline().
clipped_line = rect.clipline(line)

if clipped_line:
   # If clipped_line is not an empty tuple then the line
# collides / overlaps with the rect.The returned value contains
# the endpoints of the clipped line.
start, end = clipped_line
x1, y1 = start
x2, y2 = end
else:
   print("No clipping. The line is fully outside the rect.")

Suggestion : 3

No, it's not just the sprite. The dirty rectangle should be around the sprite, AND around the area the sprite was last in. If you only update the character, the rest of the screen won't be updated, hence you will get this trail.,Sweet sprites! It looks fine on my computer. I'm guessing it would be to do with the time it takes to redraw the scene? Have a look into dirty-rectangles and dirty-sprites. These only update the parts of the screen or the sprites that need updating, without redrawing the whole scene.,Okay. It's pretty much solved. There are no longer any trails left when he moves around. There's still a few kinks here and there with moving around but I should be able to work those out. Thank you for your help.,I don't believe I'm understanding it correctly...I've got it to where a green square is drawn where the sprite is. Now all I need to do is use pygame.display.update() on that rectangle right? It doesn't work...He leaves a trail of his previous selves. :|

Can you tell what is wrong? I'm still looking into it.

if self.Dir == "North"
and keys.Is("up"):
   self.Rect = self.WalkNorth[self.CurrentFrame].get_rect()
self.screen.blit(self.WalkNorth[self.CurrentFrame], (self.X, self.Y))
self.Y -= self.Speed

#print self.Rect
dirty = pygame.rect.Rect([self.X, self.Y, self.Rect[2], self.Rect[3]])
#print dirty
pygame.draw.rect(self.screen, (0, 255, 0), dirty) #Comment out

pygame.display.update(dirty) #Update only that area ?? Doesn 't work.
pygame.display.flip()

else:
   self.Rect = self.StandNorth.get_rect()
self.screen.blit(self.StandNorth, (self.X, self.Y))

dirty = pygame.rect.Rect([self.X, self.Y, self.Rect[2], self.Rect[3]]) #Comment out
pygame.display.update(dirty)

pygame.draw.rect(self.screen, (0, 255, 0), dirty)
pygame.display.flip()
pygame.time.delay(50)

So close, I know it. I've got the rectangles to draw over the correct areas to be updated to test it. It works, but when I take out drawing the rectangles he still leaves a trail behind him.

if self.Dir == "North"
and keys.Is("up"):
   self.Rect = self.WalkNorth[self.CurrentFrame].get_rect()
self.PrevX = self.X
self.PrevY = self.Y

self.screen.blit(self.WalkNorth[self.CurrentFrame], (self.X, self.Y))
self.Y -= self.Speed

dirty = []

dirty.append(pygame.rect.Rect([self.X, self.Y, self.Rect[2], self.Rect[3]])) #Current Location
dirty.append(pygame.rect.Rect([self.PrevX, self.PrevY, self.Rect[2], self.Rect[3]])) #Previous Location

pygame.display.update(dirty)

else:
   self.Rect = self.StandNorth.get_rect()
self.screen.blit(self.StandNorth, (self.X, self.Y))

dirty = pygame.rect.Rect([self.X, self.Y, self.Rect[2], self.Rect[3]])
pygame.display.update(dirty)

pygame.display.flip()
pygame.time.delay(50)

Firstly, I am getting this error when I run into the top of the screen. It crashes the program.

Traceback (most recent call last):
File "C:\Users\Mark\Downloads\game\Game.py", line 34, in <module>
   GameMain()
   File "C:\Users\Mark\Downloads\game\Game.py", line 30, in GameMain
   player.Move()
   File "C:\Users\Mark\Downloads\game\Handlers.py", line 95, in Move
   gfx.BlitBackground(self.X, self.Y) #Blit background again
   File "C:\Users\Mark\Downloads\game\Handlers.py", line 13, in BlitBackground
   self.surf = pygame.surface.Surface((X, Y))
   error: Invalid resolution for Surface

Suggestion : 4

Changes you make to the screen—e.g. filling it with color, or drawing on it—do not show up immediately!,if this is the first call to pygame.display.update(), it is all changes since the screen was created,pygame.display.update() shows you all the drawing you did since the last call to pygame.display.update(),Remember that after you draw, you have to call pygame.display.update() before your changes show up.

You must do the following to get started with Pygame:

import pygame
import sys

pygame.init()

To open a window of size 640,480, use the statement below.

screen = pygame.display.set_mode((640, 480))

Instead, you have to call this function

pygame.display.update()

Examples:

red = (255, 0, 0)
green = (0, 255, 0)
blue = (0, 0, 255)
darkBlue = (0, 0, 128)
white = (255, 255, 255)
black = (0, 0, 0)
pink = (255, 200, 200)

Remember that after you draw, you have to call pygame.display.update() before your changes show up.

screen.fill(color)

Suggestion : 5

However, in many cases this will not work. Below are guides for installing pygame on both windows and mac.,To fix this we simply have to draw over the previous shape before redrawing another one. We can use window.fill(color) to do this.,Now we can draw a rectangle to the screen to represent our character. We will draw the rectangle in the main loop so that it is constantly redrawn each time we loop.,Pygame is a third party module designed for creating 2D games with python. It has very simple and intuitive syntax that can allows us to create powerful games quickly. Pygame uses surfaces for drawing. This means whenever we want to draw something to the screen it must be converted to a surface. There are many methods and functions within pygame that can do this easily for us.

import pygame
pygame.init()
import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
# This line creates a window of 500 width, 500 height
pygame.display.set_caption("First Game")
x = 50
y = 50
width = 40
height = 60
vel = 5
import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True

while run:
   pygame.time.delay(100) # This will delay the game the given amount of milliseconds.In our casee 0.1 seconds will be the delay

for event in pygame.event.get(): # This will loop through a list of any keyboard or mouse events.
if event.type == pygame.QUIT: # Checks
if the red button in the corner of the window is clicked
run = False # Ends the game loop

pygame.quit() # If we exit the loop this will execute and close our game
import pygame
pygame.init()

win = pygame.display.set_mode((500, 500))
pygame.display.set_caption("First Game")

x = 50
y = 50
width = 40
height = 60
vel = 5

run = True

while run:
   pygame.time.delay(100)

for event in pygame.event.get():
   if event.type == pygame.QUIT:
   run = False

pygame.draw.rect(win, (255, 0, 0), (x, y, width, height)) #This takes: window / surface, color, rect
pygame.display.update() # This updates the screen so we can see our rectangle

pygame.quit()