20212126张楷勤《Python程序设计》实验四Python综合实验实验报告

2022/5/31 1:19:56

本文主要是介绍20212126张楷勤《Python程序设计》实验四Python综合实验实验报告,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

20212126张楷勤《Python程序设计》实验四Python综合实验实验报告

  • 课程:《Python程序设计》
  • 班级: 2126
  • 姓名:张楷勤
  • 学号:20212126
  • 实验教师:王志强
  • 实验日期:2022年5月28日
  • 必修/选修: 公选课

 

一.实验内容

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

 

二. 实验过程及结果

(一) 实验分析与设计

该游戏为打地鼠,通过鼠标点击地鼠记录分数,倒计时结束停止。

游戏中主要有两个对象:地鼠和锤子,创建enemies类和cursor类。锤子需要靠鼠标的移动来击打地鼠,通过尝试得出地鼠可能出现的位置坐标,再通过定义函数generate_next_pos()来生成地鼠出现的位置。并定义age_enemy()来设置地鼠存活的时间,再用remove_died_enemies()来移除死去的地鼠。当右上角的Time变成0后,不再画地鼠,实现游戏的结束,并且可以在左上角看到自己的得分。

(二) 实现过程

         1.安装pygame

              pygame是一组跨平台的python模块,用于创建视频游戏,现在已被用于数百种商业和开源游戏。

 python -m pip install pygame

         2.初步完成打地鼠的窗口

         (1).创建并保持游戏窗口

             导入pygame模块,调用init()方法初始化pygame模块。设置窗口的宽高,使用display模块显示窗体

import pygame

pygame.init()#初始化窗体

screen = pygame.display.set_mode((1000, 800))

          (2).设置分数和倒计时时间

              让分数显示在左上角,时间在右上角。

 1 import pygame
 2 
 3 pygame.init()#初始化窗体
 4 
 5 screen = pygame.display.set_mode((1000, 800))
 6 
 7 score_value = 0#设置分数
 8 font = pygame.font.Font('freesansbold.ttf', 32)
 9 
10 count_now = 60#设置时间
11 font = pygame.font.Font('freesansbold.ttf',32)
12 
13 textX = 10#设置分数位置
14 textY = 10
15 
16 textx = 250#设置时间位置
17 texty = 10

         (3).设定初步的模型

           先将地鼠用黄色的圆代替,后期再修改。

 1 import pygame
 2 
 3 pygame.init()#初始化窗体
 4 
 5 screen = pygame.display.set_mode((1000, 800))
 6 
 7 score_value = 0#设置分数
 8 font = pygame.font.Font('freesansbold.ttf', 32)
 9 
10 count_now = 60#设置时间
11 font = pygame.font.Font('freesansbold.ttf',32)
12 
13 textX = 10#设置分数位置
14 textY = 10
15 
16 textx = 250#设置时间位置
17 texty = 10
18 
19 enemies = []
20 
21 NUM_COL = 2#目标出现的位置
22 NUM_ROW = 2
23 
24 ENEMY_RADIUS = 150#目标设定
25 ENEMY_COLOR = (255, 255, 0)

 

          (4).随机生成地鼠及其可能出现的位置

             

 1 import pygame
 2 import math
 3 import random
 4 
 5 
 6 pygame.init()#初始化窗体
 7 
 8 screen = pygame.display.set_mode((1000, 800))
 9 
10 score_value = 0#设置分数
11 font = pygame.font.Font('freesansbold.ttf', 32)
12 
13 count_now = 60#设置时间
14 font = pygame.font.Font('freesansbold.ttf',32)
15 
16 textX = 10#设置分数位置
17 textY = 10
18 
19 textx = 250#设置时间位置
20 texty = 10
21 
22 enemies = []
23 
24 NUM_COL = 2#目标出现的位置
25 NUM_ROW = 2
26 
27 ENEMY_RADIUS = 150#目标设定
28 ENEMY_COLOR = (255, 255, 0)
29 
30 def get_pos_by_grid_ind(row, col):#随机生成地鼠及其可能出现的位置
31     return (200+row*400, 200+col*400)
32 
33 def generate_next_enemy_pos():
34     grid_index = random.randint(0, 3)
35     row = math.floor(grid_index/NUM_COL)
36     col = grid_index%NUM_COL
37     return get_pos_by_grid_ind(row, col)

         (5).将地鼠在窗体中画出来和分数和时间在窗体中的显示

 1 import pygame
 2 import math
 3 import random
 4 
 5 
 6 pygame.init()#初始化窗体
 7 
 8 screen = pygame.display.set_mode((1000, 800))
 9 
