ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

20191325施羿 2020-2021 《Python程序设计》综合实践报告

2021-06-26 16:00:37  阅读:172  来源: 互联网

标签:Python 施羿 2020 pygame key screen import event 255


20191325施羿 2020-2021 《Python程序设计》综合实践报告

课程:《Python程序设计》
班级: 1913
姓名: 施羿
学号:20191325
实验教师:王志强
实验日期:2020年6月26日
必修/选修: 公选课

1.实验内容

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

2. 实验过程及结果

代码及注释:

#20191325施羿

import pygame

import random

import os

import math

import time

import sysfrom 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》

标签:Python,施羿,2020,pygame,key,screen,import,event,255
来源: https://www.cnblogs.com/2902480848sy/p/14934497.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有