20231325 贾罗祁 《Python程序设计》实验四报告

20231325贾罗祁 2023-2024-2《Python程序设计》实验四报告

课程:《Python程序设计》
班级: 2313
姓名: 贾罗祁
学号: 20231325
实验教师:王志强
实验日期:2024年5月15日
必修/选修: 公选课

1.实验内容

Python综合应用:爬虫、数据处理、可视化、机器学习、神经网络、游戏、网络安全等。
课代表和各小组负责人收集作业(源代码、视频、综合实践报告)

例如:编写从社交网络爬取数据,实现可视化舆情监控或者情感分析。

例如:利用公开数据集,开展图像分类、恶意软件检测等

例如:利用Python库,基于OCR技术实现自动化提取图片中数据,并填入excel中。

例如:爬取天气数据,实现自动化微信提醒

例如:利用爬虫,实现自动化下载网站视频、文件等。

例如:编写小游戏:坦克大战、贪吃蛇、扫雷等等

注:在Windows/Linux系统上使用VIM、PDB、IDLE、Pycharm等工具编程实现。

2.实验过程及结果

(1)实验内容

俄罗斯方块小游戏

(2)代码步骤

1.设置方块初始数据
设置游戏参数:定义方块的大小、游戏区域的宽度和高度、游戏窗口的宽度、颜色等。
2.设置屏幕
(1)创建游戏窗口:创建游戏窗口和设置游戏窗口标题;
(2)设置时钟和帧率:创建时钟对象,设置游戏帧率为30帧/秒。
(3)定义绘制网格的函数:draw_grids()绘制游戏区域的网格线。
(4)定义显示文本的函数:show_text(surf, text, size, x, y, color=white)在屏幕上显示文本。
(5)定义绘制得分和最高分的函数:定义得分、最高分和游戏等级,显示当前得分。
3.进行俄罗斯方块游戏的构建
(1)定义方块形状类:CubeShape类定义了方块的形状、颜色、中心位置和方向,以及方块的移动、旋转和冲突检测方法。
(2)定义移除满行的函数:remove_full_line()检测并移除满行,增加得分。
(3)主游戏循环:监听用户输入,更新方块位置,处理游戏结束逻辑,显示得分和最高分,以及绘制游戏界面。
(4)处理用户输入:通过键盘事件处理方块的移动、旋转、暂停和游戏重新开始。
(5)更新方块位置和游戏状态:在游戏循环中,根据帧率更新方块位置,检测方块是否到达底部或发生冲突,生成新的方块。
(6)显示游戏结束信息:当游戏结束时,显示游戏结束信息,等待用户按任意键重新开始游戏,在每次循环结束时,更新屏幕显示,确保所有绘制操作生效。

(3)完整代码
import random
pygame.init()

cell_size = 30
grid_width = 15
grid_height = 20
board_width = cell_size * grid_width
board_height = cell_size * grid_height
side_panel_width = 200
screen_width = board_width + side_panel_width
white = (245,245,245)
black = (0, 0, 0)
grid_line_color = (139,125,107)
block_colors = [(255, 0, 0),
                (0, 255, 0),
                (0, 0, 255),
                (255, 255, 0),
                (255, 0, 255),
                (0, 255, 255),
                (128, 128, 128)]

screen = pygame.display.set_mode((screen_width, board_height))
pygame.display.set_caption("Tetris")
clock = pygame.time.Clock()
FPS = 30

def draw_grid():
    for i in range(grid_width):
        pygame.draw.line(screen, grid_line_color, (i * cell_size, 0), (i * cell_size, board_height))
    for i in range(grid_height):
        pygame.draw.line(screen, grid_line_color, (0, i * cell_size), (board_width, i * cell_size))
    pygame.draw.line(screen, white, (cell_size * grid_width, 0), (cell_size * grid_width, cell_size * grid_height))

def display_text(surf, text, size, x, y, color=white):
    font = pygame.font.SysFont("SimHei", size)
    text_surface = font.render(text, True, color)
    text_rect = text_surface.get_rect()
    text_rect.midtop = (x, y)
    surf.blit(text_surface, text_rect)

current_score = 0
max_score = 0
game_level = 1