10 score_value = 0#设置分数
11 font = pygame.font.Font('freesansbold.ttf', 32)
12 
13 count_now = 60#设置时间
14 font = pygame.font.Font('freesansbold.ttf',32)
15 
16 textX = 10#设置分数位置
17 textY = 10
18 
19 textx = 250#设置时间位置
20 texty = 10
21 
22 enemies = []
23 
24 NUM_COL = 2#目标出现的位置
25 NUM_ROW = 2
26 
27 ENEMY_RADIUS = 150#目标设定
28 ENEMY_COLOR = (255, 255, 0)
29 
30 def get_pos_by_grid_ind(row, col):#随机生成地鼠及其可能出现的位置
31     return (200+row*400, 200+col*400)
32 
33 def generate_next_enemy_pos():
34     grid_index = random.randint(0, 3)
35     row = math.floor(grid_index/NUM_COL)
36     col = grid_index%NUM_COL
37     return get_pos_by_grid_ind(row, col)
38 
39 def draw_enemies():#画地鼠
40     for enemy in enemies:
41         pygame.draw.circle(screen, ENEMY_COLOR, (enemy[0], enemy[1]), ENEMY_RADIUS)
42 
43 
44 def show_score(x, y):#显示分数
45     global score_value
46     score = font.render("Score: "+str(score_value), True, (255, 255, 255))
47     screen.blit(score, (x, y))
48     
49 def show_time(x,y):#显示时间
50     global time_value
51     time = font.render("Time: "+str(count_now), True, (255, 255, 255))
52     screen.blit(time, (x, y))
53     

 

          (6). 将开始运行后所有鼠标点击的位置发生的效果进行分析。

               当点到×时游戏结束,当鼠标点击到地鼠身上时定义了一个函数,如果点到,分数加一,并且设置地鼠出现的时间。

 1 import pygame
 2 import math
 3 import random
 4 
 5 
 6 pygame.init()#初始化窗体
 7 
 8 screen = pygame.display.set_mode((1000, 800))
 9 
10 score_value = 0#设置分数
11 font = pygame.font.Font('freesansbold.ttf', 32)
12 
13 count_now = 60#设置时间
14 font = pygame.font.Font('freesansbold.ttf',32)
15 
16 textX = 10#设置分数位置
17 textY = 10
18 
19 textx = 250#设置时间位置
20 texty = 10
21 
22 enemies = []
23 
24 NUM_COL = 2#目标出现的位置
25 NUM_ROW = 2
26 
27 ENEMY_RADIUS = 150#目标设定
28 ENEMY_COLOR = (255, 255, 0)
29 
30 def get_pos_by_grid_ind(row, col):#随机生成地鼠及其可能出现的位置
31     return (200+row*400, 200+col*400)
32 
33 def generate_next_enemy_pos():
34     grid_index = random.randint(0, 3)
35     row = math.floor(grid_index/NUM_COL)
36     col = grid_index%NUM_COL
37     return get_pos_by_grid_ind(row, col)
38 
39 def draw_enemies():#画地鼠
40     for enemy in enemies:
41         pygame.draw.circle(screen, ENEMY_COLOR, (enemy[0], enemy[1]), ENEMY_RADIUS)
42 
43 
44 def show_score(x, y):#显示分数
45     global score_value
46     score = font.render("Score: "+str(score_value), True, (255, 255, 255))
47     screen.blit(score, (x, y))
48 
49 def show_time(x,y):#显示时间
50     global time_value
51     time = font.render("Time: "+str(count_now), True, (255, 255, 255))
52     screen.blit(time, (x, y))
53 
54 def check_enemy_collision(clickX, clickY, enemyX, enemyY):
55     distance = math.sqrt(math.pow(enemyX-clickX, 2)+(math.pow(enemyY-clickY, 2)))
56     return distance<ENEMY_RADIUS
57 
58 def check_enemies_collision(click_pos, enemies):
59     for enemy in enemies:
60         if check_enemy_collision(click_pos[0], click_pos[1], enemy[0], enemy[1]):
61             global score_value
62             score_value +=1
63             enemies.remove(enemy)
64 
65 running = True
66 while running:
67     for event in pygame.get():
68         if event.type == pygame.QUIT:
69             running = False
70         if event.type == pygame.MOUSEBUTTONUP:
71             click_pos = pygame.mouse.get_pos()
72             check_enemies_collision(click_pos, enemies)
73 
74     if pygame.time.get_ticks() % 3000 < 10
75         enemies.append(generate_next_enemy_pos())

 

           (7).最后将运行时该出现的要素都写进来。

 1 import pygame
 2 import math
 3 import random
 4 
 5 
 6 pygame.init()#初始化窗体
 7 
 8 screen = pygame.display.set_mode((1000, 800))
 9 
