Python 'FOOD' object has no attribute 'draw' hatası

samet36

Centipat
Katılım
1 Ağustos 2022
Mesajlar
611
Daha fazla  
Cinsiyet
Erkek
Python'da klasik bir yılan oyunu yazıyordum. Yılanın yiyeceği yemegi ayarladım fakat çalıştırdıgımda söyle bir hata veriyor:

1664029516541.png


Şu kod yüzünden hata veriyor ne yapmalıyım.

1664029564051.png
 
Son düzenleyen: Moderatör:
Kodu tam olarak atsanız da yardım etsek?

Python:
from cmath import rect
from tkinter import font
from turtle import down, right
import pygame, sys, random
screen_width = 600
screen_height = 600
gridsize = 20
grid_width = screen_width / gridsize
grid_height = screen_height / gridsize
light_green = (0,170,140)
dark_green = (0,140,120)
food_color = (250,200,0)
snake_color = (34,34,34)
up = (0,-1)
down = (0,1)
right = (1,0)
left = (-1,0)

class FOOD:
    def __init__(self):
        self.position = (0,0)
        self.color = food_color
        self.random_position()
    def random_position(self):
        self.position = (random.randint(0,grid_width-1)*gridsize,random.randint(0,grid_height-1)*gridsize)
        def draw(self, surface):
            rect = pygame.Rect((self.position[0],self.position[1]),(gridsize,gridsize))
            pygame.draw.rect(surface,self.color,rect)

def drawGrid(surface):
    for y in range(0,int(grid_height)):
        for x in range(0,int(grid_width)):
            if (x + y) % 2 == 0:
                light = pygame.Rect((x * gridsize , y * gridsize),(gridsize,gridsize))
            pygame.draw.rect(surface,light_green,light)
        else:
            dark = pygame.Rect((x * gridsize , y * gridsize),(gridsize,gridsize))
            pygame.draw.rect(surface,dark_green,dark)
def main():
    pygame.init()
    screen = pygame.display.set_mode((screen_width,screen_height))
    font = pygame.font.SysFont("arial",20)
    surface = pygame.Surface(screen.get_size())
    surface = surface.convert()
    food = FOOD()
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        drawGrid(surface)
        food.draw(surface)
        screen.blit(surface,(0,0))
        pygame.display.update()
main()
 

Yeni konular

Geri
Yukarı