pythonpygamecontrolspgzero

i am trying to make the actor "player" show on screen (along with the other actors) and add controls to it


import pgzero, pygame

#music, sprites and background

music.play("temp.mp3")
x = 0
y = 0

WIDTH = 850
HEIGHT = 425
screen = pygame.display.set_mode((WIDTH, HEIGHT))

title = Actor('title.png')      #calling sprites and saying their pos
title.pos = 400, 212
cont = Actor('cont.png')
cont.pos = 470, 300
player = Actor('ship.png')
player.pos = 100, 56
foe = Actor('foe.png')
foe.pos = 200, 112

def draw():
    screen.clear()
screen.fill((0, 0, 0))
title.draw()
cont.draw()
player.draw()
foe.draw()
vel = 5



#controls
screen.update()

while True:
    player.pos = x, y
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); sys.exit()
            main = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                print('left')
                x -= vel
            if event.key == pygame.K_RIGHT or event.key == ord('d'):
                print('right')
                x += vel
            if event.key == pygame.K_UP or event.key == ord('w'):
                print('up')
                y += vel
            if event.key == pygame.K_DOWN or event.key == ord('s'):
                print('down')
                y -= vel

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == ord('a'):
                print('left stop')
            if event.key == pygame.K_RIGHT or event.key == ord('d'):
                print('right stop')
            if event.key == pygame.K_UP or event.key == ord('w'):
                print('up stop')
            if event.key == pygame.K_DOWN or event.key == ord('s'):
                print('down stop')
            if event.key == ord('q'):
                pygame.quit()
                sys.exit()
                main = False
screen.update()

I have tried to get it to work but i keep getting various errors the one I'm getting at the moment its: AttributeError: 'NoneType' object has no attribute 'blit' I'm new to coding i find it quite interesting but i dont seem to be getting the hang of it haha. Hopefully you can help my issue :D Thanks!


Solution

  • The Indentation in your code is incorrect after screen.clear(). You have to delete the complete while True:-loop. This code will not work with Pygame Zero. It is a Pygame application and event loop. Pygame Zero is based on Pygame but it is not the same. You cannot use Pygame zero, but the event handling of Pygame. Note draw is continuously invoked in the background. draw is the "application loop".

    A working example may look as follows:

    import pgzrun
    import pygame
    
    WIDTH = 850
    HEIGHT = 425
    
    title = Actor('title.png')      #calling sprites and saying their pos
    title.pos = 400, 212
    cont = Actor('cont.png')
    cont.pos = 470, 300
    player = Actor('ship.png')
    player.pos = 100, 56
    foe = Actor('foe.png')
    foe.pos = 200, 112
    vel = 5
    
    music.play("temp.mp3")
    
    def draw():
        screen.clear()
        screen.fill((0, 0, 0))
        title.draw()
        cont.draw()
        player.draw()
        foe.draw()
    
    def update():
        keys = pygame.key.get_pressed()
        x, y = player.pos
        x += (keys[pygame.K_d] - keys[pygame.K_a]) * vel
        y += (keys[pygame.K_s] - keys[pygame.K_w]) * vel
        player.pos = x, y
    
    pgzrun.go()