zoukankan      html  css  js  c++  java
  • 20192405Python程序设计实验4

    20192405Python程序设计实验4

    课程:《Python程序设计》
    班级: 1924
    姓名: 张纹豪
    学号:20192405
    实验教师:王志强
    实验日期:2020年6月1日
    必修/选修: 公选课

    1.实验内容

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

    2.实验过程及结果

    python利用pygame进行简单的游戏开发(贪吃蛇)
    

    3.编写程序。代码如下

    import pygame

    from sys import exit

    import random

    row = 0

    clo = 0

    def __init__(self, row, clo):

    self.row = row

    self.clo = clo

    def copy(self):

    return Point(row=self.row, clo=self.clo)

    pygame.init()

    width = 800

    hight = 400

    ROW = 30

    CLO = 40

    direct = 'left'

    window = pygame.display.set_mode((width, hight))

    pygame.display.set_caption('贪吃蛇游戏')

    # 蛇头坐标定在中间

    head = Point(row=int(ROW / 2), clo=int(CLO / 2))

    # 初始化蛇身的元素数量

    snake = [

    Point(row=head.row, clo=head.clo + 1),

    Point(row=head.row, clo=head.clo + 2),

    Point(row=head.row, clo=head.clo + 3)

    ]

    def gen_food():

    while 1:

    position = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))

    is_coll = False

    if head.row == position.row and head.clo == position.clo:

    is_coll = True

    for body in snake:

    if body.row == position.row and body.clo == position.clo:

    is_coll = True

    break

    if not is_coll:

    break

    return position

    # 蛇头颜色可以自定义

    head_color = (0, 158, 128)

    # 食物坐标

    snakeFood = gen_food()

    # 食物颜色

    snakeFood_color = (255, 255, 0)

    snake_color = (200, 147, 158)

    def rect(point, color):

    # 定位 画图需要left和top

    left = point.clo * width / CLO

    top = point.row * hight / ROW

    # 将方块涂色

    pygame.draw.rect(window, color, (left, top, width / CLO, hight / ROW))

    # 设置帧频率

    clock = pygame.time.Clock()

    while quit:

    # 处理帧频 锁帧

    clock.tick(19)

    # pygame.event.get()获取当前事件的队列 可以同时发生很多事件

    for event in pygame.event.get():

    if event.type == pygame.QUIT:

    quit = False

    elif event.type == pygame.KEYDOWN:

    # 这里小细节蛇不可以直接左右上下 要判断当前是在什么状态下前行

    if event.key == 273 or event.key == 119:

    if direct == 'left' or direct == 'right':

    direct = 'top'

    if event.key == 274 or event.key == 115:

    if direct == 'left' or direct == 'right':

    direct = 'bottom'

    if event.key == 276 or event.key == 97:

    if direct == 'top' or direct == 'bottom':

    direct = 'left'

    if event.key == 275 or event.key == 100:

    if direct == 'top' or direct == 'bottom':

    direct = 'right'

    # 吃东西

    eat = (head.row == snakeFood.row and head.clo == snakeFood.clo)

    # 处理蛇的身子

    # 1.把原来的头插入到snake的头上

    # 2.把最后一个snake删掉

    if eat:

    snakeFood = Point(row=random.randint(0, ROW - 1), clo=random.randint(0, CLO - 1))

    snake.insert(0, head.copy())

    if not eat:

    snake.pop()

    # 移动一下

    if direct == 'left':

    head.clo -= 1

    if direct == 'right':

    head.clo += 1

    if direct == 'top':

    head.row -= 1

    if direct == 'bottom':

    head.row += 1

    dead = False

    if head.clo < 0 or head.row < 0 or head.clo >= CLO or head.row >= ROW:

    dead = True

    for body in snake:

    if head.clo == body.clo and head.row == body.row:

    dead = True

    break

    if dead:

    print('Game Over')

    quit = False

    # 背景画图

    pygame.draw.rect(window, (245, 135, 155), (0, 0, width, hight))

    # 蛇头

    rect(head, head_color)

    # 绘制食物

    rect(snakeFood, snakeFood_color)

    # 绘制蛇的身子

    for body in snake:

    rect(body, snake_color)

    # 交还控制权

    pygame.display.flip()

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

    问题1:记得安装pygame模块

    其他(感悟、思考等)

    *1.课程总结
    这次的python学习包含序列、文件操作、网络编程、GUI、模块、爬虫以及其它基础知识点,通过python的学习,了解到一些编程语言的思想,尤其是因为这学期的特殊情况,这门课比c语言先开一周,于是先一步了解编程方面的知识,对我后续c语言的学习先行掀开一角,会更加轻松。王志强老师也尽心尽责,出现问题先自己尝试解决的方法也让我获益匪浅。(要善用搜索引擎)

    *2.课程体会

    掌握了不少的py技巧,算是填上了之前学期的坑哈哈哈。从hello world一步步的学习中,踩入计算机的大门;从不知道怎么解决问题,到善于自己网上查找答案。我相信这对于我后续的学习也会有很大的帮助。多一门语言,技多不压身嘛。

    3.意见和建议
    不太习惯看视频学习,直接上直播课对我来说可能效果会更好。
    建议:
    希望如果有多余时间可以课上拓宽一下,讲一些比较复杂一点的例子。

    参考资料

    https://cloud.tencent.com/developer/news/315511

  • 相关阅读:
    查看SQL Server版本号(2005 & 2008)
    Installing an App for Testing
    Viewstate 的用法
    How to Open the Pdf file?
    工具类:Log
    SPSiteDataQuery and SPQuery
    SPSite, SPWeb Dispose and Class Design Partter
    Add Properties or Delete List Folder Content Type
    SharePoint UserControl
    Click Once 布署
  • 原文地址:https://www.cnblogs.com/zwh133/p/13074189.html
Copyright © 2011-2022 走看看