'pygame.Surface' 对象没有属性 'color'
'pygame.Surface' object has no attribute 'color'
所以,我的问题是我正在尝试绘制一个矩形,但我一直收到错误提示 'pygame.Surface' object has no attribute 'color'
。有人可以帮助我吗?
完整错误消息
Traceback (most recent call last):
File "main.py", line 64, in <module>
game.new()
File "main.py", line 23, in new
self.run()
File "main.py", line 32, in run
self.draw()
File "main.py", line 55, in draw
self.snake.draw(self.screen)
File "C:\Users\sidna\Dropbox\Dev Stuff\Games\Snake\sprites.py", line 15, in draw
pygame.draw.rect(screen, self.color
AttributeError: 'pygame.Surface' object has no attribute 'color'
Sprites.py
import pygame
class Snake():
def __init__(self):
self.x = 0
self.y = 0
self.w = 10
self.h = 10
self.velX = 1
self.velY = 0
self.color = (0, 0, 0)
def draw(screen, self):
pygame.draw.rect(screen, self.color
(self.x, self.y, self.w, self.h))
def animate(self):
self.x = self.x + self.velX
self.y = self.y + self.velY
Main.py
from settings import *
from sprites import *
import pygame
import random
class Game:
def __init__(self):
# initialize game window, etc
pygame.init()
pygame.mixer.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock()
self.running = True
def new(self):
# start a new game
self.snake = Snake()
self.run()
def run(self):
# Game Loop
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.draw()
self.animate()
self.update()
def update(self):
# Game Loop - Update
pygame.display.update()
def events(self):
# Game Loop - events
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
def draw(self):
# Game Loop - draw
self.screen.fill((255, 255, 255))
self.snake.draw(self.screen)
def animate(self):
self.snake.animate()
game = Game()
while game.running:
game.new()
pygame.quit()
遵循 Python 禅 显式优于隐式。
Often, the first argument of a method is called self. This is nothing
more than a convention: the name self has absolutely no special
meaning to Python. Note, however, that by not following the convention
your code may be less readable to other Python programmers, and it is
also conceivable that a class browser program might be written that
relies upon such a convention.
这意味着你应该将第一个参数设置为self
,而且你忘记了一个comma
,代码应该是这样的:
def draw(self,screen):
pygame.draw.rect(screen, self.color,(self.x, self.y, self.w, self.h))
查看此问题Why always add self as first argument to class methods? and Python doc 了解更多详细信息。
所以,我的问题是我正在尝试绘制一个矩形,但我一直收到错误提示 'pygame.Surface' object has no attribute 'color'
。有人可以帮助我吗?
完整错误消息
Traceback (most recent call last):
File "main.py", line 64, in <module>
game.new()
File "main.py", line 23, in new
self.run()
File "main.py", line 32, in run
self.draw()
File "main.py", line 55, in draw
self.snake.draw(self.screen)
File "C:\Users\sidna\Dropbox\Dev Stuff\Games\Snake\sprites.py", line 15, in draw
pygame.draw.rect(screen, self.color
AttributeError: 'pygame.Surface' object has no attribute 'color'
Sprites.py
import pygame
class Snake():
def __init__(self):
self.x = 0
self.y = 0
self.w = 10
self.h = 10
self.velX = 1
self.velY = 0
self.color = (0, 0, 0)
def draw(screen, self):
pygame.draw.rect(screen, self.color
(self.x, self.y, self.w, self.h))
def animate(self):
self.x = self.x + self.velX
self.y = self.y + self.velY
Main.py
from settings import *
from sprites import *
import pygame
import random
class Game:
def __init__(self):
# initialize game window, etc
pygame.init()
pygame.mixer.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(TITLE)
self.clock = pygame.time.Clock()
self.running = True
def new(self):
# start a new game
self.snake = Snake()
self.run()
def run(self):
# Game Loop
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.draw()
self.animate()
self.update()
def update(self):
# Game Loop - Update
pygame.display.update()
def events(self):
# Game Loop - events
for event in pygame.event.get():
# check for closing window
if event.type == pygame.QUIT:
if self.playing:
self.playing = False
self.running = False
def draw(self):
# Game Loop - draw
self.screen.fill((255, 255, 255))
self.snake.draw(self.screen)
def animate(self):
self.snake.animate()
game = Game()
while game.running:
game.new()
pygame.quit()
遵循 Python 禅 显式优于隐式。
Often, the first argument of a method is called self. This is nothing more than a convention: the name self has absolutely no special meaning to Python. Note, however, that by not following the convention your code may be less readable to other Python programmers, and it is also conceivable that a class browser program might be written that relies upon such a convention.
这意味着你应该将第一个参数设置为self
,而且你忘记了一个comma
,代码应该是这样的:
def draw(self,screen):
pygame.draw.rect(screen, self.color,(self.x, self.y, self.w, self.h))
查看此问题Why always add self as first argument to class methods? and Python doc 了解更多详细信息。