标签:pycharm height pip 积累 内容 click aries append def
课程:《Python程序设计》
班级: 2033
姓名: 冷骏
学号:20203322
实验教师:王志强
实验日期:2021年6月27日
必修/选修: 公选课
实验1设计思路:
1、随机生成食物与小蛇位置
2、通过键盘上下左右键位操作,来控制小蛇的运动方向
3、小蛇吃到食物后变长并生成食物在随机生成新的位置
4、通过让蛇变快来提升游戏难度
5、当蛇头碰到窗口边缘或者自己身体时游戏结束
(1)导入模块:
from random import randrange from turtle import * from freegames import square, vector
(2)游戏初始化:
food = vector(0, 0)#食物的坐标随机生成 snake = [vector(10, 0)]#蛇出现的的位置 aim = vector(0, -10)
(3)蛇的运动--基本位置:
def change(x, y):#改变蛇的位置 aim.x = x aim.y = y def inside(head):#如果头部在边界则返回ture return -200 < head.x < 190 and -200 < head.y < 190
(4)蛇的运动--蛇吃食物
def move():#将蛇向前移动一段 head = snake[-1].copy() head.move(aim) if not inside(head) or head in snake: square(head.x, head.y, 9, ‘red‘) update() return snake.append(head) if head == food:#如果头碰到食物 print(‘Snake:‘, len(snake)) food.x = randrange(-15, 15) * 10 food.y = randrange(-15, 15) * 10 #生成新的食物 else: snake.pop(0) clear() for body in snake:#如果头碰到了身体 square(body.x, body.y, 9, ‘black‘) square(food.x, food.y, 9, ‘green‘) update() ontimer(move, 100)
(5)设置界面与键位:
setup(420, 420, 370, 0)#设置画面大小 hideturtle() tracer(False) listen() onkey(lambda: change(10, 0), ‘Right‘)#向右 onkey(lambda: change(-10, 0), ‘Left‘)#向左 onkey(lambda: change(0, 10), ‘Up‘)#向上 onkey(lambda: change(0, -10), ‘Down‘)#向下 move() done()
(6)实验展现:
(7)完整代码:
码云链接:https://gitee.com/Lengjunnnn/snakegame.git
1 # This is a sample Python script. 2 3 # Press Shift+F10 to execute it or replace it with your code. 4 # Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings. 5 6 7 def print_hi(name): 8 # Use a breakpoint in the code line below to debug your script. 9 print(f‘Hi, {name}‘) # Press Ctrl+F8 to toggle the breakpoint. 10 11 12 # Press the green button in the gutter to run the script. 13 if __name__ == ‘__main__‘: 14 print_hi(‘PyCharm‘) 15 16 # See PyCharm help at https://www.jetbrains.com/help/pycharm/ 17 """Snake, classic arcade game. 18 19 Exercises 20 21 1. How do you make the snake faster or slower? 22 2. How can you make the snake go around the edges? 23 3. How would you move the food? 24 4. Change the snake to respond to mouse clicks. 25 26 """ 27 28 from random import randrange 29 from turtle import * 30 31 from freegames import square, vector 32 33 food = vector(0, 0) 34 snake = [vector(10, 0)] 35 aim = vector(0, -10) 36 37 38 def change(x, y): 39 "Change snake direction." 40 aim.x = x 41 aim.y = y 42 43 44 def inside(head): 45 "Return True if head inside boundaries." 46 return -200 < head.x < 190 and -200 < head.y < 190 47 48 49 def move(): 50 "Move snake forward one segment." 51 head = snake[-1].copy() 52 head.move(aim) 53 54 if not inside(head) or head in snake: 55 square(head.x, head.y, 9, ‘red‘) 56 update() 57 return 58 59 snake.append(head) 60 61 if head == food: 62 print(‘Snake:‘, len(snake)) 63 food.x = randrange(-15, 15) * 10 64 food.y = randrange(-15, 15) * 10 65 else: 66 snake.pop(0) 67 68 clear() 69 70 for body in snake: 71 square(body.x, body.y, 9, ‘black‘) 72 73 square(food.x, food.y, 9, ‘green‘) 74 update() 75 ontimer(move, 100) 76 77 78 setup(420, 420, 370, 0) 79 hideturtle() 80 tracer(False) 81 listen() 82 onkey(lambda: change(10, 0), ‘Right‘) 83 onkey(lambda: change(-10, 0), ‘Left‘) 84 onkey(lambda: change(0, 10), ‘Up‘) 85 onkey(lambda: change(0, -10), ‘Down‘) 86 move() 87 done()
在本学期python公修课的学习当中, 学习了很多知识,每天抱着快乐学习的心态,认识了很多朋友,每天和他们交流交流学习心得,很是开心。虽然课程告一段落,但是我对python的探索却远远没有结束,python比我们同时期所学习的c语言要方便得多,不仅仅语句更简洁,也能够实现更多的功能。
python是我一直想学习的一门语言,多谢了王老师的教导,让我能够真正的入门了python这一门语言,王老师讲课幽默风趣,能够将一些比较难以理解的知识点很形象的展现出来,让我们非常容易理解,相比起原来学习c语言时枯燥的语句逐句讲解,不懂只能课下再听网课,python课程教会了我更多的东西,体会到了用python写脚本的快乐,给了我很大的成就感。最后,python是一门强大的语言,学习它是为了做出更好服务于我们生活工作的工具,未来我也不会停下对python深入学习的脚步,会继续尝试更多有意思的项目,积累经验,不断进步。
20203322 2020-2021-2《Python程序设计》实验四报告
标签:pycharm height pip 积累 内容 click aries append def
原文地址:https://www.cnblogs.com/Lennnnnn/p/14952323.html