def draw_current_score():
    display_text(screen, u'当前得分:{}'.format(current_score), 20, board_width + side_panel_width // 2, 200)

def draw_max_score():
    display_text(screen, u'最高得分:{}'.format(max_score), 20, board_width + side_panel_width // 2, 100)

def show_game_over(screen):
    display_text(screen, '俄罗斯方块', 30, 225, 250)
    display_text(screen, '按任意键开始游戏', 20, 225, 300)

board_color_matrix = []
for i in range(grid_height):
    board_color_matrix.append([0] * grid_width)

class BlockForm(object):
    shapes = ['I', 'J', 'L', 'O', 'S', 'T', 'Z']
    I = [[(0, -1), (0, 0), (0, 1), (0, 2)], [(-1, 0), (0, 0), (1, 0), (2, 0)]]
    J = [[(-2, 0), (-1, 0), (0, 0), (0, -1)], [(-1, 0), (0, 0), (0, 1), (0, 2)], [(0, 1), (0, 0), (1, 0), (2, 0)], [(0, -2), (0, -1), (0, 0), (1, 0)]]
    L = [[(-2, 0), (-1, 0), (0, 0), (0, 1)], [(1, 0), (0, 0), (0, 1), (0, 2)], [(0, -1), (0, 0), (1, 0), (2, 0)], [(0, -2), (0, -1), (0, 0), (-1, 0)]]
    O = [[(0, 0), (0, 1), (1, 0), (1, 1)]]
    S = [[(-1, 0), (0, 0), (0, 1), (1, 1)], [(1, -1), (1, 0), (0, 0), (0, 1)]]
    T = [[(0, -1), (0, 0), (0, 1), (-1, 0)], [(-1, 0), (0, 0), (1, 0), (0, 1)], [(0, -1), (0, 0), (0, 1), (1, 0)], [(-1, 0), (0, 0), (1, 0), (0, -1)]]
    Z = [[(0, -1), (0, 0), (1, 0), (1, 1)], [(-1, 0), (0, 0), (0, -1), (1, -1)]]
    shapes_with_dir = {'I': I, 'J': J, 'L': L, 'O': O, 'S': S, 'T': T, 'Z': Z}

    def __init__(self):
        self.form = self.shapes[random.randint(0, len(self.shapes) - 1)]
        self.center = (2, grid_width // 2)
        self.dir = random.randint(0, len(self.shapes_with_dir[self.form]) - 1)
        self.color = block_colors[random.randint(0, len(block_colors) - 1)]

    def get_all_cell_positions(self, center=None):
        curr_form = self.shapes_with_dir[self.form][self.dir]
        if center is None:
            center = [self.center[0], self.center[1]]
        return [(cell[0] + center[0], cell[1] + center[1]) for cell in curr_form]

    def conflict(self, center):
        for cell in self.get_all_cell_positions(center):
            if cell[0] < 0 or cell[1] < 0 or cell[0] >= grid_height or cell[1] >= grid_width:
                return True
            if board_color_matrix[cell[0]][cell[1]] is not 0:
                return True
        return False

    def rotate(self):
        new_dir = self.dir + 1
        new_dir %= len(self.shapes_with_dir[self.form])
        old_dir = self.dir
        self.dir = new_dir
        if self.conflict(self.center):
            self.dir = old_dir
            return False

    def move_down(self):
        center = (self.center[0] + 1, self.center[1])
        if self.conflict(center):
            return False
        self.center = center
        return True

    def move_left(self):
        center = (self.center[0], self.center[1] - 1)
        if self.conflict(center):
            return False
        self.center = center
        return True

    def move_right(self):
        center = (self.center[0], self.center[1] + 1)
        if self.conflict(center):
            return False
        self.center = center
        return True

    def pause(self):
        is_pause = True
        while is_pause:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    pygame.quit()
                    quit()
                if event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_p:
                        is_pause = False
                    elif event.key == pygame.K_p:
                        is_pause = True
            pygame.display.update()

    def draw(self):
        for cell in self.get_all_cell_positions():
            pygame.draw.rect(screen, self.color, (cell[1] * cell_size, cell[0] * cell_size, cell_size, cell_size))
            pygame.draw.rect(screen, white, (cell[1] * cell_size, cell[0] * cell_size, cell_size, cell_size), 1)

def draw_board():
    for i, row in zip(range(grid_height), board_color_matrix):
        for j, color in zip(range(grid_width), row):
            if color is not 0:
                pygame.draw.rect(screen, color, (j * cell_size, i * cell_size, cell_size, cell_size))
                pygame.draw.rect(screen, white, (j * cell_size, i * cell_size, cell_size, cell_size), 2)

def remove_full_lines():
    global board_color_matrix
    global current_score
    new_matrix = [[0] * grid_width for i in range(grid_height)]
    index = grid_height - 1
    n_full_lines = 0
    for i in range(grid_height - 1, -1, -1):
        is_full = True
        for j in range(grid_width):
            if board_color_matrix[i][j] is 0:
                is_full = False
                continue
        if not is_full:
            new_matrix[index] = board_color_matrix[i]
            index -= 1
        else:
            n_full_lines += 1
    current_score += n_full_lines
    board_color_matrix = new_matrix

running = True
game_paused = False
game_over = True
frame_counter = 0
active_block = 0

while running:
    clock.tick(FPS // game_level)  # 根据等级调整FPS
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if game_over:
                game_over = False
                active_block = BlockForm()
                break
            if event.key == pygame.K_LEFT:
                active_block.move_left()
            elif event.key == pygame.K_RIGHT:
                active_block.move_right()
            elif event.key == pygame.K_DOWN:
                active_block.move_down()
            elif event.key == pygame.K_UP:
                active_block.rotate()
            elif event.key == pygame.K_p:
                active_block.pause()
            elif event.key == pygame.K_SPACE:
                while active_block.move_down() == True:
                    pass
            elif event.key == pygame.K_o:
                game_over = True
                current_score = 0
                active_block = 0
                board_color_matrix = [[0] * grid_width for i in range(grid_height)]
            remove_full_lines()

    if game_over is False and frame_counter % (FPS // game_level) == 0:
        if active_block.move_down() == False:
            for cell in active_block.get_all_cell_positions():
                board_color_matrix[cell[0]][cell[1]] = active_block.color
            active_block = BlockForm()
            if active_block.conflict(active_block.center):
                game_over = True
                current_score = 0
                active_block = 0
                board_color_matrix = [[0] * grid_width for i in range(grid_height)]
        remove_full_lines()

    frame_counter += 1
    screen.fill(black)
    draw_grid()
    draw_board()
    draw_current_score()
    draw_max_score()
    if max_score <= current_score:
        max_score = current_score
    else:
        max_score = max_score
    if active_block is not 0:
        active_block.draw()
    if game_over:
        show_game_over(screen)
    pygame.display.update()
(4)运行结果

1

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

1、
问题一:运行界面中出现了中文显示为乱码的情况
问题一解决方法:我的pycharm没有下载想要的字体,这个可以通过使用系统中默认安装的字体SimHei(黑体)解决
def show_text(surf, text, size, x, y, color=white):
font = pygame.font.SysFont("SimHei", size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
2、
问题二:下落的速度比较慢,游戏的趣味性很低
问题二解决方法:通过调整每秒帧数来实现速度的变化,每增加10分,方块下落的速度就增加一点,直到达到100分后速度不再增加

4.其他(感悟、思考等)

设计俄罗斯方块小游戏,首先我感觉是很麻烦的,哪怕是借助GPT的帮助,但同时,也让我很强烈很强烈地感觉到Python的优势,pygame这个库确实带来了极大地便利;其次,在写这个代码的过程中,制作简易的图形界面,丰富游戏可玩性以及修复一大堆bug,制作完毕让我感觉很有成就感。同时,这个实验也让我感觉到了我还需要在编程语言、程序设计等继续学习努力。

5.对Python程序设计课:

  • 感想与体会
    经过了一个学期的Python课程学习,我对于Python这一编程语言有了一定的了解,也为我将来的课程学习打下了一定的基础。
    随着课程的深入,我逐渐领悟到Python的简洁(尤其是相较于C语言)从基础的变量赋值到复杂的数据结构,从函数的定义到类的创建,每一步的学习都让我对编程有了更深的理解。
    在课程中,我完成了几个实验,比如Socket的使用、剪刀石头布游戏,这些实验极大地提升了我的编程能力,让我更好的理解了如何将理论知识应用到实际问题中,如何通过调试和测试来优化代码。
    当然,必须提到,王老师的课非常有趣,幽默易懂的风格让我对于Python的学习也产生了很大的兴趣(尤其是相较于C语言),就像老师讲到的“‘蛋炒饭’和“‘盖浇饭’”,让我很清楚地区分了面向对象和面向过程,还有老师经常提的“人生苦短,我用python”,我感觉我在以后得学习中都不会忘记了。
  • 意见建议
    老师可以考虑留一些课后作业之类的,感觉这个对于巩固课堂的知识还是有着很好的帮助;
    感觉可以搞一个小组作业之类的,这样对同学之间的交流合作也有一定的帮助吧。

参考资料

https://blog.csdn.net/newlw/article/details/124688995?ops_request_misc=&request_id=&biz_id=102&utm_term=python%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-7-124688995.142v100control&spm=1018.2226.3001.4187

https://blog.csdn.net/m0_50162049/article/details/128043161?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522171687094016800186569523%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=171687094016800186569523&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_click~default-3-128043161-null-null.142v100control&utm_term=python%E4%BF%84%E7%BD%97%E6%96%AF%E6%96%B9%E5%9D%97&spm=1018.2226.3001.4187

posted @ 2024-05-29 21:15  20231325贾罗祁  阅读(5)  评论(0编辑  收藏  举报