Js贪吃蛇代码:Python贪吃蛇游戏数学

我正在使用 python 3 并遵循一个 [tutorial] [1]。我得到一个问题,我的蛇不动。它在 vec_add () 上调用无效语法,当我删除一些括号时,我得到:

Traceback (most recent call last):
  File "_ctypes/callbacks.c", line 234, in 'calling callback function'
  File "C:\Python34\lib\site-packages\OpenGL\GLUT\special.py", line 164, in deregister
    function( value )
  File "C:/Users/jay/Desktop/Python/OpenGL/Snake Game.py", line 51, in update
    snake.insert(0, vec_add(snake[0], snake_dir))      # insert new position in the beginning of the snake list
TypeError: vec_add() missing 2 required positional arguments: 'x2' and 'y2'

蛇应该向右移动。

from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
window = 0                                             # glut window number
width, height = 500, 500                               # window size
field_width, field_height = 50, 50                     # internal resolution
snake = [(20, 20)]                                       # snake list of (x, u) positions
snake_dir = (1, 0)                                     # snake movement direction
#Note: snake dir (1, 0) means that its current movement
#direction is x=1 and y=0, which means it moves to the right.
interval = 200 # update interval in milliseconds
def vec_add((x1, y1), (x2, y2)):    
    return (x1 + x2, y1 + y2)
def refresh2d_custom(width, height, internal_width, internal_height):
    glViewport(0, 0, width, height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0.0, internal_width, 0.0, internal_height, 0.0, 1.0)
    glMatrixMode (GL_MODELVIEW)
    glLoadIdentity()
def draw_rect(x, y, width, height):
    glBegin(GL_QUADS)                                  # start drawing a rectangle
    glVertex2f(x, y)                                   # bottom left point
    glVertex2f(x + width, y)                           # bottom right point
    glVertex2f(x + width, y + height)                  # top right point
    glVertex2f(x, y + height)                          # top left point
    glEnd()                                            # done drawing a rectangle
def draw_snake():
    glColor3f(1.0, 1.0, 1.0)  # set color to white
    for x, y in snake:        # go through each (x, y) entry
        draw_rect(x, y, 1, 1) # draw it at (x, y) with width=1 and height=1
def draw():                                            # draw is called all the time
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen
    glLoadIdentity()                                   # reset position
    refresh2d_custom(width, height, field_width, field_height)
    draw_snake()
    glutSwapBuffers()                                  # important for double buffering
def update(value):
    snake.insert(0, vec_add(snake[0], snake_dir))      # insert new position in the beginning of the snake list
    snake.pop()                                        # remove the last element
    glutTimerFunc(interval, update, 0)                 # trigger next update
# initialization
glutInit()                                             # initialize glut
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(width, height)                      # set window size
glutInitWindowPosition(0, 0)                           # set window position
window = glutCreateWindow(b"noobtuts.com")              # create window with title
glutDisplayFunc(draw)                                  # set draw function callback
glutIdleFunc(draw)                                     # draw all the time
glutTimerFunc(interval, update, 0)                     # trigger next update
glutMainLoop()                                         # start everything
0

在此函数中:

def vec_add((x1, y1), (x2, y2)):    
    return (x1 + x2, y1 + y2)

Python 不支持在函数参数列表中解构。所以你可以这样写:

def vec_add(p1, p2):    
    return (p1[0] + p2[0], p1[1] + p2[1])

这与您对vec_add的现有调用兼容。

0

可以使用普通元组来存储 & amp;操作坐标,但是您可能会发现命名元组是一种更方便的数据结构。命名元组的元素可以按名称或索引访问,并且由于命名元组是一个类,您可以向其添加自定义方法。

这是一个简短的演示,它将向量加法和减法方法添加到一个名为 tuple 的 2 元素中,它还添加了一个有用的__str__方法。

#!/usr/bin/env python
from collections import namedtuple
Vector = namedtuple('Vector', ['x', 'y'])
Vector.__str__ = lambda self: 'Vector({s.x}, {s.y})'.format(s=self)
Vector.__add__ = lambda self, other: Vector(self.x + other.x, self.y + other.y)
Vector.__sub__ = lambda self, other: Vector(self.x - other.x, self.y - other.y)
snake = [Vector(20, 20)]
snake_dir = Vector(x=1, y=0)
print(snake_dir.x)
print(snake[0] + snake_dir)
output
1
Vector(21, 20)

有关更多信息,请参阅namedtuple的 Python 文档;这里是这些文档的Python 3 version

您还可以从头开始定义自己的 Vector 或 Point 类,如this answer。如果要存储大量点,则可能希望将__slots__ = ()添加到类定义中以最大程度地减少内存使用,如 Python 2 命名的元组文档中所述。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表边看边学立场,如若转载,请注明出处

(275)
Python生成器和迭代器理解:Python3:不是生成器的迭代器
上一篇
大佬caa是谁:你是谁你是谁 是谁(app para sumar horas y minutos)
下一篇

相关推荐

  • python界面开发构建一个简单、可靠的用户界面

    Python界面开发是指使用Python语言来创建图形用户界面(GUI)的过程。它可以帮助你创建可视化的应用程序,使用户能够与你的程序交互。…

    2023-03-18 10:39:36
    0 74 23
  • python 创建列表:使用Python创建一个强大的列表

    示例示例Python创建列表的方法有多种,下面介绍其中几种常用的方法。使用 [] 创建空列表…

    2023-03-03 12:56:32
    0 39 92
  • python的列表排序轻松实现有序数据

    示例示例Python列表排序是指对列表中的元素进行排序。Python内置的sorted()函数可以对list进行排序:示例代码:…

    2023-03-03 12:45:31
    0 95 91
  • python中的变量名:如何使用Python中的变量名

    Python中的变量名是用户定义的标识符,用于指代某个值。变量名可以由字母、数字、下划线组成,但不能以数字开头,也不能使用关键字。…

    2023-02-15 14:41:46
    0 38 60
  • python菜鸟教程:Python 基础教程

    Python菜鸟教程是一个免费的Python学习网站,旨在帮助Python初学者快速入门。它提供了大量的Python教程,包括基础语法、控制流、函数、模块、文件I/O、错误处理、类、正则表达式、GUI编程、网络编程、CGI编程等等。…

    2023-02-23 05:58:40
    0 33 49
  • python setdefault函数 None}

    示例示例Python 函数用于在字典中查找指定键,如果该键不存在,则将其设置为指定的默认值。语法:…

    2023-01-20 14:28:09
    0 29 50
  • python 闭包函数:如何利用Python闭包函数实现更复杂的功能

    Python闭包函数是一种特殊的函数,它可以访问其他函数作用域中的变量。闭包函数有助于将函数与其上下文环境中的变量绑定在一起,因此可以在函数外部使用这些变量。…

    2023-02-15 10:32:25
    0 66 40
  • python屏幕抓取挖掘数据的新方法

    Python屏幕抓取是一种使用Python编写的程序,用于从计算机屏幕上抓取图像、文字或其他数据。它可以用来从游戏中抓取图像,从网页中抓取文本,从文档中抓取图像,或者从视频中抓取帧。…

    2023-01-19 10:51:32
    0 99 95

发表评论

登录 后才能评论

评论列表(53条)