10 score_value = 0#设置分数
11 font = pygame.font.Font('freesansbold.ttf', 32)
12 
13 count_now = 60#设置时间
14 font = pygame.font.Font('freesansbold.ttf',32)
15 
16 textX = 10#设置分数位置
17 textY = 10
18 
19 textx = 250#设置时间位置
20 texty = 10
21 
22 enemies = []
23 
24 NUM_COL = 2#目标出现的位置
25 NUM_ROW = 2
26 
27 ENEMY_RADIUS = 150#目标设定
28 ENEMY_COLOR = (255, 255, 0)
29 
30 def get_pos_by_grid_ind(row, col):#随机生成地鼠及其可能出现的位置
31     return (200+row*400, 200+col*400)
32 
33 def generate_next_enemy_pos():
34     grid_index = random.randint(0, 3)
35     row = math.floor(grid_index/NUM_COL)
36     col = grid_index%NUM_COL
37     return get_pos_by_grid_ind(row, col)
38 
39 def draw_enemies():#画地鼠
40     for enemy in enemies:
41         pygame.draw.circle(screen, ENEMY_COLOR, (enemy[0], enemy[1]), ENEMY_RADIUS)
42 
43 
44 def show_score(x, y):#显示分数
45     global score_value
46     score = font.render("Score: "+str(score_value), True, (255, 255, 255))
47     screen.blit(score, (x, y))
48 
49 def show_time(x,y):#显示时间
50     global time_value
51     time = font.render("Time: "+str(count_now), True, (255, 255, 255))
52     screen.blit(time, (x, y))
53 
54 def check_enemy_collision(clickX, clickY, enemyX, enemyY):
55     distance = math.sqrt(math.pow(enemyX-clickX, 2)+(math.pow(enemyY-clickY, 2)))
56     return distance<ENEMY_RADIUS
57 
58 def check_enemies_collision(click_pos, enemies):
59     for enemy in enemies:
60         if check_enemy_collision(click_pos[0], click_pos[1], enemy[0], enemy[1]):
61             global score_value
62             score_value +=1
63             enemies.remove(enemy)
64 
65 running = True
66 while running:
67     for event in pygame.event.get():
68         if event.type == pygame.QUIT:
69             running = False
70         if event.type == pygame.MOUSEBUTTONUP:
71             click_pos = pygame.mouse.get_pos()
72             check_enemies_collision(click_pos, enemies)
73 
74     if pygame.time.get_ticks() % 3000 < 5:
75         enemies.append(generate_next_enemy_pos())
76 
77     screen.fill((0, 0, 0))#画背景
78     draw_enemies()#画地鼠
79     show_score(textX,textY)#显示分数
80     show_time(textx,texty)#显示时间
81     pygame.display.update()#更新界面

 

         完成后的效果图如下

 

 

           3.进一步完善这个模型

          (1).将这个黑色的背景换成地鼠洞

               利用pygame.image.load()将背景图片导入。

