课程:《Python程序设计》
班级: 2031
姓名:袁子博
学号:20203110
实验教师:王志强
必修/选修: 公选课

1.实验内容

Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。

我做的是游戏 贪吃蛇

2.实验过程及结果

1.下载pygame

2.编写程序

设置游戏名字、游戏框大小

基于python的贪吃蛇游戏设计的毕业论文 python贪吃蛇课程设计报告_Python

 

 

造一条

基于python的贪吃蛇游戏设计的毕业论文 python贪吃蛇课程设计报告_解决方法_02

  我拿四个格子当蛇,并且搞出随机位置出现的食物 

基于python的贪吃蛇游戏设计的毕业论文 python贪吃蛇课程设计报告_Python_03

 

基于python的贪吃蛇游戏设计的毕业论文 python贪吃蛇课程设计报告_解决方法_04

 

 判断输入(上下左右),改变蛇的位置

基于python的贪吃蛇游戏设计的毕业论文 python贪吃蛇课程设计报告_Python_05

 

 判断死亡。这个代码这样的话,即头不能碰到边界也不能碰到自己,这里蛇能吃自己的脖子!比如头在(1,1),其次是(0,1),这时候按左键,游戏也会over!

基于python的贪吃蛇游戏设计的毕业论文 python贪吃蛇课程设计报告_python_06

 

 

 

码云:https://gitee.com/YuanZibo/Y.py/blob/master/main.py

 代码:

import pygame 
import random 
 
caption_height = 600 
caption_width = 600 
white_color = (255, 255, 255) 
black_color = (0, 0, 0) 
game_title = '非常贪吃的贪吃蛇' 
cell = 10 
head_pos = [300, 300] 
food_pos = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10] 
snake_init_pos = [[300,300], [290,300], [280,300], [270,300]] 
pygame.init() 
caption = pygame.display.set_mode((caption_width, caption_height)) 
pygame.display.set_caption(game_title) 
clock = pygame.time.Clock() 
def hit_the_self(): 
if snake_init_pos[0] in snake_init_pos[1:]: 
return True 
else: 
return False 
def draw_rect(color, position): 
pygame.draw.rect(caption, color, pygame.Rect(position[0], position[1], cell, cell)) 
def hit_the_wall(head_pos): 
if head_pos[0] >= caption_width or head_pos[0]<0 or head_pos[1] >= caption_height or head_pos[1] < 0: 
return True 
else: 
return False 
 
def change_direction(head_pos): 
global food_pos 
snake_init_pos.insert(0, list(head_pos)) 
 
if head_pos != food_pos: 
snake_init_pos.pop() 
else: 
food_pos = [random.randrange(1, 60) * 10, random.randrange(1, 60) * 10] 
 
if hit_the_self() or hit_the_wall(head_pos): 
pygame.quit() 
 
 
def main(): 
for pos in snake_init_pos: 
draw_rect(white_color, pos) 
 
draw_rect(white_color, food_pos) 
pygame.display.update() 
 
while 1: 
for event in pygame.event.get(): 
if event.type == pygame.QUIT: 
pygame.quit() 
if event.type == pygame.KEYDOWN: 
if event.key == pygame.K_LEFT: 
head_pos[0] = head_pos[0]-cell 
change_direction(head_pos) 
elif event.key == pygame.K_RIGHT: 
head_pos[0]=head_pos[0]+cell 
change_direction(head_pos) 
elif event.key == pygame.K_UP: 
head_pos[1]= head_pos[1]-cell 
change_direction(head_pos) 
elif event.key == pygame.K_DOWN: 
head_pos[1]= head_pos[1]-cell
change_direction(head_pos) 
 
caption.fill(black_color) 
draw_rect(white_color, food_pos) 
 
for pos in snake_init_pos: 
draw_rect(white_color, pos) 
 
pygame.display.update() 
clock.tick(10) 
if __name__ == '__main__': 
main()

3. 实验过程中遇到的问题和解决过程

问题1:pygame用不了

解决方法:没有import!!!!!

问题2:撞到墙不死

解决方法:设置if语句判断是否撞到,判断的方法是头是否在游戏框范围外

4.其他(感悟、思考等)

  python结课了,但是我对于python的感受和老师第一节课的一段代码一样,即输出“人生苦短,我用python”。

  之前并没有接触过python这门课程,但是在高中学习过VB语言。王老师带着我入了python的门,教会了我很多东西,比如字典、列表、爬虫等知识,让我感受到了不同语言的不同的美妙之处,VB是编写程序交互功能很强,python是写起来很方便,当然,通过这次实验,我好像发现python交互功能好像更强,只不过我不知道。所以,王老师把我领进门了,后面的学习更多的还是要靠自己。老师讲课很好,常常通过一个例子来达到学习的目的,这种方法是我很喜欢的而且效果也很好。

  另外提一嘴,老师有时候讲的快捷键真不错,我现在常常用!比如alt+a全选,我以前咋没发现呢。

建议:写代码讲课的时候,有些特别的代码遇到第二次的时候可以再顺带提一下。