Python选修课,期末大作业Pygame小游戏<Sharpshooter>

本篇博文为博主大一Python选修课的期末作业,主要运用了Pygame叙写了一个实现小小功能的小游戏,可以随意拿去当作业上交。(程序在文底附录)

一、目标分析。

1.在屏幕下方中央生成一个炮台

2.随机生成蝙蝠并作四周反弹运动

3.时时捕获鼠标位置,调整炮台角度

4.鼠标点击射出炮弹

Ⅰ.判断是否射中
Ⅱ.射中则分数增加

二、使用画图程序绘制相关图片

1.蝙蝠: 在这里插入图片描述

2.炮台: 在这里插入图片描述

3.炮弹: 在这里插入图片描述

三、程序实现。

1.导入相关库

import pygame,math,random

2.定义炮台转动的函数—“whirl”

1.mouse_x和mouse_y为捕获的鼠标xy轴对应坐标

# 定义炮台转动函数
def whirl(image,a,b):
    mouse_x,mouse_y = pygame.mouse.get_pos()
    angle = math.degrees(math.atan2(mouse_x-400,600-mouse_y)) #弧度转角度
    new_image = pygame.transform.rotate(image,-angle)
    screen.blit(new_image,(a,b))

3.定义蝙蝠随机运动函数—“batmove”

1.引入全局变量new_x和new_y方便修改蝙蝠坐标参数
2.new_x/y为二维列表,第一个变量代表蝙蝠代号,第二个变量代表蝙蝠坐标参数xy

# 定义蝙蝠随机运动函数
def batmove(direction,n):
    global new_x,new_y
    if direction==1:
        new_x=locations[n][0]+speedx[n]
        new_y=locations[n][1]+speedy[n]
    elif direction==2:
        new_x=locations[n][0]+speedx[n]
        new_y=locations[n][1]-speedy[n]
    elif direction==3:
        new_x=locations[n][0]-speedx[n]
        new_y=locations[n][1]+speedy[n]
    else:
        new_x=locations[n][0]-speedx[n]
        new_y=locations[n][1]-speedy[n]
    return (new_x,new_y)

3.初始化相关参数

1.count_shell:炮弹数量
2.count_socre:得分情况
3.font和screen:pygame窗口中的字体和银幕大小
4.keep_going:程序运行判断,True为继续运行,False则结束运行
5.White和Black:为RGB参数

# 初始化
pygame.init()
flag = False	#判断鼠标按下条件
count_shell = 10
count_score = 0
font = pygame.font.SysFont("Arial", 24)
screen = pygame.display.set_mode([800,600])
keep_going = True
White = (255,255,255)
Black = (0,0,0)

4.更改程序名称

# 更改程序名称
pygame.display.set_caption("Sharpshooter")

5.加载背景图片

# 加载背景图片
background = pygame.image.load("Background.jpg")

6.加载炮台、炮弹与蝙蝠

1.bat.set_colorkey:当绘制 bat 对象时,将所有与 colorkeys 相同的颜色值绘制为透明

# 加载炮台和蝙蝠
cannon = pygame.image.load("Cannon.png")
bat = pygame.image.load("Bat.jpg")
shell = pygame.image.load("shell.png")
colorkey = bat.get_at((0,0))
bat.set_colorkey(colorkey)

7.随机蝙蝠位置和运动方向

# 随机蝙蝠位置和运动方向
locations = [0]*10  #蝙蝠(x,y)位置存放
direction = [0]*10  #随机方向
speedx = [5]*10  #调整方向
speedy = [5]*10
bat_flag = [1]*10  #蝙蝠是否被击中标记
bat_rect = bat.get_rect()   #获取蝙蝠位置

8.炮弹初始化

1.shell_x/y:设置炮台初始位置
2.shell_angle:设置炮弹角度(默认为0)

# 炮弹初始化
shell_x = 400
shell_y = 500
shell_angle = 0
shell_xy = (400,500)
shell_rect = shell.get_rect()   #获取炮弹位置

9.初始化随机蝙蝠位置和方向

