Python TypeError:draw()正好接受1个参数(给定2个)
我试图在pygame屏幕上显示一个图像,但我不断得到错误:当我运行第二个文件时,TypeError:draw()正好取1个参数(给定2个) 代码如下:Python TypeError:draw()正好接受1个参数(给定2个),python,pygame,typeerror,Python,Pygame,Typeerror,我试图在pygame屏幕上显示一个图像,但我不断得到错误:当我运行第二个文件时,TypeError:draw()正好取1个参数(给定2个) 代码如下: import pygame from pygame.locals import * # Create a class called Person class Person: def __init__(self, newX, newY): ###defines paramaters### self.x =
import pygame
from pygame.locals import *
# Create a class called Person
class Person:
def __init__(self, newX, newY): ###defines paramaters###
self.x = newX
self.y = newY
def draw(window): ###loads and draws the image###
img = pygame.image.load("dude.gif")
window.blit(img, (self.x, self.y))
def moveLeft(self):
# Change x so that the object can move left
pass
def moveRight(self):
# Change x so that the object can move right
pass
def moveUp(self):
# Change y so that the object can move up
pass
def moveDown(self):
# Change y so that the object can move down
pass
这是第二个文件:
import pygame, sys
from pygame.locals import *
from Person import *
# Creates the screen to draw on
pygame.init()
screen = pygame.display.set_mode((800,600))
# Allows a key that is held down to count as multiple presses
pygame.key.set_repeat(100,100)
# Creates a Person object named guy
guy = Person(50,50)
# Event Loop
while True:
# Colors the screen white
screen.fill((255,255,255))
# Draw your person on the screen
guy.draw(screen)
# Update the screen
pygame.display.update()
# Check for key presses and mouse clicks
for event in pygame.event.get():
# Determine if the user has closed the window or pressed escape
if event.type==QUIT or (event.type==KEYUP and event.key==K_ESCAPE):
# Quit the program
pygame.quit()
sys.exit()
# Check if an arrow key is pressed and
# move guy in the correct direction
elif event.type==KEYDOWN:
if event.key==K_UP:
guy.moveUp()
elif event.key==K_DOWN:
guy.moveDown()
elif event.key==K_LEFT:
guy.moveLeft()
elif event.key==K_RIGHT:
guy.moveRight()
您的
draw
方法应定义为def draw(self,window):
:
方法签名中缺少
self
参数def draw(self,window):
,而不是def draw(window):
@JoshuaTaylor,谢谢——有很多非常类似的问题,但我很难找到一个完全正确的问题。@CharlesDuffy我刚刚用谷歌搜索了“正好取了1个参数(给定2个)”缺少self
。这个问题是第一个击中目标的问题,我用作欺骗目标的问题是第二个。(哇,谷歌更新内容太快了。)谢谢。我讨厌那种自我的东西那种自我的东西不值得讨厌。是我,也是你。它是面向对象编程的精髓,是一个对象和它的能力之间的参照。我嘴里说的是什么???点点头。“显明胜于含蓄”——很像Python的禅意。如果你对我的答案感到满意,那么请考虑点击我的回答,点击嘀嗒记号。
def draw(self, window):
img = pygame.image.load("dude.gif")
# and after all, you're using self here
window.blit(img, (self.x, self.y))