#I’m so confused on this python code on pygame
27 messages · Page 1 of 1 (latest)
Can you copy and paste the code rather than taking a photograph?
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
I think it's an indentation issue on line 25
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()
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
Sorry what’s codeblocked I’m new to coding
it just a way to format your discord message so it is more readable
it adds syntax highlighting
Oh ok also how do I add the highlighting because it all looks so plan lol
It says
while run:
Indentationerror: unexpected indent
As I said previously, I believe you want to indent line 25
What like remove the space
yeah u need to put indent it so it is inside ur main function
Add white space at the start of the line so it's on the same level as the while loop after it
!tip 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
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
Put the cursor at the start of the line and press the space bar key on your keyboard 4 times
or use tab
omg its fixed lol thank you guys so much
if u use tab it should indent it however your editor is expecting you too, or how the rest of your indents are
when i tyed to use that it just made all the text green