#Python error in my code

1 messages ยท Page 1 of 1 (latest)

trail pumice
#

you defined run variable within the scope of main function

#

And then you try to use it outside of the function

#

So there's your problem

#

You messed up indentations so python things everything else is global

#
import pygame
import sys

# Initialize pygame
pygame.init()

# Constants
WIDTH, HEIGHT = 800, 600
PLAYER_WIDTH = 40
PLAYER_HEIGHT = 60
PLAYER_VEL = 5
BG_COLOR = (255, 255, 255)

WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game")

def draw(player):
    WIN.fill(BG_COLOR)  # Clear the screen
    pygame.draw.rect(WIN, (255, 0, 0), player)  # Draw the player
    pygame.display.update()  # Update the display

def main():
    run = True
    player = pygame.Rect(200, HEIGHT - PLAYER_HEIGHT, PLAYER_WIDTH, PLAYER_HEIGHT)

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                sys.exit()

        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            player.x -= PLAYER_VEL
        if keys[pygame.K_d]:
            player.x += PLAYER_VEL

        draw(player)

if __name__ == "__main__":
    main()

This should be correct

#

Python is indentation sensitive

#

thankyou for your help mister.i'll try your method.

#

it works

#

Nice ๐Ÿ™‚

#

your remake my code ,right?

#

did you see my code in github?

#

in that code the error is from "while run" comand line right?

#

What is the answer fir that error

#

In that code, as I said, you used run variable outside of your main function

#

Everything was outside of main function

#

Because your identations were not correct

#

what is i want to fix in that error

trail pumice
#

only that

#
def main():
  run = True

is not the same as

def main():
run = True

You see the difference?

#

And I fixed it, basically, in example I sent

#

yes

#

the space

#

Yes

#

ah

#

i'll try

#

i remove the space

trail pumice
#
import pygame
import time
import random

WIDTH, HEIGHT = 750, 650
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Survive From Trash")

BG = pygame.image.load("images/trash.jpg")

PLAYER_WIDTH = 40
PLAYER_HEIGHT = 60

PLAYER_VEL = 5


def draw(player):
    WIN.blit(BG,(0, 0))

    player.draw.rect(WIN, "red", player)

    pygame.display.update()


def main():
  run = True
  player = pygame.Rect(200, HEIGHT - PLAYER_HEIGHT, PLAYER_WIDTH, PLAYER_HEIGHT)

  while run:
      for event in pygame.event.get():
          if event.type == pygame.QUIT:
              run = False
              break
  
      keys = pygame.key.get_pressed()
      if keys[pygame.K_a]:
          player.x -= PLAYER_VEL
      if keys[pygame.K_d]:
          player.x += PLAYER_VEL
  
      draw(player)
  
  pygame.quit()

if __name__ == "__main__":
    main()
#

what is the difference?

#

what did you do?

#

im in rush

#

Can you not tell space difference between this and your original code in github?

trail pumice
#

the space?

#
aaaaaaaaaaaaaaaaaaaaaaaaa

vs

      aaaaaaaaaaaaaa
#

I can't explain any simpler

#

Check both codes and you will see

#

i see

#

space?

#

LOL

#

i'm not a pro

#

Well that's the most basic of python

#

thats why i'm slow

#

Space (identation) sensitivity as I mentioned in the beginning

#

i canot see the difference

trail pumice
#

wait i see

#

however the error about while run.i dont know whats going on

#

what Tell me which part did you change. Then I will have to buy the parts

#

Just add identation for all of this

trail pumice
#

So it is inside main function and not outside of it

#

@trail pumice https://www.youtube.com/watch?v=waY3LfJhQLY i code from this guy.is my code isn't the same ?(i'm not done.but i try to run the code in 22:18 timeline .i try to cheack the code like him)

In today's video I will be giving you guys an introduction to Python Game Development using the Pygame module, which is a simple 2D graphics library in Python. This tutorial will be simple, fun, yet allow you to create an extensible game!

๐Ÿ’ป Master Blockchain and Web 3.0 development today by using BlockchainExpert: https://algoexpert.io/blockcha...

โ–ถ Play video
trail pumice
#

Again, look at your code and look at his code

#

i fix the error
this guy help me from a call. //github.com/OsandaG
thank you for your help and sorry about wasting time for me.

#

๐Ÿ˜ข sorry

trail pumice
#

Ok, glad you resolved it ๐Ÿ™‚