-3
  • I want to know why I get an error in this code.
  • I am trying to upload a character in JPG format and it gives me an error.
  • I am using IDLE and it is python 3.7.3
    import pygame
    pygame.init() #Initialize the game

    win = pygame.display.set_mode((1000,700)) # Sets the window size

    pygame.display.set_caption("First Game") # Title of window in the string

    *walkRight = [pygame.image.load('Person.JPG')]* # load image

    x = 100
    y = 690
    width = 10
    height = 10
    vel = 5
    isJump = False
    jumpCount = 5 # timer for mid-air
    left = False
    right = False

    run = True
    while run:
        pygame.time.delay(100) # waits for 100 miliseconds = 0.1 second

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

        keys = pygame.key.get_pressed()

        if keys[pygame.K_LEFT] and x > vel: # left
            x -= vel
        if keys[pygame.K_RIGHT] and x < 500 - width - vel: # right
            x += vel
        if not(isJump): # not jumping
            if keys[pygame.K_SPACE]:
                isJump = True

        else:
            if jumpCount >= -5:
                checker = 1 # positive jumpCount
                if jumpCount < 0:
                    checker = -1 # negative jumpcount
                y -= (jumpCount ** 2) * 0.5 * checker
                jumpCount -= 1
            else:
                isJump = False # allows the player to jump again
                jumpCount = 5 # how long it is in mid air

        win.fill((255,255,255)) # Fills the screen black
        pygame.draw.rect(win, (255, 0, 0), (x, y, width, height))
        pygame.display.update() # updates the screen each time

    pygame.quit()

Why does the place marked * give me an error?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Account_2
  • 53
  • 6

1 Answers1

0

As you haven't mentioned what operating system you are using, I will try to help with both Windows and Linux. It doesn't matter which OS you use if you have mentioned the correct path of the file in your code.

There are multiple ways to get absolute file paths in different operating systems:

For example:

  1. On Windows, hold the Shift key and right-click on the image and then click on "Copy as path".

  2. On Linux, just right-click on the image and click "copy".

Now get back to your code and paste the complete image path. Just to give you an example, the full path should look like "C:\Users\userName\Downloads\image.png" on windows and "/home/username/Desktop/image.png".

To avoid username in Linux, you can replace "/home/username" with "~/", so your full path would look like "~/Desktop/image.png".

Note: It is never recommended to hard code paths if you are planning to share the code or run it on production/a remote server. However, if you are just learning and experimenting, this should do the trick.

Amit Yadav
  • 4,422
  • 5
  • 34
  • 79
  • This led me to another problem. When I did the changes, it says, "Syntax error, (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3" : truncated \UXXXXXXXX escape – Account_2 Oct 02 '19 at 12:09
  • Try putting 1. `r"` [**r"C:\Users\DeePak\Desktop\myac.csv"**] before the path or 2. use forward slashes (/) instead of backward slashes (\) [**"C:/Users/DeePak/Desktop/myac.csv"**]or use escape character (\) before every backward slash [**"C:\\Users\\DeePak\\Desktop\\myac.csv"**]. Check [this](https://stackoverflow.com/questions/37400974/unicode-error-unicodeescape-codec-cant-decode-bytes-in-position-2-3-trunca) question for more details. – Amit Yadav Oct 02 '19 at 16:00
  • whoops! I found a typo error. The tick was added on accident – Account_2 Oct 11 '19 at 11:47
  • @Account_2 would you mind letting us know what typo error you are talking about? – Amit Yadav Oct 13 '19 at 15:11
  • 1
    The picture was called "wakRight" but it said "walkRight" – Account_2 Oct 16 '19 at 00:19
  • Anyway I have another question. You may want to try to help. – Account_2 Oct 16 '19 at 00:20