# 初始化随机蝙蝠位置和方向
for n in range(10):
    locations[n] = (random.randint(200,700),random.randint(100,350))
    direction[n] = int(random.randint(1,4))
timer=pygame.time.Clock()  #时钟

10.游戏开始

while keep_going :
    # 判断游戏是否结束
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False
    
    # 鼠标左键记录炮弹方位
    if event.type == pygame.MOUSEBUTTONDOWN:
        if pygame.mouse.get_pressed()[0]:  
            flag = True
        mouse_x,mouse_y = pygame.mouse.get_pos()
        shell_angle = math.atan2(mouse_x-400,600-mouse_y)

    '''
    # 加载背景图
    screen.blit(background,(0,0))
    '''

    # 绘制炮弹
    if flag:    
        new_shell = pygame.transform.rotate(shell,-math.degrees(shell_angle))
        screen.blit(new_shell,shell_xy)
        shell_x += 5*math.sin(shell_angle)
        shell_y -= 5*math.cos(shell_angle)
        shell_xy = (shell_x,shell_y)
        shell_rect[0] = shell_x - shell.get_width()/2  #修改炮弹位置
        shell_rect[1] = shell_y - shell.get_height()/2  
        
        # 判断炮弹是否越界,越界则炮弹消失
        if shell_x<=0 or shell_x+shell.get_width()>=800:
            shell_x,shell_y = 400,500
            shell_xy = (shell_x,shell_y)
            flag = False
            count_shell -= 1
        if shell_y <= 0 :
            shell_x,shell_y = 400,500
            shell_xy = (shell_x,shell_y)
            flag = False    
            count_shell -= 1


    # 绘制蝙蝠
    for n in range(10): 
        if bat_flag[n]: 
            screen.blit(bat,batmove(direction[n],n))
            
            # 判断蝙蝠是否越界,越界则更改方向
            if new_x <= 200 or new_x + bat.get_width() >= 800:
                speedx[n] = -speedx[n]
            if new_y <= 0 or new_y+bat.get_height() >= 450:
                speedy[n] = -speedy[n]

            locations[n] = (new_x,new_y)


    # 碰撞判定--💥
    for n in range(10):
        if pygame.Rect.colliderect(shell_rect,locations[n][0]-bat.get_width()/2,
            locations[n][1]-bat.get_height()/2,bat.get_width(),bat.get_height()):
            flag = False
            bat_flag[n]=0
            locations[n]=(800,600)
            shell_x,shell_y = 400,500
            shell_xy = (shell_x,shell_y)
            shell_rect = shell.get_rect()
            count_shell -= 1
            count_score += 10


    # 实时获取鼠标位置以改变炮台方向
    whirl(cannon,350,465)

    # 显示分数以及炮弹剩余
    draw_string = "Your score:   " + str(count_score)
    draw_string += "  -  Remaining shells : " + str(count_shell)
    text = font.render(draw_string, True, Black)
    text_rect = text.get_rect()
    text_rect.centerx = screen.get_rect().centerx
    text_rect.y = 10
    screen.blit (text, text_rect)

    # 游戏结束判定
    if count_shell == 0:
        if count_score == 100:
            print(f"恭喜您百发百中击杀了全部{int(count_score/10)}只蝙蝠,为新冠肺炎的抵制贡献了一大份力量!!")
        elif count_score>=5 :
            print(f"恭喜您击杀了{int(count_score/10)}只蝙蝠,为新冠肺炎的抵制贡献了一小份力量!")
        elif count_score>=1 :
            print(f"恭喜您击杀了{int(count_score/10)}只蝙蝠,为新冠肺炎的抵制贡献了一份微薄的力量!")
        else :
            print("您一只蝙蝠都没有杀死,对不起<Sharpshooter>这个称号!")          
        pygame.quit()
        exit()
	
    # 更新画布
    pygame.display.update()
    screen.fill(White)
    timer.tick(40)  #每秒40

11.游戏结束

pygame.quit()

四、附录

百度网盘,提取码:xi99

  • 19
    点赞
  • 152
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sumzeek丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值