#I’m so confused on this python code on pygame

27 messages · Page 1 of 1 (latest)

wary silo
arctic heart
#

Can you copy and paste the code rather than taking a photograph?

wary silo
#

I’m trying to see if the player is on the screen but this line of code has been working this whole time so I’m a bit confused

#

Yea for sure hold on

arctic heart
#

I think it's an indentation issue on line 25

wary silo
#

import pygame
import time
import random

WIDTH, HIGHT = 1000, 700
WIN = pygame.display.set_mode((WIDTH, HIGHT))
pygame.display.set_caption("space dodge")

BG = pygame.transform.scale(pygame.image.load("Space-Background-Image-2.jpg"), (WIDTH, HIGHT))

PLAYER_WIDTH = 40
PLAYER_HEIGHT = 60

def draw(player):

WIN.blit(BG, (0, 0))

pygame.draw.rect(WIN, "blue", player)

pygame.display.update()

def main():
run = True

player = pygame.Rect(200, HIGHT - PLAYER_HEIGHT,
PLAYER_WIDTH, PLAYER_HEIGHT)

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

    draw(player)

pygame.quit()

if name == "main":
main()

river spruce
#

whats the actual error??

#
import pygame
import time
import random

WIDTH, HIGHT = 1000, 700
WIN = pygame.display.set_mode((WIDTH, HIGHT))
pygame.display.set_caption("space dodge")

BG = pygame.transform.scale(pygame.image.load("Space-Background-Image-2.jpg"), (WIDTH, HIGHT))

PLAYER_WIDTH = 40
PLAYER_HEIGHT = 60

def draw(player):

    WIN.blit(BG, (0, 0))

    pygame.draw.rect(WIN, "blue", player)

    pygame.display.update()

def main():
    run = True

player = pygame.Rect(200, HIGHT - PLAYER_HEIGHT,
                     PLAYER_WIDTH, PLAYER_HEIGHT)

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

        draw(player)

    pygame.quit()

if name == "main":
    main()
``` codeblocked for u
#

more readable now

wary silo
#

Sorry what’s codeblocked I’m new to coding

river spruce
#

it just a way to format your discord message so it is more readable
it adds syntax highlighting

wary silo
river spruce
wary silo
arctic heart
#

As I said previously, I believe you want to indent line 25

wary silo
#

What like remove the space

river spruce
#

yeah u need to put indent it so it is inside ur main function

arctic heart
#

!tip indentation

stray roostBOT
#
Indentation

Indentation and white space in python is very important for defining blocks of code. It does not use the curly brace syntax many other languages use for this.
For code to be inside an if, while/for loop or function etc. it should be indented like the below. The indentation should also be consistent. Here is an example using a for loop:

for i in range(10):
    print("This lines prints while the loop is running")
    print(i)
print("This line prints after the loop has finished")

You can find further information here

wary silo
#

player = pygame.Rect(200, HIGHT - PLAYER_HEIGHT,
PLAYER_WIDTH, PLAYER_HEIGHT)

#

so how would i do that here sorry im haveing a hard time understanding

arctic heart
#

Put the cursor at the start of the line and press the space bar key on your keyboard 4 times

river spruce
#

or use tab

wary silo
#

omg its fixed lol thank you guys so much

river spruce
#

if u use tab it should indent it however your editor is expecting you too, or how the rest of your indents are

wary silo