Smooth Player Movement in Pygame | Python Pygame Player Movement Tutorial |(GameDev)

preview_player
Показать описание
Player movement for 2D games. Pygame
In this video, we'll see how we can add smooth player movement to our player in Pygame.
Thank you for watching this video, I hope you enjoyed it.
Don't forget to ask in the comment any questions you have and I will try to respond to them and help you out.

GitHub Link:

Tags:
- Game Dev
- Pygame
- Python Game Dev

Please leave a LIKE and SUBSCRIBE for more content!
Рекомендации по теме
Комментарии
Автор

For anyone who wants to fix speed increase you get by moving diagonally

In order to fix this issue you have to normalize the movement.
Firstly at 2:09, you want to change the velocity to a vector

self.vel = pygame.Vector2(0, 0)
Instead of self.velX = 0 and self.velY = 0

In the update function at 3:24 you can copy-paste this

self.vel = pygame.Vector2(0, 0)
if self.left_pressed and not self.right_pressed:
self.vel[0] = -self.speed
if self.right_pressed and not self.left_pressed:
self.vel[0] = self.speed
if self.up_pressed and not self.down_pressed:
self.vel[1] = -self.speed
if self.down_pressed and not self.up_pressed:
self.vel[1] = self.speed

if self.vel != (0, 0):
self.vel.normalize_ip()

self.x += self.vel[0] * self.speed
self.y += self.vel[1] * self.speed

self.rect = pygame.Rect(int(self.x), int(self.y), 32, 32)



This normalizes the vector and multiplies the velocity by the speed so you won't gain speed by going diagonally.

swippy
Автор

Hi i like nearly everything in the vid in fact you even made that in a class and it helps me really out. I just wanted so that you can config the size and choose how fast you are. I did that and it was very easy. Thank you :)

vulnoryx
Автор

Спасибо огромное, наконец-то решил проблему с дёрганием при подключении к серверу

justbkn
Автор

Thank you, nice idea of using key up and other techniques that I haven't thought of

niroscalfon
Автор

Hi ! The end result at the beginning is a very good idea ! Thanks

arnaudullens
Автор

this is really good, try and make the text a bit smaller and more compact next time though.

kirbyanimates
Автор

Thanks so much, I've subbed for more ;)

LucasD
Автор

would be helpful if you pin this.
note for this system:

1. due to you moving for example left and down, you would move diagonally down left, but at the speed of: root((player speed ** 2) * 2), so basically if your player doesnt move exactly 1 pixel per game loop, it will move diagonally faster

2. this system doesnt include acceleration, to see if you want acceleration in your game or not, simply after adding this system run the code, spam really fast all directions of movement, if you dont
like the way it feels you need to add acceleration

3. in this exact system, if you press oppsite directions the player wont move, but alot of games also include dominant side, meaning if you press left and right, it will move left. if you want to do that you gotta chamge the update() function, you gotta get rid of all the 'and' statements so you stay with only 4 if statements, then you gotta do this: fi you want left side to dominant over right, you gotta put the 'if self.right_pressed:' under the 'if self.left_pressed:' and change it to elif, same way for the vertical dominant side you choose

semyongalitsky
Автор

i had a problem with the #draw section

I put win.fill((12, 24, 36)) but it couldn't recognize "win for some reason"

saturrrn
Автор

hi, thanks for the video, helped soo much. but
i couldn't find where to write "playerimg = pygame.image.load()" or "screen.blit(playerImg, (x, y))" can you help me pls?

bigshrek
Автор

Hey @codeNULL maybe perhaps do a collision system tutorial using this code? checking for collision with colliderect then calculating the position of the collisions for each side? Would love to see it :D

tacticcoconut
Автор

Do you know how to make drag and drop movement in python games? For example, the kind of drag and drop in 2D dress up games? And how to make the clothes object interact with the model object?

diasybarrett
Автор

i really like the music at 1:29 (yeah of course im being sarcastic because THE MUSIC FREAKIN SUXS)

kalacarte
Автор

thanks for the tutorial, is there anyway to do the same thing but with sprites that can only move left and right and jumping?

thehappypeople
Автор

The thing is that it moves faster by diagonal (up+left example) than only horizontal and vertical, and that is hard to help with tho

DevDoomer
Автор

how would I make it a custom image instead of a rectangle thanks good vid.

ashtenjampayas
Автор

i actully do not understand the logic of triple quotes

vaibhavpandey
Автор

"""Smooth Movement in pygame"""
#You Are Welcome!!!
#imports
import pygame, sys

#Constants
WIDTH, HEIGHT = 400, 400
TITLE = "Smooth Movement"

#pygame initialization
pygame.init()
win = pygame.display.set_mode((WIDTH, HEIGHT))

clock = pygame.time.Clock()

#Player Class
class Player:
def __init__(self, x, y):
self.rect = pygame.Rect(x, y, 32, 32)
self.x = int(x)
self.y = int(y)
self.color = (250, 120, 60)
self.velX = 0
self.velY = 0
self.left_pressed = False
self.right_pressed = False
self.up_pressed = False
self.down_pressed = False
self.speed = 4

def draw(self, win):
pygame.draw.rect(win, self.color, self.rect)

def update(self):
self.velX = 0
self.velY = 0
if self.left_pressed and not self.right_pressed:
self.velX = -self.speed
if self.right_pressed and not self.left_pressed:
self.velX = self.speed
if self.up_pressed and not self.down_pressed:
self.velY = -self.speed
if self.down_pressed and not self.up_pressed:
self.velY = self.speed

self.x += self.velX
self.y += self.velY

self.rect = pygame.Rect(self.x, self.y, 32, 32)

#Player Initialization
player = Player(WIDTH/2, HEIGHT/2)

#Main Loop
while True:

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player.left_pressed = True
if event.key == pygame.K_RIGHT:
player.right_pressed = True
if event.key == pygame.K_UP:
player.up_pressed = True
if event.key == pygame.K_DOWN:
player.down_pressed = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
player.left_pressed = False
if event.key == pygame.K_RIGHT:
player.right_pressed = False
if event.key == pygame.K_UP:
player.up_pressed = False
if event.key == pygame.K_DOWN:
player.down_pressed = False

#Draw
win.fill((12, 24, 36))
player.draw(win)

#update
player.update()
pygame.display.flip()

clock.tick(120)

Drakonopia
Автор

thanks for the video. music makes you lose concentration.

DrWhot
Автор

bro I spent 30mins on this and it doesn't work.... does the pygame stuff not work on Because if so I've wasted hours of my time.

Willith_the_rd