'float' object cannot be interpreted as an integer的意思是:float类型不能解释为int类型 。
代码错误处应该发生在图中红框内的代码语句中。
因为使用的是Python3所以在所框语句中应该使用//去代替/。
扩展资料:
Python贪吃蛇代码:
import pygame,sys,random,time
from pygame.locals import * #从pygame模块导入常用的函数和常量
#定义颜色变量
black_colour = pygame.Color(0,0,0)
white_colour = pygame.Color(255,255,255)
red_colour = pygame.Color(255,0,0)
grey_colour = pygame.Color(150,150,150)
#定义游戏结束函数
def GameOver(gamesurface):
#设置提示字体的格式
GameOver_font = pygame.font.SysFont("MicrosoftYaHei", 16)
#设置提示字体的颜色
GameOver_colour = GameOver_font.render('Game Over',True,grey_colour)
#设置提示位置
GameOver_location = GameOver_colour.get_rect()
GameOver_location.midtop = (320,10)
#绑定以上设置到句柄
gamesurface.blit(GameOver_colour,GameOver_location)
#提示运行信息
pygame.display.flip()
#休眠5秒
time.sleep(5)
#退出游戏
pygame.quit()
#退出程序
sys.exit()
#定义主函数
def main():
#初始化pygame,为使用硬件做准备
pygame.init()
pygame.time.Clock()
ftpsClock = pygame.time.Clock()
#创建一个窗口
gamesurface = pygame.display.set_mode((640,480))
#设置窗口的标题
pygame.display.set_caption('tanchishe snake')
#初始化变量
#初始化贪吃蛇的起始位置
snakeposition = [100,100]
#初始化贪吃蛇的长度
snakelength = [[100,100],[80,100],[60,100]]
#初始化目标方块的位置
square_purpose = [300,300]
#初始化一个数来判断目标方块是否存在
square_position = 1
#初始化方向,用来使贪吃蛇移动
derection = "right"
change_derection = derection
#进行游戏主循环
while True:
#检测按键等pygame事件
for event in pygame.event.get():
if event.type==QUIT:
#接收到退出事件后,退出程序
pygame.quit()
sys.exit()
elif event.type==KEYDOWN:
#判断键盘事件,用w,s,a,d来表示上下左右
if event.key==K_RIGHT or event.key==ord('d'):
change_derection = "right"
if event.key==K_LEFT or event.key==ord('a'):
change_derection = "left"
if event.key==K_UP or event.key==ord('w'):
change_derection = "up"
if event.key==K_DOWN or event.key==ord('s'):
change_derection = "down"
if event.key==K_ESCAPE:
pygame.event.post(pygame.event.Event(QUIT))
#判断移动的方向是否相反
if change_derection =='left'and not derection =='right':
derection = change_derection
if change_derection =='right'and not derection =='left':
derection = change_derection
if change_derection == 'up' and not derection =='down':
derection = change_derection
if change_derection == 'down' and not derection == 'up':
derection = change_derection
#根据方向,改变坐标
if derection == 'left':
snakeposition[0] -= 20
if derection == 'right':
snakeposition[0] += 20
if derection == 'up':
snakeposition[1] -= 20
if derection == 'down':
snakeposition[1] += 20
#增加蛇的长度
snakelength.insert(0,list(snakeposition))
#判断是否吃掉目标方块
if snakeposition[0]==square_purpose[0] and snakeposition[1]==square_purpose[1]:
square_position = 0
else:
snakelength.pop()
#重新生成目标方块
if square_position ==0:
#随机生成x,y,扩大二十倍,在窗口范围内
x = random.randrange(1,32)
y = random.randrange(1,24)
square_purpose = [int(x*20),int(y*20)]
square_position = 1
#绘制pygame显示层
gamesurface.fill(black_colour)
for position in snakelength:
pygame.draw.rect(gamesurface,white_colour,Rect(position[0],position[1],20,20))
pygame.draw.rect(gamesurface,red_colour,Rect(square_purpose[0],square_purpose[1],20,20))
#刷新pygame显示层
pygame.display.flip()
#判断是否死亡
if snakeposition[0]<0 or snakeposition[0]>620:
GameOver(gamesurface)
if snakeposition[1]<0 or snakeposition[1]>460:
GameOver(gamesurface)
for snakebody in snakelength[1:]:
if snakeposition[0]==snakebody[0] and snakeposition[1]==snakebody[1]:
GameOver(gamesurface)
#控制游戏速度
ftpsClock.tick(5)
if __name__ == "__main__":
main()
1、先到python的官方网站下载软件,打开官网后,选择downlad项目,然后选择需要下载的大版本,2.7还是3.4。2、选择完版本后,进入后一个页面,在这个页面可以选择操作系统及对应的版本,win下注意分64位和32位版本,不要下错了。
3、32位的程序大约为17.5M,建议非官网下载的还是要比对下MD5以防万一,在上一个页面就有MD5值。核对完成后双击安装,首先是询问是为所有用户还是为当前用户安装,用缺省的就行。
4、下一步后是选择安装目录,这个看个人喜好了,不过还是要记一下目录,因为缺省的代码也会放在这个目录下。
5、下面继续缺省配置选择就开始安装了,python在安装过程中要执行一些角本,因些需要至少有system的权限才可以安装。
6、安装完成,但在win10在任务栏和桌面上都未生成图标,使用起来麻烦些,在开始窗口打开所有应,按字母找到python程序项,右键选择固定在任务栏或屏幕。
1.在Ubuntu14.04 LTS版本中,已经自行安装了python,可以在Terminal(CTRL+ALT+T)中输入:ls /usr/bin | grep python 进行查看。如果想运行python2.7的话,直接在终端输入:python即可。如果想运行python3.4的话,直接在终端输入:python3即可。
2.Ubuntu14.04 LTS中的python是没有自带IDLE的,可以在终端输入:sudo apt-get install idle-python3.4,进行python3.4版本的IDLE的安装,安装好之后直接在/usr/share/applications,就可以找到IDLE的图标,直接将其复制到桌面上,以后直接在桌面双击就可以启动。
或者在终端输入:/usr/bin/idle-python3.4即可启动。