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

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

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

1.实验内容

在综合实践中制作了一个pygame小游戏,是一个关于弹球的小游戏,效果如下图

这个小游戏通过上下左右四个按键控制小球的运动(有一定的物理模型),碰到边界就会弹回来,碰到蓝色方块就会游戏结束。

2. 实验过程及结果

代码如下:

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)

这个小游戏是具有一定的物理模型的,比如按一次左边,小球就以一倍速度往左边位移,按两次就两倍速度,按右边减速,上下亦如此。由于绘画功底比较烂,所以就用红色的球和蓝色的方块来设计游戏的整体美工。游戏简单好玩,bug暂时还没有发现。

其他(感悟、思考等)

上了王老师的python公选课,感触颇深,学到了很多的python知识。王老师上课幽默风趣,作业新颖,注重程序的实用性,真正做到了玩中学,学中玩。从python的基础知识,到分支循环结构、列表元组、字符串、函数、类和对象、爬虫等,王老师带着我们一步步深入学习,我们不仅学到的是知识,更激发了对编程的兴趣。虽然王老师没有讲授pygame的相关内容,但我还是凭着自己的兴趣去设计出这样一个小游戏,查找了很多资料。王老师的讲课真的很好,我们都很喜欢!

参考资料

  • 《零基础入门学习python》
posted @ 2020-05-26 20:45  20192418zx  阅读(89)  评论(0编辑  收藏  举报