Python TypeError: draw() takes exactly 1 argument (2 given)
Python TypeError: draw() takes exactly 1 argument (2 given)
我试图让图像出现在 pygame 屏幕上,但我不断收到错误消息:TypeError: draw() takes exactly 1 argument (2 given) when I 运行 第二个文件。
代码如下:
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):
:
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))
我试图让图像出现在 pygame 屏幕上,但我不断收到错误消息:TypeError: draw() takes exactly 1 argument (2 given) when I 运行 第二个文件。
代码如下:
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):
:
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))