Python简单游戏代码

Python简单游戏代码本人新人一枚 第一次在 CSDN 上写博客 代码不难 主要是混个积分 代码如下 importpygame sys randomfrompy localsimport 定义颜色变量目标方块的颜色 redColor pygame Color 250 0 0 贪吃蛇的颜色 whiteColor pygame Color 255 255 255 背景颜色 b

本人新人一枚,第一次在CSDN上写博客,代码不难,主要是混个积分。

代码 如下

import pygame,sys,random from pygame.locals import * #定义颜色变量 目标方块的颜色 redColor = pygame.Color(250,0,0) #贪吃蛇的颜色 whiteColor = pygame.Color(255,255,255) #背景颜色 blackColor = pygame.Color(0,0,0) def gameOver(): pygame.quit() sys.exit() def main(): #初始化pygame pygame.init() #控制游戏速度 fpsColck = pygame.time.Clock() #创建pygame显示层 playSurface = pygame.display.set_mode((640,480)) pygame.display.set_caption('贪吃蛇') #初始化贪吃蛇的起始坐标 snakePosition = [100,100] #初始化贪吃蛇的长度 snakeBody = [[100,100],[80,100],[60,100]] #初始化目标方块的坐标 targetPosition = [300,300] #初始化一个目标方块的标记 目的:用来判断是否吃掉这个目标方块 targerflag = 1 #初始化方向 direction = 'right' #定义一个方向变量 changeDirection = direction while True: for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() elif event.type == KEYDOWN: if event.key == K_RIGHT: changeDirection = 'right' if event.key == K_LEFT: changeDirection = 'left' if event.key == K_UP: changeDirection = 'up' if event.key == K_DOWN: changeDirection = 'down' #对应键盘Esc键 if event.key == K_ESCAPE: pygame.event.post(pygame.event.Event(QUIT)) #确定方向 if changeDirection == 'left' and not direction == 'right': direction = changeDirection if changeDirection == 'right' and not direction == 'left': direction = changeDirection if changeDirection == 'up' and not direction == 'down': direction = changeDirection if changeDirection == 'down' and not direction == 'up': direction = changeDirection #根据方向移动蛇头的坐标 if direction == 'right': snakePosition[0] += 20 if direction == 'left': snakePosition[0] -= 20 if direction == 'up': snakePosition[1] -= 20 if direction == 'down': snakePosition[1] += 20 #增加蛇的长度 snakeBody.insert(0,list(snakePosition)) #判断目标方块是否被吃掉 if snakePosition[0] == targetPosition[0] and snakePosition[1] == targetPosition[1]: targerflag = 0 else: snakeBody.pop() if targerflag == 0: x = random.randrange(1,32) y = random.randrange(1,24) targetPosition = [int(x*20),int(y*20)] targerflag = 1 #绘制pygame的显示层 playSurface.fill(blackColor) for position in snakeBody: pygame.draw.rect(playSurface,whiteColor,Rect(position[0],position[1],20,20)) pygame.draw.rect(playSurface,redColor,Rect(targetPosition[0],targetPosition[1],20,20)) pygame.display.flip() if snakePosition[0]> 620 or snakePosition[0] < 0: gameOver() elif snakePosition[1] > 460 or snakePosition[1] < 0: gameOver() fpsColck.tick(50) if __name__ == '__main__': main()

 

#初始化贪吃蛇的起始坐标 snakePosition = [100,100]

#初始化贪吃蛇的长度 snakeBody = [[100,100],[80,100],[60,100]]

#初始化目标方块的坐标 targetPosition = [300,300]

这三段代码可以进行修改参数,修改蛇的起始坐标,长度以及目标方块坐标。

才学3个礼拜的python,写着玩一下。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/232071.html原文链接:https://javaforall.net

(0)
上一篇 2025年11月3日 下午5:01
下一篇 2025年11月3日 下午5:22


相关推荐

  • mysql中explain的用法_mysql substr用法

    mysql中explain的用法_mysql substr用法基于Mysql5.7版本的explain参数详解…Mysql官网相关参数解读一:idSELECT标识符1.id越大越先执行2.相同id,从从往下执行二:select_type1.SIMPLE:最简单的查询(没有关联查询没有子查询没有union的查询语句)2:PRIMARY:子查询最外层的查询语句3.SUBQUERY:子查询内层查询语句4.DERIVED:派生表查询,FROM后的不是表而是查询后的结果集5.UNION:union或unionall中的第二个以后的查询表6.U

    2022年8月31日
    11
  • Log4j中conversionPattern的含义「建议收藏」

    Log4j中conversionPattern的含义「建议收藏」%a–表示礼拜几,英文缩写形式,比如“Fri”%A–表示礼拜几,比如“Friday”%b–表示几月份,英文缩写形式,比如“Oct”%B–表示几月份,“October”%c–

    2022年7月2日
    29
  • 三角形面积的计算公式小学_正三角形面积公式

    三角形面积的计算公式小学_正三角形面积公式1、三角形面积=1/2*底*高(三边都可做底)2、三角形面积=1/2absinC=1/2acsinB=1/2bcsinA3、三角形面积=abc/4R(其中R是三角形外接圆半径)你看看理解一下,其中1是比较常用的。4、三角形面积S=√x*(x-a)*(x-b)*(x-c)其中"√"是大根号,”x”为三角形周长的一半,a,b,c为边长三角形的面积的平方=p(p-a)(p-b)(p-

    2025年6月3日
    4
  • Linux查看系统版本和内核版本[通俗易懂]

    Linux查看系统版本和内核版本[通俗易懂]1.查看Linux系统版本cat/etc/issue或者cat/etc/redhat-release示例:[root@localhostgrafana]#cat/etc/issueCentOSrelease6.5(Final)Kernel\ronan\m2.查看Linu

    2022年10月9日
    6
  • 【技术类】不得不看的Landsat 8常用的波段组合形式

    【技术类】不得不看的Landsat 8常用的波段组合形式Landsat8hasbeenonlineforacoupleofmonthsnow,andtheimageslookincredible.WhileallofthebandsfrompreviousLandsatmissionsarestillincorporated,thereareacoupleofnewones,such

    2022年7月23日
    11
  • 深入javascript之原型和原型链

    深入javascript之原型和原型链原型和原型链是 js 中的难点也是重点 明白了原型和原型链会让我们在后面不管是学习还是工作都会更加高效 并且原型和原型链会是面试中必不可少的话题 看完此篇文章一定会让你对原型 原型链有深刻全面的了解 深入系列 深入 javascript 之作用域深入系列 深入 javascript 之执行上下文 nbsp 一 函数对象 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp 所有引用类型 函数 数组 对象 都拥有 prot

    2026年3月20日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号