# 20212217刘恒谦 2022-2022-2 《Python程序设计》实验四报告

## 2. 实验过程及结果

1.购买云服务器

2.下载pygame

wget https://files.pythonhosted.org/packages/1a/04/d6f1159feaccdfc508517dba1929eb93a2854de729fa68da9d5c6b48fa00/setuptools-39.2.0.zip

unzip setuptools-39.2.0.zip

cd setuptools-39.2.0

python3 setup.py build
#以上命令用于下载setuptool

tar xf pip-10.0.1.tar.gz

cd pip-10.0.1

python3 setup.py install
#以上下载pip3--10.0.1

pip3 install pygame


3.配置远程桌面

yum install xterm

yum install xauth


vi /etc/ssh/sshd_config


xming下载并安装好后，在菜单栏找到xlaunch，一直点下一步至完成即可。之后打开putty输入xterm即可看见窗口了。

xterm启动很慢，需要耐心等待5秒左右

4.编写脚本上传运行

want是食物列表中的一个食物，它也是个坐标。

head = [200, 300]            #蛇头——一个坐标
towards = 0         #蛇的方向
foods = [(690, 400)]#食物列表
want = foods[0]              #想要的食物——一个坐标

going = True
while going:                       #游戏循环走起——Going
SCREEN.fill((255, 255, 255))
for event in pg.event.get([pg.KEYDOWN, pg.QUIT]):
if event.type == pg.QUIT:
going = False         #按XX游戏退出——QUIT
#接近目标
#蛇头在食物 左 towards = 0  或  上 towards = 1
if head[towards] < want[towards] - R -A:
run = V
#蛇头在食物 右 towards = 0  或  下 towards = 1
elif head[towards] > want[towards] + R:
run = -V
#什么时候换方向
else:
towards = 1 - towards


#吃到食物
for food in foods:
if food[0] - R - A <= head[0] <= food[0] + R and food[1] - R - A <= head[1] <= food[1] + R:
#删除食物
foods.remove(food)
#追加食物
foods.append((randint(0, 750), randint(0, 550)))
#蛇身增长
#吃到的就是目标食物
if food == want:
#换一个目标
want = choice(foods)


### 源代码

import pygame as pg
from random import randint, choice

SCREEN = pg.display.set_mode([800, 600])
pg.display.set_caption('贪吃蛇')

V = 21      #蛇速
A = 20      #蛇身方块
R = 20      #食物半径

#初始贪吃蛇
direct = "右"

#初始化食物
foods = [(220, 350), (450, 250), (390, 340), (690, 400)]

#初始智能蛇
which = 0
want = foods[0]
run = V

going = True
while going:
SCREEN.fill((255, 255, 255))
#以下是贪吃蛇的部分_______________________________________________________________________________________
for event in pg.event.get([pg.KEYDOWN, pg.QUIT]):
if event.type == pg.QUIT:
going = False
elif event.key == pg.K_w:
direct = "上"
elif event.key == pg.K_s:
direct = "下"
elif event.key == pg.K_a:
direct = "左"
elif event.key == pg.K_d:
direct = "右"
pg.event.clear()

if direct == '上':
if direct == '下':
if direct == '左':
if direct == '右':

snacks.pop()

#以下是智能蛇的部分_______________________________________________________________________________________
#接近目标
#蛇头在食物 左 towards = 0  或  上 towards = 1
if thead[which] < want[which] - R:
run = V
#蛇头在食物 右 towards = 0  或  下 towards = 1
elif thead[which] > want[which] + R:
run = -V
#什么时候换方向
else:
which = 1 - which
#蛇的运动
other.pop()

#吃到食物_______智能蛇 和 贪吃蛇_____________________________________________________________________________
for food in foods:
#把食物画出来
pg.draw.circle(SCREEN, (238, 180, 34), (food[0], food[1]), R)
#如果贪吃蛇吃到食物
if food[0] - R - A <= head[0] <= food[0] + R and food[1] - R - A <= head[1] <= food[1] + R:
#删除食物
foods.remove(food)
#蛇身增长
#吃到的就是目标食物
if food == want:
#换一个目标
want = choice(foods)
#如果智能蛇吃到食物
elif food[0] - R - A <= thead[0] <= food[0] + R and food[1] - R - A <= thead[1] <= food[1] + R:
#删除食物
foods.remove(food)
#追加食物
#吃到的就是目标食物
if food == want:
#换一个目标
want = choice(foods)
#追加食物
if len(foods) < 4:
foods.append((randint(0, 750), randint(0, 550)))

#安排就绪后绘制
for snack in snacks[1:]:
pg.draw.rect(SCREEN, (100, 200, 200), (snack[0], snack[1], A, A))

for the in other[1:]:
pg.draw.rect(SCREEN, (0, 238, 118), (the[0], the[1], A, A))

pg.display.update()
pg.time.Clock().tick(20)
pg.quit()


## 3.实验问题及解决

1.下载pygame报错

whereis python3

whereis pip3


python3 -V

pip3 -V


python3 file.py


2.运行报错
pygame和tkinter, turtle一样，运行时会产生窗口，如果没有可用的屏幕，就会引发error

## 6.结课感想与体会

posted on 2022-05-27 18:13  谦寻大川  阅读(96)  评论(0编辑  收藏  举报