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

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

概述本篇博文为博主大一Python选修课的期末作业,主要运用了Pygame叙写了一个实现小小功能的小游戏,可以随意拿去当作业上交。(程序在文底附录)一、目标分析。1.在屏幕下方中央生成一个炮台2.随机生成蝙蝠并作四周反d运动3.时时捕获鼠标位置,调整炮台角度4.鼠标点击射出炮dⅠ.

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

一、目标分析。1.在屏幕下方中央生成一个炮台2.随机生成蝙蝠并作四周反d运动3.时时捕获鼠标位置,调整炮台角度4.鼠标点击射出炮d
Ⅰ.判断是否射中Ⅱ.射中则分数增加
二、使用画图程序绘制相关图片1.蝙蝠:

2.炮台:

3.炮d:

三、程序实现。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:炮d数量
2.count_socre:得分情况
3.Font和screen:pygame窗口中的字体和银幕大小
4.keep_going:程序运行判断,True为继续运行,False则结束运行
5.White和Black:为RGB参数

# 初始化pygame.init()flag = False	#判断鼠标按下条件count_shell = 10count_score = 0Font = pygame.Font.SysFont("Arial", 24)screen = pygame.display.set_mode([800,600])keep_going = TrueWhite = (255,255,255)Black = (0,0,0)
4.更改程序名称
# 更改程序名称pygame.display.set_caption("Sharpshooter")
5.加载背景图片
# 加载背景图片background = pygame.image.load("Background.jpg")
6.加载炮台、炮d与蝙蝠

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]*10bat_flag = [1]*10  #蝙蝠是否被击中标记bat_rect = bat.get_rect()   #获取蝙蝠位置
8.炮d初始化

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

# 炮d初始化shell_x = 400shell_y = 500shell_angle = 0shell_xy = (400,500)shell_rect = shell.get_rect()   #获取炮d位置
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        # 鼠标左键记录炮d方位    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))    '''    # 绘制炮d    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  #修改炮d位置        shell_rect[1] = shell_y - shell.get_height()/2                  # 判断炮d是否越界,越界则炮d消失        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)    # 碰撞判定--          总结       

以上是内存溢出为你收集整理的Python选修课,期末大作业Pygame小游戏<Sharpshooter>全部内容,希望文章能够帮你解决Python选修课,期末大作业Pygame小游戏<Sharpshooter>所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1187450.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)