我怎样才能改变“蠕虫”的大小?在我的游戏中?

发布于 2024-12-11 09:17:33 字数 2612 浏览 0 评论 0原文

可能的重复:
我如何更改宽度我的“贪吃蛇游戏”中的蛇是什么?

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

短叹 2024-12-18 09:17:33

使用draw()将其变厚

 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

会自动吸收主体中的新像素。不错的游戏,我前段时间在诺基亚 PyS60 上破解了它。

Grow it thicker with

 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() will automatically take in the new pixels in body. Nice game, I hacked it on Nokia PyS60 some time ago.

歌枕肩 2024-12-18 09:17:33

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) 

这是我这样做的想法,但是抱歉,未经测试。我通常使用精灵。

编辑: Pygame 文档,“一次获取和设置一个像素通常太慢,无法在游戏或实时情况下使用。

Surface.set_at is for placing colored pixels, however by manipulating the x value, or y 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:

#Snake
for snakeLength in range(3):
    if Snake is facing right:
        self.surface.set_at((x, y+snakeLength), self.color) 

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. "

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文