OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Position of a moving image in Pygame [duplicate]

  • Thread starter Thread starter Nemo
  • Start date Start date
N

Nemo

Guest
I came across the following code from this post

Code:
import pygame
import math
  
class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.original_image = pygame.image.load('CarBlue64.png')
        self.original_image = pygame.transform.rotate(self.original_image, 90)
        self.image = self.original_image
        self.rect = self.image.get_rect(center=(x, y))
        self.direction = pygame.math.Vector2((0, -1))
        self.velocity = 5
        self.position = pygame.math.Vector2(x, y)
        self.health = 10

    def move(self, x, y, clamp_rect):
        self.position -= self.direction * y * self.velocity
        self.position += pygame.math.Vector2(-self.direction.y, self.direction.x) * x * self.velocity
        self.rect.center = round(self.position.x), round(self.position.y)

        test_rect = self.rect.clamp(clamp_rect)
        if test_rect.x != self.rect.x:
            self.rect.x = test_rect.x
            self.position.x = self.rect.centerx
            self.health = max(0, self.health - 1)
        if test_rect.y != self.rect.y:
            self.rect.y = test_rect.y
            self.position.y = self.rect.centery
            self.health = max(0, self.health - 1)

My question is: why does the function move() make two assignments to the attribute self.position? Why doesn't the image move if I remove the first assignment? What is the purpose of the second assignment then?

Code:
self.position -= self.direction * y * self.velocity # first assignment
self.position += pygame.math.Vector2(-self.direction.y, self.direction.x) * x * self.velocity # second assignment
<p>I came across the following code from this <a href="https://stackoverflow.com/questions/64867475/how-to-put-a-health-bar-over-the-sprite-in-pygame">post</a></p>
<pre><code>import pygame
import math

class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.original_image = pygame.image.load('CarBlue64.png')
self.original_image = pygame.transform.rotate(self.original_image, 90)
self.image = self.original_image
self.rect = self.image.get_rect(center=(x, y))
self.direction = pygame.math.Vector2((0, -1))
self.velocity = 5
self.position = pygame.math.Vector2(x, y)
self.health = 10

def move(self, x, y, clamp_rect):
self.position -= self.direction * y * self.velocity
self.position += pygame.math.Vector2(-self.direction.y, self.direction.x) * x * self.velocity
self.rect.center = round(self.position.x), round(self.position.y)

test_rect = self.rect.clamp(clamp_rect)
if test_rect.x != self.rect.x:
self.rect.x = test_rect.x
self.position.x = self.rect.centerx
self.health = max(0, self.health - 1)
if test_rect.y != self.rect.y:
self.rect.y = test_rect.y
self.position.y = self.rect.centery
self.health = max(0, self.health - 1)
</code></pre>
<p>My question is: why does the function <code>move()</code> make two assignments to the attribute <code>self.position</code>? Why doesn't the image move if I remove the first assignment? What is the purpose of the second assignment then?</p>
<pre><code>self.position -= self.direction * y * self.velocity # first assignment
self.position += pygame.math.Vector2(-self.direction.y, self.direction.x) * x * self.velocity # second assignment
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
5
Total visitors
5
Top