#πŸ”’ Walking problems!

49 messages Β· Page 1 of 1 (latest)

molten island
#

So I'm making a game, wow how exotic, and I'm in very early stages of the process at the moment. It is meant to be an RPG, so I would assume it would be a big thing to have smooth character movement! :o) So, my problem is- the character seems to hit the wall which is OUTSIDE of the window on lower and right sides, which is not a problem for the other side for some reason. I attached all the assets and the video to show my problem in case i didnt elaborate well enough πŸ₯Ή k thx yall!!
I use pygames 2.6.1, python ver 3.11.9 and Kenny Yip Coding for tutorials at the moment if that helps.
(also, 51 is a symbolic number, must be kept)

import pygame
import os

X=850
Y=500

Player_x_loco = X/2
Player_y_loco = Y/2
#51/141 (size of the original pictture)
Dingus_width = 17
Dingus_height = 47
walk_speed = 5.1

#images vvv
sburb=pygame.image.load(os.path.join("asssets", "sburb.png"))
kawaii=pygame.image.load(os.path.join("asssets", "kawaiiSpace.png"))
ding_forward=pygame.image.load(os.path.join("asssets", "dingus_sprite.png"))

screen = pygame.display.set_mode((X, Y))
feel="placeholder"
pygame.display.set_caption(feel)
pygame.display.set_icon(sburb)
clock = pygame.time.Clock() #<--------FRAMERATE GCFGXCHVJK>
running = True

class Player(pygame.Rect):
    def __init__(self):
        pygame.Rect.__init__(self, Player_x_loco, Player_y_loco, Dingus_width, Dingus_height)
        self.image = ding_forward
player=Player()

#better have the visuals in its own lil thing (apparently- ig i see why)
def graph(): #short for GRAPHics
    #the up left corner -> (0,0); (down is +, up is -); (the x is normal)
    screen.fill((0,80,80))
    screen.blit(kawaii, (0,0))
    screen.blit(player.image, player)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: #x
            running = False
    graph()
    keys = pygame.key.get_pressed()
    if (keys[pygame.K_w] or keys[pygame.K_UP]):
        player.y=max(player.y-walk_speed, 0)
    if (keys[pygame.K_s] or keys[pygame.K_DOWN]):
        player.y=min(player.y+walk_speed, Y-player.height)
    if (keys[pygame.K_a] or keys[pygame.K_LEFT]):
        player.x=max(player.x-walk_speed,0)
    if (keys[pygame.K_d] or keys[pygame.K_RIGHT]):
        player.x=min(player.x+walk_speed, X-player.width)
    pygame.display.update() #<--------- necesaary 2see anything
    clock.tick(60)```
wanton ridgeBOT
#

@molten island

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

wraith linden
#

the character seems to hit the wall which is OUTSIDE of the window on lower and right sides when?

molten island
#

its on video!

wraith linden
#

oh ok sorry i thought its an asset nvm

versed fiber
molten island
versed fiber
#

thanks

wraith linden
#

i feel like the specified dimensions of the rect do not match the dimensions of the asset

wraith linden
molten island
#

ty goat the saturn πŸ˜‡

wraith linden
#

damn im glad, i learnt pygame today by teaching someone else the basics lol, now i could help you πŸ˜‚

#

ive never written a single line of pygame

#

tell me if u fix it and it works @molten island

molten island
#

gimme sek

wraith linden
#

k

wraith linden
#

yayyyy

molten island
#

eveyrthing im just ass at coding

wraith linden
#

it comes with experience

#

i have put like 5 yrs into programming by now

#

havent done much though cuz i started at 12 and now im 17 so my small brain could only do small projs

#

ive earned actual potential now

hushed bone
#

@molten island nice to hear that @wraith linden could solve your problem
also consider that you could improve your code (but will still just give you the same result) but using pathlib instead of os.path stuff and also a newer version of Python (if you use python 3.14 you will have to switch over to installing pygame-ce instead of `pygame, but you won't have to change a single line of code as the import is exactly the same)

wraith linden
#

idk which pygame ver theyre using. pygame-ce also uses import pygame

molten island
#

okee

hushed bone
wraith linden
#

oh ok ok

molten island
#

First time hearing about it though πŸ’” πŸ₯Ή

molten island
#

ty goat β€οΈβ€πŸ©Ή

hushed bone
#

your code could look something like

import pygame
from pathlib import Path

# cutting out the code that isn't relevant here

project_home = Path(__file__).resolve(strict=True).parent
assets_dir = project_home / "assets"

#images vvv
sburb = pygame.image.load(assets_dir / "sburb.png")
kawaii = pygame.image.load(assets_dir / "kawaiiSpace.png")
ding_forward = pygame.image.load(assets_dir / "dingus_sprite.png")
#

the paths are platform independent as well, regardless if the syntax uses / as a separator in the code

molten island
#

hot damn bro did you write it all within discord

#

youve been typing for a while

#

interesting! :oD

#

i dont quite understand

project_home = Path(__file__).resolve(strict=True).parent```
though, but I guess it is due to me merely not knowing better anout it, so I will just look it up. Thank you so much for caring and suggesitng stuff! :o)
#

kudos to u! β€οΈβ€πŸ©Ή

hushed bone
# molten island i dont quite understand ```py project_home = Path(__file__).resolve(strict=True)...

it's just to get the path to the directory where the python script that is currently executing
__file__ is a special variable holding a string of the path and name of the currently executing python script
Path() around that turns that string into a Path object
then we use the .resolve() method on that Path object to resolve the path to a absolute path and resolving any possible symbolic links in the file path into the real path, the strict=True argument will just make sure that the whole path actually exists (a bit of defensive coding that should in reality be unnecessary here)
then the .parent property will be the path without the last element of it (that would be the name of the python script file here) leaving us with the path to the directory where the python script file is stored

wanton ridgeBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.