课程:《Python程序设计》
班级: 2222
姓名: 李元浩
学号:20222212
实验教师:王志强
实验日期:2024年3月13日
必修/选修: 公选课
## 1.实验内容
1. 熟悉Python开发环境;
2. 练习Python运行、调试技能;
3. 编写程序,练习变量和类型、字符串、对象、缩进和注释等;
4. 掌握git技能(可把猜数字游戏上传到gitee)
## 2. 实验过程及结果
1. 先下载并熟悉Pycharm的开发环境;
2. 根据课堂和书本上的知识掌握基本的Python编程技能。
3. 编写要求程序
### 猜数字程序代码如下:
```python
import random
choice = 1
while choice == 1:
flag = 1
maxint = 100
level = int(input("""请输入数字编号来选择游戏难度:
1.正常(1~100)
2.困难(1~1000)
3.地狱(1~10000)
"""))
if level == 1:
maxint = 100 # 通过选择的数字分支进行难度处理,将对应上限赋值给maxint
elif level == 2:
maxint = 1000
else:
maxint = 10000
answer = random.randint(1, maxint)
guess = int(input("输入一个数字:"))
while flag == 1:
if answer == guess: # 数字大小判断以及后续反馈的实现
print("你猜对了!")
flag = 0
elif answer > guess:
print("你猜测的数字太小了!", end="")
guess = int(input("请输入大一点的数:"))
else:
print("你猜测的数字太大了!", end="")
guess = int(input("请输入小一点的数:"))
# 选择是否重复运行的实现
choice = int(input("""是否再继续游玩:
0,结束游戏
1,继续游戏
"""))
```
### 贪吃蛇小游戏源程序代码如下:
```python
import pygame
import random
import sys
# 初始化pygame
pygame.init()
# 设置窗口大小
WIDTH, HEIGHT = 640, 480
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇小游戏")
# 定义颜色
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# 贪吃蛇数据结构(初始长度为1)
snake = [(100, 50)]
snake_direction = "RIGHT"
# 食物位置
food = (random.randint(1, (WIDTH//10) - 1) * 10, random.randint(1, (HEIGHT//10) - 1) * 10)
# 游戏速度
clock = pygame.time.Clock()
FPS = 10
# 游戏主循环
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and snake_direction != "DOWN":
snake_direction = "UP"
elif event.key == pygame.K_DOWN and snake_direction != "UP":
snake_direction = "DOWN"
elif event.key == pygame.K_LEFT and snake_direction != "RIGHT":
snake_direction = "LEFT"
elif event.key == pygame.K_RIGHT and snake_direction != "LEFT":
snake_direction = "RIGHT"
elif event.key == pygame.K_ESCAPE:
running = False
# 移动贪吃蛇
head = snake[0]
if snake_direction == "UP":
head = (head[0], head[1] - 10)
elif snake_direction == "DOWN":
head = (head[0], head[1] + 10)
elif snake_direction == "LEFT":
head = (head[0] - 10, head[1])
elif snake_direction == "RIGHT":
head = (head[0] + 10, head[1])
snake.insert(0, head)
# 判断游戏结束
if head in snake[1:] or head[0] < 0 or head[0] >= WIDTH or head[1] < 0 or head[1] >= HEIGHT:
running = False
# 吃食物
if head == food:
food = (random.randint(1, (WIDTH//10) - 1) * 10, random.randint(1, (HEIGHT//10) - 1) * 10)
else:
tail = snake.pop()
# 绘制界面
screen.fill(WHITE)
pygame.draw.rect(screen, GREEN, (food[0], food[1], 10, 10))
for pos in snake:
pygame.draw.rect(screen, RED, (pos[0], pos[1], 10, 10))
pygame.display.flip()
# 控制帧率
clock.tick(FPS)
# 退出游戏
pygame.quit()
sys.exit()
```
### 利用PyCharm进行代码运行
![img](https://img-community.csdnimg.cn/images/8540c39cfb894abbbfbdf01f1bc36d4b.png "#left")
![video](CSDN_COMMUNITY_VIDEO-371143-ID)
### 利用PyCharm进行代码调试
- 设置断点
![img](https://img-community.csdnimg.cn/images/ebf47eb04a58431c9afe11b99b9738f0.png "#left")
- 单步调试
![img](https://img-community.csdnimg.cn/images/5fa33aca2bef4ab9a659ca010f414a36.png "#left")
### 利用老师课上说的三板斧方式将代码托管至码云
![img](https://img-community.csdnimg.cn/images/23271f5cfebb46f199e476c8a7d2795e.png "#left")
## 3. 实验过程中遇到的问题和解决过程
- 问题1:在学习Debug调试的过程中,误将“步入”按钮理解为“步过”按钮,导致总是会弹出random的原函数窗口。
问题1解决方案:在CSDN上仔细查询了各个调试按钮的功能具体区别,了解到“步过”的功能是顺着程序执行代码,且不进入函数;“步入”的功能为进入到函数中,且包括源代码函数。
- 问题2:在学习Pdb调试过程中,将输入“n”和“c”作用弄混。
问题2解决方案:通过查询相关资料,学习到在pdb中输入n为执行下一行,输入c为输入c继续执行程序。前者是继续进行下一行的调试,后者是让程序能够继续运行。
## 参考资料
[python 调试工具 pdb 的基本用法(Python Debugger)](https://blog.csdn.net/qq_43799400/article/details/122582895?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522167836433116800180647981%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=167836433116800180647981&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-122582895-null-null.142^v73^insert_down2,201^v4^add_ask,239^v2^insert_chatgpt&utm_term=pdb&spm=1018.2226.3001.4187)
[为pycharm配置gitee](https://mp.weixin.qq.com/s/0gakV_4DhEsLpauaCcmIkA)
[Pycharm | 一文掌握 Pycharm 中的 Git 操作 ( 超详细)](https://blog.csdn.net/hxj0323/article/details/109208253)