如何解决pygame中的set_mode询问位置参数,我不知道该怎么办
我正在尝试做Snake来锻炼,并在运行游戏时遇到了这个问题:
TypeError: draw() missing 1 required positional argument: 'surface'
我试图修复它很多次,这很困难,因为我正在关注一个教程,视频中的那个人也没有这样的问题。
这是完整的代码,尚未完成,如果我在语法上有任何错误,也很抱歉,我不是母语人士。
import pygame
def main():
global rows,width,s
width = 500
rows = 20
win = pygame.display.set_mode(size=(width,width))
s = snake((0,255,0),(10,10))
flag = True
clock = pygame.time.Clock()
while flag:
pygame.time.delay(50)
clock.tick(10)
s.move()
redrawWindow(win)
#Serpente-------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
class snake(object):
body = []
turns = {}
def __init__(self,color,pos):
self.color = color
self.head = cube(pos)
self.body.append(self.head)
self.dirnx = 0
self.dirny = 1
def move(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
keys = pygame.key.get_pressed()
for key in keys:
if keys[pygame.K_LEFT]:
self.dirnx = -1
self.dirny = 0
self.turns[self.head.pos[:]] = [self.dirnx,self.dirny]
elif keys[pygame.K_RIGHT]:
self.dirnx = 1
self.dirny = 0
self.turns[self.head.pos[:]] = [self.dirnx,self.dirny]
elif keys[pygame.K_UP]:
self.dirnx = 0
self.dirny = -1
self.turns[self.head.pos[:]] = [self.dirnx,self.dirny]
elif keys[pygame.K_DOWN]:
self.dirnx = 0
self.dirny = 1
self.turns[self.head.pos[:]] = [self.dirnx,self.dirny]
for i,c in enumerate(self.body):
p = c.pos[:]
if p in self.turns:
turn = self.turns[p]
c.move(turn[0],turn[1])
if i == len(self.body)-1:
self.turns.pop(p)
else:
if c.dirnx == -1 and c.pos[0] <= 0:
c.pos = (c.rows-1,c.pos[1])
elif c.dirnx == 1 and c.pos[0] >= c.rows-1:
c.pos = (0,c.pos[1])
elif c.dirny == 1 and c.pos[1] >= c.rows-1:
c.pos = (c.pos[0],0)
elif c.dirny == -1 and c.pos[1] <= 0:
c.pos = (c.pos[0],c.rows-1)
else:
c.move(c.dirnx,c.dirny)
def reset(self,pos):
pass
def addCube(self):
pass
def draw(self,surface):
for i,c in enumerate(self.body):
if i == 0:
c.draw(surface,True)
else:
c.draw(surface)
class cube(object):
rows = 20
w = 500
def __init__(self,start,dirnx=1,dirny=0,color=(0,0)):
self.pos = start
self.dirnx = 1
self.dirny = 0
self.color = color
def move(self,dirnx,dirny):
self.dirnx = dirnx
self.dirny = dirny
self.pos = (self.pos[0] + self.dirnx,self.pos[1] + self.dirny)
def draw(self,surface,eyes=False):
dis = self.w // self.rows
i = self.pos[0]
j = self.pos[1]
pygame.draw.rect(surface,self.color,(i*dis+j,j*dis+1,dis-2,dis-2))
#Resto----------------------------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
def redrawWindow(surface):
global rows,s
surface.fill((0,0))
s.draw()
drawGrid(width,rows,surface)
pygame.display.update()
def drawGrid(w,surface):
sizeBtwn = w // rows
x = 0
y = 0
for i in range(rows):
x = x + sizeBtwn
y = y + sizeBtwn
pygame.draw.line(surface,(20,20,20),(x,w))
pygame.draw.line(surface,(0,y),(w,y))
main()```
Also sorry if I did any grammatical mistakes,I'm not a native speaker.
解决方法
该问题可能与您在程序开始时未导入pygame有关。只需尝试在第一行中添加import pygame
。如果仍然出现相同的错误,则可以查看如何download pygame。
该方法需要表面(屏幕)参数。
尝试:
def redrawWindow(surface):
global rows,width,s
surface.fill((0,0))
s.draw(surface) # <<< pass surface to method
drawGrid(width,rows,surface)
pygame.display.update()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。