'I made a very simple Pygame Zero game, but my Actor image and the screen.clear() is not working. Can you help me fix this?

This is the full code for the very bad game example.

import pgzrun
from random import randint
from pgzero.builtins import Actor, animate, keyboard

apple = Actor('apple')

def draw():
    screen.clear()
    apple.draw()

def place_apple():
    apple.x = randint(10, 800)
    apple.y = randint(10, 600)

def on_mouse_down(pos):
    if apple.collidepont(pos):
        print("Good shot!")
        place_apple()
    else:
        print("You missed!")
        quit()

place_apple()
pgzrun.go()

the screen.clear() isn't working and apple = actor("apple") also isn't working. I have no idea why. Please help me!



Solution 1:[1]

try this:

from pgzero.actor import Actor
from pgzero.keyboard import keyboard

not sure if it helps.

Are you using mu editor or pycharm ??

Do you have an apple image in the correct path ?

Solution 2:[2]

i tried the same instructions and found that adding the import pygame solved the issue.

import pgzrun
from random import randint
import pygame

WIDTH  = 800
HEIGHT = 600
pig = Actor('pig')
schaap = Actor('schaap')
zombie = Actor('zombie')
konijn = Actor('konijn')

def draw():    
    screen.clear
    screen.surface = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
    pig.draw()
    schaap.draw()
    zombie.draw()
    konijn.draw()

def plaats_pig():    
    pig.x = randint(10,800)
    pig.y = randint(10,600)

def plaats_schaap():    
    schaap.x = randint(20,800)
    schaap.y = randint(20,600)

def plaats_zombie():    
    zombie.x = randint(70,800)
    zombie.y = randint(70,600)

def plaats_konijn():    
    konijn.x = randint(100,800)
    konijn.y = randint(100,600)


def on_mouse_down(pos):
    if pig.collidepoint(pos):
        sounds.schiet.play()    
        plaats_pig()
    elif schaap.collidepoint(pos):
        sounds.schiet.play()    
        plaats_schaap()
    elif zombie.collidepoint(pos):
        sounds.schiet.play()    
        plaats_zombie()
    elif konijn.collidepoint(pos):
        sounds.schiet.play()    
        plaats_konijn()
    else:
        sounds.mis.play()
        print('lekker puhh, mis')  
    

plaats_pig()
plaats_schaap()
plaats_zombie()
plaats_konijn()
pgzrun.go()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dielna Reboot
Solution 2 Mwalima