import pygame
import math
import random


pygame.init()#初始化窗体

bg_image = pygame.image.load("0.png")#设置游戏界面
#bg_image = pygame.transform.scale(bg_image, (bg_image.get_width()/3, bg_image.get_height()/3))
screen = pygame.display.set_mode((bg_image.get_width(), bg_image.get_height()))

score_value = 0#设置分数
font = pygame.font.Font('freesansbold.ttf', 32)
...
    screen.blit(bg_image, (0, 0))#填充游戏背景
    draw_enemies()#画地鼠
    show_score(textX,textY)#显示分数
    show_time(textx,texty)#显示时间
    pygame.display.update()#更新界面

 

 

          (2).将原来的黄圈改成地鼠

                首先根据地鼠的坑位将原来的2*2改成5*5,再通过试的方法将地鼠的坑位和地鼠出现的坐标保持一致,同时用pygame.image.load()将地鼠的图片导入,最后再根据现在地鼠的位置将原来算好的地鼠出现的位置的函数                                            check_exist(new_pos)和generate_next_enemy_pos()改正

 

 

import pygame
...
NUM_COL = 5#设置地鼠出现的位置
NUM_ROW = 5

possible_enemy_pos = [(5,52),(170,52),(85,52),(250,52),(330,52),(5,125),(85,125),(170,125),(250,125),(330,125),
                      (5,198),(85,198),(170,198),(250,198),(330,198),(5,270),(85,270),(170,270),(250,270),(330,270),
                      (5,345),(85,345),(170,345),(250,345),(330,345)]#设置地鼠的具体位置

enemy_image = pygame.image.load("3.jpg")#导入地鼠图片
#enemy_image = pygame.transform.scale(enemy_image, (enemy_image.get_width() // 2,enemy_image.get_height() // 2))
ENEMY_RADIUS = min(enemy_image.get_width(), enemy_image.get_height())// 2.5
#enemybeated_image = pygame.image.load("4.jpg")

def check_exist(new_pos):
    for enemy in enemies:
        if new_pos == (enemy.x, enemy.y):
            return True
    return False

def generate_next_enemy_pos():#算出地鼠出现的位置
    new_pos=()
    while True:
        grid_index = random.randint(0, NUM_ROW*NUM_COL-1)
        new_pos = possible_enemy_pos[grid_index]
        if not check_exist(new_pos):
            break
    return new_pos


def draw_enemies():#画地鼠
    for enemy in enemies:
        screen.blit(enemy_image, (enemy.x, enemy.y))
...

 

 

 

         (3).修改鼠标点击判断

              此时运行代码发现点地鼠没有反应,是因为判断鼠标点击位置和地鼠出现的位置出现偏差。

 

import pygame
...
def check_enemy_collision(clickX, clickY, enemyX, enemyY):#检查鼠标和点击的位置相不相同
    enemyX,enemyY = enemyX + ENEMY_RADIUS, enemyY+ENEMY_RADIUS
    distance = math.sqrt(math.pow(enemyX - clickX,2)+(math.pow(enemyY-clickY,2)))
    return distance<ENEMY_RADIUS
...

 

 

 

            (4).修改地鼠出现的时间

                 pygame对于这些地鼠出现的时间有规范的写法,可以进行规范化。

 

import pygame
...
enemy_image = pygame.image.load("3.jpg")#导入地鼠图片
#enemy_image = pygame.transform.scale(enemy_image, (enemy_image.get_width() // 2,enemy_image.get_height() // 2))
ENEMY_RADIUS = min(enemy_image.get_width(), enemy_image.get_height())// 2.5
enemybeated_image = pygame.image.load("4.jpg")

