Python 我如何更改“文件”的大小;蠕虫“;在我的游戏中?
可能重复:Python 我如何更改“文件”的大小;蠕虫“;在我的游戏中?,python,pygame,Python,Pygame,可能重复: 我正在使用python和pygame: 如何更改游戏中“蠕虫”的宽度?到目前为止,蛇只有1个像素宽。我如何将宽度更改为3或4个像素宽 class Worm: def __init__(self, surface): self.surface = surface self.x = surface.get_width()/2 self.y = surface.get_height()/2 self.length =
我正在使用python和pygame: 如何更改游戏中“蠕虫”的宽度?到目前为止,蛇只有1个像素宽。我如何将宽度更改为3或4个像素宽
class Worm:
def __init__(self, surface):
self.surface = surface
self.x = surface.get_width()/2
self.y = surface.get_height()/2
self.length = 5
self.grow_to = 50
self.vx = 0
self.vy = -1
self.body = []
self.crashed = False
self.color = (255, 255, 0)
def move(self):
"""Move the worm"""
self.x += self.vx
self.y += self.vy
if (self.x, self.y) in self.body:
self.crashed = True
self.body.insert(0, (self.x, self.y))
if (self.grow_to > self.length):
self.length += 1
if len(self.body) > self.length:
self.body.pop()
def draw(self):
for x, y in self.body:
self.surface.set_at((x, y), self.color)
def position (self):
return self.x, self.y
def eat(self):
self.grow_to += 25
新代码-但这会导致蛇继续生长
class Worm:
def __init__(self, surface):
self.surface = surface
self.x = surface.get_width()/2
self.y = surface.get_height()/2
self.length = 5
self.grow_to = 50
self.vx = 0
self.vy = -1
self.body = []
self.crashed = False
self.color = (255, 255, 0)
def move(self):
"""Move the worm"""
self.x += self.vx
self.y += self.vy
if (self.x, self.y) in self.body:
self.crashed = True
self.body.insert(0, (self.x, self.y))
self.body.insert(0, (self.x, self.y+1))
self.body.insert(0, (self.x, self.y-1))
self.body.insert(0, (self.x+1, self.y))
self.body.insert(0, (self.x-1, self.y))
if (self.grow_to > self.length):
self.length += 1
if len(self.body) > self.length:
self.body.pop()
def draw(self):
for x, y in self.body:
self.surface.set_at((x, y), self.color)
for x, y in self.body:
self.surface.set_at((x, y), self.color)
def position (self):
return self.x, self.y
return self.x, self.y+1
return self.x, self.y-1
return self.x+1, self.y
return self.x-1, self.y
def eat(self):
self.grow_to += 25
Surface.set_at
用于放置彩色像素,但是通过操纵x
值或y
值,您应该能够通过循环运行该代码并将其打印多次
编辑:对不起,代码应该添加到Y值,使蛇“更厚”
这是伪代码:
#Snake
for snakeLength in range(3):
if Snake is facing right:
self.surface.set_at((x, y+snakeLength), self.color)
这是我的想法,但这是未经测试的抱歉。我通常用雪碧
编辑:,“一次获取和设置一个像素通常太慢,无法用于游戏或实时情况。”用
self.body.insert(0, (self.x, self.y))
self.body.insert(0, (self.x, self.y+1))
self.body.insert(0, (self.x, self.y-1))
self.body.insert(0, (self.x+1, self.y))
self.body.insert(0, (self.x-1, self.y))
... # corner cases if they are important
draw()将自动接受主体中的新像素。不错的游戏,我不久前在诺基亚PyS60上破解了它。你怎么画蛇?我想是Pygame,但是你使用的是
Pygame.draw.rect(self.image,color,[xpos,ypos,width,height])
..没有im使用self.surface.set_at((x,y),self.color)
surface.set_
是为放置彩色像素而设计的,因此根据蛇面对的方向,运行surface.set_
两到三次,每次在x值上增加1个像素。现在,当我提出你的建议时,蛇的移动速度比它删除“尾巴”的速度快,因此它会继续增长。你是对的,当在x中移动时,它实际上应该只在y方向上增加,反之亦然。就像下面的伪代码一样,程序应检查方向,并仅在另一个方向添加像素,以避免增加蛇的长度。我不知道如何设置注释的格式,因此需要修复下面的空白:-(如果self.vy!=0:self.body.insert(0,(self.x+1,self.y))self.body.insert(0,(self.x-1,self.y))如果self.vx!=0:self.body.insert(0,(self.x,self.y+1))self.body.insert(0,(self.x,self.y-1))