Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python代码中的蛇游戏_Python_Graphics - Fatal编程技术网

Python代码中的蛇游戏

Python代码中的蛇游戏,python,graphics,Python,Graphics,我正在为一个简单的蛇游戏编写代码,在这个游戏中,玩家控制蛇的头部(我使用图形功能创建的一个盒子)来吃“苹果”(另一个盒子)。我想出了如何在while循环中使用checkKey方法来移动长方体。我还发现了如何检测蛇和苹果之间的碰撞。当蛇与苹果相撞时,我不知道如何在蛇的末端添加一个身体片段(另一个盒子)。我也不知道如何以与蛇头(用户控制的蛇头部分)相同的模式移动身体部分。有什么建议吗?请查看此代码。。这也是蛇的游戏。它会给你你想要的一切 import curses from curses impor

我正在为一个简单的蛇游戏编写代码,在这个游戏中,玩家控制蛇的头部(我使用图形功能创建的一个盒子)来吃“苹果”(另一个盒子)。我想出了如何在while循环中使用checkKey方法来移动长方体。我还发现了如何检测蛇和苹果之间的碰撞。当蛇与苹果相撞时,我不知道如何在蛇的末端添加一个身体片段(另一个盒子)。我也不知道如何以与蛇头(用户控制的蛇头部分)相同的模式移动身体部分。有什么建议吗?

请查看此代码。。这也是蛇的游戏。它会给你你想要的一切

import curses
from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
from random import randint


curses.initscr()
win = curses.newwin(20, 60, 0, 0)
win.keypad(1)
curses.noecho()
curses.curs_set(0)
win.border(0)
win.nodelay(1)

key = KEY_RIGHT                                                    # Initializing values
score = 0

snake = [[4,10], [4,9], [4,8]]                                     # Initial snake co-ordinates
food = [10,20]                                                     # First food co-ordinates

win.addch(food[0], food[1], '*')                                   # Prints the food

while key != 27:                                                   # While Esc key is not pressed
    win.border(0)
    win.addstr(0, 2, 'Score : ' + str(score) + ' ')                # Printing 'Score' and
    win.addstr(0, 27, ' SNAKE ')                                   # 'SNAKE' strings
    win.timeout(150 - (len(snake)/5 + len(snake)/10)%120)          # Increases the speed of Snake as its length increases

    prevKey = key                                                  # Previous key pressed
    event = win.getch()
    key = key if event == -1 else event 


    if key == ord(' '):                                            # If SPACE BAR is pressed, wait for another
        key = -1                                                   # one (Pause/Resume)
        while key != ord(' '):
            key = win.getch()
        key = prevKey
        continue

    if key not in [KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, 27]:     # If an invalid key is pressed
        key = prevKey

    # Calculates the new coordinates of the head of the snake. NOTE: len(snake) increases.
    # This is taken care of later at [1].
    snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])

    # If snake crosses the boundaries, make it enter from the other side
    if snake[0][0] == 0: snake[0][0] = 18
    if snake[0][1] == 0: snake[0][1] = 58
    if snake[0][0] == 19: snake[0][0] = 1
    if snake[0][1] == 59: snake[0][1] = 1

    # Exit if snake crosses the boundaries (Uncomment to enable)
    #if snake[0][0] == 0 or snake[0][0] == 19 or snake[0][1] == 0 or snake[0][1] == 59: break

    # If snake runs over itself
    if snake[0] in snake[1:]: break


    if snake[0] == food:                                            # When snake eats the food
        food = []
        score += 1
        while food == []:
            food = [randint(1, 18), randint(1, 58)]                 # Calculating next food's coordinates
            if food in snake: food = []
        win.addch(food[0], food[1], '*')
    else:    
        last = snake.pop()                                          # [1] If it does not eat the food, length decreases
        win.addch(last[0], last[1], ' ')
    win.addch(snake[0][0], snake[0][1], '#')

curses.endwin()
print("\nScore - " + str(score))

你用什么工具来构建它?这是非常广泛的。请阅读帮助中心,了解如何提出一个好问题的技巧。我认为作者是在寻找说明,而不仅仅是他们可能不理解的一段代码。如果你要复制/粘贴代码,至少要有礼貌地承认它的价值。