GENERATE_ENEMY, APPEAR_INTERVAL = pygame.USEREVENT + 2, 500
pygame.time.set_timer(GENERATE_ENEMY, APPEAR_INTERVAL)
...
running = True#鼠标点击时发生的事情(关闭键和点地鼠
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONUP:
            click_pos = pygame.mouse.get_pos()
            check_enemies_collision(click_pos, enemies)
        if event.type == GENERATE_ENEMY:
            if len(enemies) < NUM_COL*NUM_ROW:
                new_pos = generate_next_enemy_pos()
                enemies.append(Enemy(new_pos[0], new_pos[1]))
...

 

 

 

             (5).给地鼠赋予生命

                    当一段时间没有打到这个地鼠,它就会自己钻回洞里。

import pygame
import random
import math
import  sys
from dataclasses import  dataclass
...
GENERATE_ENEMY, APPEAR_INTERVAL = pygame.USEREVENT + 2, 500
pygame.time.set_timer(GENERATE_ENEMY, APPEAR_INTERVAL)

ENEMY_LIFE_SPAN = 3*1000
AGE_ENEMY, AGE_INTERVAL = pygame.USEREVENT+2, ENEMY_LIFE_SPAN//7
pygame.time.set_timer(AGE_ENEMY, AGE_INTERVAL)

@dataclass
class Enemy:
    x: int
    y: int
    life: int = ENEMY_LIFE_SPAN
...
def age_enemy():
    for enemy in enemies:
        enemy.life -= 1000

def remove_died_enemies():
    for enemy in enemies:
        if enemy.life == 0:
            enemies.remove(enemy)

running = True#鼠标点击时发生的事情(关闭键和点地鼠
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONUP:
            click_pos = pygame.mouse.get_pos()
            check_enemies_collision(click_pos, enemies)
        if event.type == AGE_ENEMY:
            age_enemy()
            remove_died_enemies()
        if event.type == GENERATE_ENEMY:
            if len(enemies) < NUM_COL*NUM_ROW:
                new_pos = generate_next_enemy_pos()
                enemies.append(Enemy(new_pos[0], new_pos[1]))
...

 

           (6).鼠标改成锤子

               现在我们要将游戏界面中的鼠标改成锤子。注意,要将显示锤子的代码写在背景和地鼠之后,防止被遮挡。

import pygame
...
def remove_died_enemies():
    for enemy in enemies:
        if enemy.life == 0:
            enemies.remove(enemy)


cursor_image = pygame.image.load("1.png") # 设置鼠标图片
pygame.mouse.set_visible(False) # 隐藏鼠标
...
screen.blit(bg_image, (0, 0))#填充游戏背景
    if count_now != 0:
        draw_enemies()#显示地鼠
    show_score(textX, textY)#显示分数
    show_time(textx, texty)
    pointer_rect = cursor_image.get_rect()
    pointer_rect.center = pygame.mouse.get_pos()
    cursor_x = pointer_rect.x
    cursor_y = pointer_rect.y
    screen.blit(cursor_image, (cursor_x+20, cursor_y))
    pygame.display.update()#更新游戏界面

 

           (7).将倒计时进行下去

                 运行代码后发现倒计时始终没有变,是因为还没有加入倒计时的代码。

import pygame
...
ENEMY_LIFE_SPAN = 3*1000
AGE_ENEMY, AGE_INTERVAL = pygame.USEREVENT+2, ENEMY_LIFE_SPAN//7
pygame.time.set_timer(AGE_ENEMY, AGE_INTERVAL)

clock_event = pygame.USEREVENT+3
pygame.time.set_timer(clock_event,1000)
...
running = True#鼠标点击时发生的事情(关闭键和点地鼠
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONUP:
            click_pos = pygame.mouse.get_pos()
            check_enemies_collision(click_pos, enemies)
        if event.type == AGE_ENEMY:
            age_enemy()
            remove_died_enemies()
        if event.type == GENERATE_ENEMY:
            if len(enemies) < NUM_COL*NUM_ROW:
                new_pos = generate_next_enemy_pos()
                enemies.append(Enemy(new_pos[0], new_pos[1]))
        if event.type == clock_event:
            count_now-= 1
            font.render("Time:"+str(count_now), True, (255, 255, 255))
            if count_now == 0:
                pygame.time.set_timer(clock_event, 0)
