欢迎来到今天的讨论,我们将探讨,python五子棋代码红方蓝方 python五子棋代码最简单的,让我们开始吧!
本篇博文为博主大一Python选修课的期末作业,主要运用了Pygame叙写了一个实现小小功能的小游戏,可以随意拿去当作业上交学而思python分几个level。(程序在文底附录)
一、目标分析。
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)
(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参数
# 初始化
()
flag = False #判断鼠标按下条件
count_shell = 10
count_score = 0
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 = ("")
6.加载炮台、炮弹与蝙蝠
1.bat.set_colorkey:当绘制 bat 对象时,将所有与 colorkeys 相同的颜色值绘制为透明
# 加载炮台和蝙蝠
cannon = ("")
bat = ("")
shell = ("")
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))
.Clock() #时钟
10.游戏开始
while keep_going :
# 判断游戏是否结束
for event in ():
if == :
keep_going = False
# 鼠标左键记录炮弹方位
if == 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)
'''
# 加载背景图
(background,(0,0))
'''
# 绘制炮弹
if flag:
new_shell = pygame.transform.rotate(shell,-math.degrees(shell_angle))
(new_shell,shell_xy)
shell_x += 5*(shell_angle)
shell_y -= 5*(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]:
(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 .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
(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>这个称号!")
()
exit()
# 更新画布
pygame.display.update()
(White)
(40) #每秒40
11.游戏结束
()
四、附录
百度网盘,提取码:xi99
原文地址1:https://blog.csdn.net/qq_21891843/article/details/115731853
python手册 http://www.78tp.com/python/