zoukankan      html  css  js  c++  java
  • 20192418 2019-2020-2 《Python程序设计》实验4报告

    20192418张曦 2019-2020-2 《Python程序设计》实验4报告

    课程:《Python程序设计》
    班级: 1924
    姓名: 张曦
    学号:20192418
    实验教师:王志强
    实验日期:2020年5月26日
    必修/选修: 公选课

    1.实验内容

    python综合实践实验中,我做的是一个pygame小游戏。这个游戏有一定的物理模型,效果如下:

    2. 实验过程及结果

    代码及注释:

    #20192418张曦
    import pygame
    import random
    import os
    import math
    import time
    import sys
    from pygame.locals import *
    from sys import exit
    
    # 交代帧率,窗口界面大小
    FPS = 50
    window_w, window_h = 640, 500
    Blue = (0, 0, 255)
    Green = (0, 255, 0)
    
    # 初始化游戏界面
    pygame.init()
    screen = pygame.display.set_mode((window_w, window_h), pygame.DOUBLEBUF, 32)
    pygame.display.set_caption("20192418")
    clock = pygame.time.Clock()
    
    a, b = 320, 240  # a,b为小球坐标
    vx, vy = 100, 0  # vx,vy为小球速度分量
    alla, allb = 0, 0  # alla,allb为小球路程分量
    listy = []  # 随机生成障碍的横坐标
    listx = []  # 随机生成障碍的纵坐标
    k = 0  # k为进程步长
    key = 0  # key为软件运行钥匙
    
    while True:
        # 绘画
        screen.fill((0, 0, 0))
        pygame.draw.circle(screen, (255, 0, 0), (int(a), int(b)), 10)
        rect2 = pygame.draw.rect(screen, (255, 255, 255), (0, 480, 640, 20), 0)
        if (key == 0):
            fonttext2 = pygame.font.Font("freesansbold.ttf", 20)
            text2 = fonttext2.render("Press Up,Down,Right,Left to control ball avoiding blue blocks", True, Blue,
                                     Green)
            textrect2 = text2.get_rect()
            textrect2.center = (320, 490)
            screen.blit(text2, textrect2)
        if (key == 1):
            fonttext3 = pygame.font.Font("freesansbold.ttf", 20)
            text3 = fonttext3.render("Play again.", True, Blue, Green)
            textrect3 = text3.get_rect()
            textrect3.center = (320, 490)
            screen.blit(text3, textrect3)
            mousex, mousey = pygame.mouse.get_pos()
            for event in pygame.event.get():
                if (event.type == MOUSEBUTTONDOWN):
                    import practise2.py
    
                    print("work")
    
                    # 生成障碍
    
        if ((alla + allb) % 400 == 0 and key == 0):
            k += 1
            import random
    
            x = random.randint(0, 620)
            y = random.randint(0, 460)
            listx.append(int(x))
            listy.append(int(y))
            # 绘画障碍
        for i in range(0, k):
            positation = []
            rect = pygame.draw.rect(screen, (0, 0, 255), (listx[i], listy[i], 20, 20), 0)
            if (a >= listx[i] - 10 and a <= listx[i] + 30):
                if (b >= listy[i] - 10 and b <= listy[i] + 30):
                    fonttext = pygame.font.Font("freesansbold.ttf", 24)
                    text = fonttext.render("Game over", True, Blue, Green)
                    textrect = text.get_rect()
                    textrect.center = (320, 240)
                    screen.blit(text, textrect)
                    vx, vy = 0, 0
                    key = 1
                    continue
    
                    # 更新
        pygame.display.update()
    
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_DOWN:
                    vy += 100
                if event.key == pygame.K_UP:
                    vy -= 100
                if event.key == pygame.K_LEFT:
                    vx -= 100
                if event.key == pygame.K_RIGHT:
                    vx += 100
    
        time_passed = clock.tick(FPS)
    
        a += vx * (0.02)
        b += vy * (0.02)
        if (a <= 0 or a >= 640):
            vx *= (-1)
        if (b <= 0 or b >= 480):
            vy *= (-1)
    
        alla += abs(a)
        allb += abs(b)
    

    这个小游戏由上下左右操控小球的移动,比如按一次左键,小球就会以一倍速往左运动,按两次就会两倍速往左运动,在往左运动的过程中按右键就会减速,连续按右键就会以N倍速往右移动,碰到边界会反弹,碰到蓝色方块就会游戏结束。

    因为美工功底比较差,所以用了红色小球和蓝色方块来构成游戏的美工。

    蓝色方块会随机刷新,随着时间的推移,蓝色方块会越来越多,难度逐渐增大,让你难以操控小球继续生存下去。

    其他(感悟、思考等)

    在王老师的指导下,经过一学期的学习,我感触颇深,也学到了很多的python相关知识。从数据类型、操作符,到分支循环结构、列表元组、字符串、函数,再到类和对象、爬虫,我们一步步深入python的学习,也让我们培养了对编程浓厚的兴趣。在王老师没有过多讲授pygame的情况下,我通过搜集资料,自己摸索怎样去设计一个简单的游戏。王老师讲课风趣幽默,作业新颖而且实用性强,真正让我们做到了在玩中学,学中玩。在今后的学习中,我还会继续摸索如何用python去编程,在python的道路上越走越远。

    参考资料

    • 《零基础入门python》
  • 相关阅读:
    149. Max Points on a Line(js)
    148. Sort List(js)
    147. Insertion Sort List(js)
    146. LRU Cache(js)
    145. Binary Tree Postorder Traversal(js)
    144. Binary Tree Preorder Traversal(js)
    143. Reorder List(js)
    142. Linked List Cycle II(js)
    141. Linked List Cycle(js)
    140. Word Break II(js)
  • 原文地址:https://www.cnblogs.com/zhangxi2418/p/12968754.html
Copyright © 2011-2022 走看看