0

I am trying to move a sprite in pygame, I tried to adapt it from a youtube video where he goes through and make a tilebased game, unfortunately it is about 1 - 2 years old so the versions between the modules and python may have changed the functions of some code

Here is the code(this is the only relevant part, i have 2 other files with the main, and settings files which both work fine):

class Player(pg.sprite.Sprite):
def __init__(self, game, x, y):
    self.groups = game.all_sprites
    pg.sprite.Sprite.__init__(self, self.groups)
    self.game = game
    self.image = pg.Surface((tilesize, tilesize))
    self.image.fill(yellow)
    self.rect = self.image.get_rect()
    self.vx, self.vy = 0, 0
    self.x = x * tilesize
    self.y = y * tilesize

def get_keys(self):
    self.vx, self.vy = 0, 0
    keys = pg.key.get_pressed()
    if keys[pg.K_LEFT] or keys[pg.K_a]:
        self.vx = -playerspeed
    if keys[pg.K_RIGHT] or keys[pg.K_d]:
        self.vx = playerspeed
    if keys[pg.K_UP] or keys[pg.K_w]:
        self.vy = -playerspeed
    if keys[pg.K_DOWN] or keys[pg.K_s]:
        self.vy = playerspeed
    if self.vx != 0 and self.vy != 0:
        self.vx *= 0.7071
        self.vy *= 0.7071

def collide_with_walls(self, dir):
    if dir == 'x':
        hits = pg.sprite.spritecollide(self, self.game.walls, False)
        if hits:
            if self.vx > 0:
                self.x = hits[0].rect.left - self.rect.width
            if self.vx < 0:
                self.x = hits[0].rect.right
            self.vx = 0
            self.rect.x = self.x
    if dir == 'y':
        hits = pg.sprite.spritecollide(self, self.game.walls, False)
        if hits:
            if self.vy > 0:
                self.y = hits[0].rect.top - self.rect.height
            if self.vy < 0:
                self.y = hits[0].rect.bottom
            self.vy = 0
            self.rect.y = self.y

def update(self):
    self.get_keys()
    self.x += self.vx * self.game.dt
    self.y += self.vy * self.game.dt
    self.rect.x = self.x
    self.collide_with_walls('x')
    self.rect.y = self.y
    self.collide_with_walls('y')

The code itself is not broken, its that it just doesn't move, it blinks.

I would prefer to use it this way as it is a lot more smooth that other methods.

the video : Kidscanlearn

Thanks for any help :)

Tudor Popescu
  • 509
  • 1
  • 5
  • 16
  • 2
    In your `get_keys()` function, are you seeing the key presses? Are your `vx` and `vy` attributes being modified? Is your framerate so fast that `self.game.dt` is zeroing `vx` and `vy`. A [Minimal, Complete and Verifiable](https://stackoverflow.com/help/mcve) example is required for people to help you. – import random Feb 07 '18 at 04:29
  • I ran the code and couldn't reproduce the error, so I'm voting to close this question until an [mcve](https://stackoverflow.com/help/mcve) is provided. – skrx Feb 07 '18 at 05:06
  • @Eric vx and vy are only used here in the sprites code, I also set the FPS to be 60, I don't really know what seems to be stopping it, possibly the collision? – Tudor Popescu Feb 07 '18 at 06:58
  • We can't answer that without seeing a complete example. – skrx Feb 07 '18 at 09:55
  • @skrx The yt video has a link to github with the code – Tudor Popescu Feb 07 '18 at 14:11
  • I ran the code and, like @skrx, couldn't reproduce the error. The error must be in the code in the main and settings files. Please provide a [Minimal, Complete and Verifiable](https://stackoverflow.com/help/mcve) so we can fix your issue. – Micheal O'Dwyer Feb 07 '18 at 14:12
  • @TudorPopescu The code on GitHub doesn't help fix your problem because it is likely not the exact same. Post an [mcve](https://stackoverflow.com/help/mcve) of the code that you are running which produces the error. With the code you have provided there is nothing wrong meaning that there must be an error in the other code you have typed from the video. – Micheal O'Dwyer Feb 07 '18 at 14:21
  • @MichealO'Dwyer I just checked through the code and I found out that there was actually something wrong with the code, thanks for the help. – Tudor Popescu Feb 07 '18 at 17:59
  • 2
    @TudorPopescu I would encourage you to post an answer down below so people viewing this question both here and in the feed know that there is an answer which solved the problem. – Micheal O'Dwyer Feb 07 '18 at 18:06
  • @MichealO'Dwyer it was a copying problem, it was my fault :p, the game.dt function was poorly written. sorry for all this trouble – Tudor Popescu Feb 08 '18 at 20:49
  • @TudorPopescu No worries! That is what we are here for ;D – Micheal O'Dwyer Feb 08 '18 at 20:54
  • @MichealO'Dwyer If you know much about vectors and collision, help would be appreciated :) https://stackoverflow.com/questions/48695008/collision-with-vectors-with-other-classes-pygame – Tudor Popescu Feb 08 '18 at 21:25

0 Answers0