我怎样才能改变“蠕虫”的大小?在我的游戏中?
可能的重复:
我如何更改宽度我的“贪吃蛇游戏”中的蛇是什么?
我使用 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
Possible Duplicate:
How would i change the width of the snake in my “snake game”?
Im using python and pygame:
How can i change the width of the "worm" in my game? So far the snake is only 1 pixel wide how can i change the width to be 3 or 4 pixels wide?
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
NEW CODE____________________________ But this causes the snake to grow continuesly
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用draw()将其变厚
会自动吸收主体中的新像素。不错的游戏,我前段时间在诺基亚 PyS60 上破解了它。
Grow it thicker with
draw() will automatically take in the new pixels in body. Nice game, I hacked it on Nokia PyS60 some time ago.
Surface.set_at 用于放置彩色像素,但是通过操纵 x 值或 y 值,您应该能够通过以下方式运行该代码:一个循环并让它打印出来多次。
编辑:抱歉,代码应该添加到 Y 值以使蛇“更粗”...
这是伪代码:
这是我这样做的想法,但是抱歉,未经测试。我通常使用精灵。
编辑: Pygame 文档,“一次获取和设置一个像素通常太慢,无法在游戏或实时情况下使用。”
Surface.set_at
is for placing colored pixels, however by manipulating thex
value, ory
value, you should be able to run that code through a loop and get it to print out multiple times.Edit: Sorry, the code should add to the Y value to make the snake 'thicker'...
This is pseudo-code:
That's my idea of doing that, but it's untested sorry. I use sprites usually.
Edit: Pygame Docs, "Getting and setting pixels one at a time is generally too slow to be used in a game or realtime situation. "