...

 

(三) 实验结果

     最终代码

import pygame
import random
import math
import  sys
from dataclasses import  dataclass

pygame.init()#游戏初始化

bg_image = pygame.image.load("0.png")#设置游戏界面
bg_image = pygame.transform.scale(bg_image, (bg_image.get_width()/3, bg_image.get_height()/3))
screen = pygame.display.set_mode((bg_image.get_width(), bg_image.get_height()))

score_value = 0#设置分数
font = pygame.font.Font('freesansbold.ttf',32)

count_now = 60
font = pygame.font.Font('freesansbold.ttf',32)

textX = 10#分数位置
textY = 10

textx = 250
texty = 10

count = 0

enemies = []#设置地鼠

NUM_COL = 5#设置地鼠出现的位置
NUM_ROW = 5

possible_enemy_pos = [(5,50),(170,50),(85,50),(250,50),(330,50),(5,125),(85,125),(170,125),(250,125),(330,125),
                      (5,198),(85,198),(170,198),(250,198),(330,198),(5,270),(85,270),(170,270),(250,270),(330,270),
                      (5,345),(85,345),(170,345),(250,345),(330,345)]#设置地鼠的具体位置

enemy_image = pygame.image.load("3.jpg")#导入地鼠图片
#enemy_image = pygame.transform.scale(enemy_image, (enemy_image.get_width() // 2,enemy_image.get_height() // 2))
ENEMY_RADIUS = min(enemy_image.get_width(), enemy_image.get_height())// 2.5
enemybeated_image = pygame.image.load("4.jpg")

GENERATE_ENEMY, APPEAR_INTERVAL = pygame.USEREVENT + 2, 500
pygame.time.set_timer(GENERATE_ENEMY, APPEAR_INTERVAL)

ENEMY_LIFE_SPAN = 3*1000
AGE_ENEMY, AGE_INTERVAL = pygame.USEREVENT+2, ENEMY_LIFE_SPAN//7
pygame.time.set_timer(AGE_ENEMY, AGE_INTERVAL)

clock_event = pygame.USEREVENT+3
pygame.time.set_timer(clock_event,1000)


@dataclass
class Enemy:
    x: int
    y: int
    life: int = ENEMY_LIFE_SPAN

def check_exist(new_pos):
    for enemy in enemies:
        if new_pos == (enemy.x, enemy.y):
            return True
    return False

def generate_next_enemy_pos():#算出地鼠出现的位置
    new_pos=()
    while True:
        grid_index = random.randint(0, NUM_ROW*NUM_COL-1)
        new_pos = possible_enemy_pos[grid_index]
        if not check_exist(new_pos):
            break
    return new_pos


def draw_enemies():#画地鼠
    for enemy in enemies:
        screen.blit(enemy_image, (enemy.x, enemy.y))


def show_score(x,y):#显示分数
    global score_value
    score = font.render("Score: "+str(score_value), True, (255, 255, 255))
    screen.blit(score, (x, y))

def show_time(x,y):
    global time_value
    time = font.render("Time: "+str(count_now), True, (255, 255, 255))
    screen.blit(time, (x, y))

def check_enemy_collision(clickX, clickY, enemyX, enemyY):#检查鼠标和点击的位置相不相同
    enemyX,enemyY = enemyX + ENEMY_RADIUS, enemyY+ENEMY_RADIUS
    distance = math.sqrt(math.pow(enemyX - clickX,2)+(math.pow(enemyY-clickY,2)))
    return distance<ENEMY_RADIUS


def check_enemies_collision(click_pos, enemies):#检测鼠标点击的位置和地鼠的位置相不相同,并移除地鼠
    for enemy in enemies:
        if check_enemy_collision(click_pos[0], click_pos[1], enemy.x, enemy.y):
            global score_value
            score_value +=1
            enemies.remove(enemy)

