0

Source Code

import pygame

pygame.init()
space_width = 55
space_height = 40
win = pygame.display.set_mode((600, 400))
pygame.display.set_caption("hi")
playerimg = pygame.image.load('001-spaceship.png')
ply = pygame.transform.rotate(pygame.transform.scale(playerimg, (space_width, space_height)), 90)
print(win)


def draw_window(plyred):
    win.fill((255, 0, 0))
    win.blit(ply, (plyred.x, plyred.y))
    pygame.display.update()


def main():
    plyred = pygame.rect(100, 100, (space_width, space_height))


    run = True
    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        plyred += 1
    
    draw_window(plyred)
    
    pygame.quit()

My code won't run and only shows a temporary black screen before crashing.

Federico Baù
  • 6,013
  • 5
  • 30
  • 38
rob
  • 9
  • 1
  • 1
    Please correct the formatting (see [Markdown help](https://stackoverflow.com/editing-help)) and [indentation](https://docs.python.org/3/reference/lexical_analysis.html) – Rabbid76 Jan 18 '21 at 18:58

1 Answers1

0

First of all, there are so many mistake on the code, anyway here, this should at least show you something:

import pygame

pygame.init()
space_width = 55
space_height = 40
win = pygame.display.set_mode((800, 600))
pygame.display.set_caption("hi")

playerimg = pygame.image.load('001-spaceship.jpg')
ply = pygame.transform.rotate(pygame.transform.scale(playerimg, (space_width, space_height)), 90)
  

def draw_window(plyred):
    win.fill((255, 0, 0))
    win.blit(ply, (plyred.x, plyred.y))
    pygame.display.update()

def main():
    plyred = pygame.Rect(100, 100, space_width, space_height)

    run = True
    while run:

        win.blit(playerimg, (0, 0))
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                break
        draw_window(plyred)



if __name__ == '__main__':
    main()

Consideration and area where you should improve

  1. Indentation, indentation, indentation, Python is one of the few Programming languages where it punishes you if you don't indent correctly (hopefully) and so are other Programmers if you don't do so, here ---> Indentation in Python

  2. The Biggest issue was on the variable plyred, for of all is the Class Rect and not rect(which is a function from pygame.draw.rect) -----> Here your best friend Documentation :)

  3. Also, it was givin error because space_width, space_height was inside a tuple, so it should be pygame.Rect(100, 100, space_width, space_height) instead.

  4. You never call the main function, so one way is express it this way:

    if name == 'main': main()

Another is just call it as so at the end of the script:

main()

**DOCUMENTATION What does if name == “main”: do?


Projects

Federico Baù
  • 6,013
  • 5
  • 30
  • 38