#def appear_beated_enemy():
#    for enemy in enemies:
#        if check_enemy_collision(click_pos[0], click_pos[1], enemy.x, enemy.y):
#            screen.blit(enemybeated_image, (enemy.x, enemy.y))
#            if pygame.time.get_ticks() %3000 < 1:
#                enemies.remove(enemy)

def age_enemy():
    for enemy in enemies:
        enemy.life -= 1000

def remove_died_enemies():
    for enemy in enemies:
        if enemy.life == 0:
            enemies.remove(enemy)


cursor_image = pygame.image.load("1.png") # 设置鼠标图片
pygame.mouse.set_visible(False) # 隐藏鼠标

running = True#鼠标点击时发生的事情(关闭键和点地鼠
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.MOUSEBUTTONUP:
            click_pos = pygame.mouse.get_pos()
            check_enemies_collision(click_pos, enemies)
        if event.type == AGE_ENEMY:
            age_enemy()
            remove_died_enemies()
        if event.type == GENERATE_ENEMY:
            if len(enemies) < NUM_COL*NUM_ROW:
                new_pos = generate_next_enemy_pos()
                enemies.append(Enemy(new_pos[0], new_pos[1]))
        if event.type == clock_event:
            count_now-= 1
            font.render("Time:"+str(count_now), True, (255, 255, 255))
            if count_now == 0:
                pygame.time.set_timer(clock_event, 0)


    screen.blit(bg_image, (0, 0))#填充游戏背景
    if count_now != 0:
        draw_enemies()#显示地鼠
    show_score(textX, textY)#显示分数
    show_time(textx, texty)
    pointer_rect = cursor_image.get_rect()
    pointer_rect.center = pygame.mouse.get_pos()
    cursor_x = pointer_rect.x
    cursor_y = pointer_rect.y
    screen.blit(cursor_image, (cursor_x+20, cursor_y))
    pygame.display.update()#更新游戏界面

 

 (四) 将代码上传到华为云上

        1.在华为云服务器上下载pygame

        2.打开WinSCP,并将写好的文件和图片移到云服务器中

 

 

        3.打开putty,登录华为为云服务器,并将环境设置好

 

 

 

 

        4.由于游戏需要界面显示,因此要下载Xming

           链接为https://pan.baidu.com/s/1wMDA-8zqd8eiu6iGWGPqFQ?_at_=1653577595955

        5.进入先前python文件和图片保存的地址

 

 

        6.打开先前下载的XLaunch,一直点击下一步完成

 

 

        7.输入命令xterm,如果没有要另行下载

 

 

        8.进入Xming后,输入python3 5.py便能在云端运行

 

 

三. 课程感想与意见建议

python课还是很有意思的,内容由浅入深,十分的丰富。由于有一点点其它语言的基础对于python最初的运用来时比较顺手的,又掌握了一门语言的基础中的基础的运用,开心。早就听说python语言简介明了不需要繁杂的定义也没有那么多的格式要求(除了缩进),在课程的不断学习中也是逐渐体会到了,也希望在日后的生活中能够继续学习python,更加深入的了解python

经过这一学期学习,我掌握了Python的语言基础、流程控制语句、序列的应用、有关字符串的操作、文件操作、异常处理,学习了解了正则表达式、网络爬虫、Socket编程、Python操作数据库等等。这门课程令人受益匪浅。

同时,我还在原先的认知水平上又了解到了Git,CSDN两个平台,尤其是CSDN网站对小白很友好,不会可以在上面提问,一些热心的人会为你解答,对于未来的学习大有帮助(只是这里面如果解答的人解释不清楚不能追问)。其中令我感触最深的就是Python的第三方库的数量之多、覆盖面之广,利用这些第三方库可以轻松实现许多复杂的功能,对于深入学习和创新实践大有裨益。

虽然课程结束了,但对Python的学习仍要继续、仍在路上。以后,我会继续坚持学习python!!

人生苦短,我用python!!!

 



这篇关于20212126张楷勤《Python程序设计》实验四Python综合实验实验报告的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程