#1、导入模块

importpygamefrom pygame.locals import *

importmathimportrandom#2、游戏初始化

#2.1设置展示窗口

pygame.init()

width , height= 640 , 480screen=pygame.display.set_mode((width,height))#2.2 游戏中需要用到的按键keys:wasd

keys =[False,False,False,False]#2.3 游戏玩家的初始位置

playerpos = [100,100]#2.4 跟踪箭头

arrows =[]#2.5 记录玩家精度:消灭的獾的数量and射出的箭头数量

acc =[0,0]#2.6 添加坏蛋和坏蛋计时器(隔一段时间出来一个坏蛋)

badguys = [[640,100]]

badtimer= 100badtimer1=0#2.7 初始健康值

healthvalue = 194

#2.8 声音初始化

pygame.mixer.init()#3、导入声音和图片

#3.1、导入展示的图片

player = pygame.image.load(r".\\pic_lib\dude2.png")

background= pygame.image.load(r".\\pic_lib\grass.png")

arrow= pygame.image.load(r".\\pic_lib\bullet.png")

badguyimg1= pygame.image.load(r".\\pic_lib\badguy4.png")

badguyimg= badguyimg1 #添加了一个图片的复制

castle = pygame.image.load(r".\\pic_lib\castle.png")

healthbar= pygame.image.load(r".\\pic_lib\healthbar.png")

health= pygame.image.load(r".\\pic_lib\health.png")

gameover= pygame.image.load(r".\\pic_lib\gameover.png")

youwin= pygame.image.load(r".\\pic_lib\youwin.png")#3.2、加载游戏操作的声音

hit = pygame.mixer.Sound(r".\\pic_lib\explode.wav")

enemy= pygame.mixer.Sound(r".\\pic_lib\enemy.wav")

shoot= pygame.mixer.Sound(r".\\pic_lib\shoot.wav")

hit.set_volume(0.05)

enemy.set_volume(0.05)

shoot.set_volume(0.05)#3.3 加载背景音乐

pygame.mixer.music.load(r".\\pic_lib\moonlight.wav")

pygame.mixer.music.play(-1,0.0)

pygame.mixer.music.set_volume(0.25)#4、保持循环,游戏运行

running = 1exitcode=0whilerunning:

badtimer-= 1

#5、clear the screen before drawing it again

screen.fill(0)#6、draw the screen element

#6.0 添加背景

for x in range(round( width/background.get_width() ) +1):for y in range(round( height/background.get_height() ) +1):

screen.blit(background,(x*100,y*100))#图片小于背景

#6.1 添加castles

screen.blit(castle,(0,30))

screen.blit(castle,(0,135))

screen.blit(castle,(0,240))

screen.blit(castle,(0,345))#6.2 添加玩家

position =pygame.mouse.get_pos()#获取鼠标位置

angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26))#鼠标的位置和玩家的位置,根据三角定理,可得旋转角度函数为:atan2()

playerrot = pygame.transform.rotate(player,360-angle*57.29)#将玩家按所需角度旋转

playerpos1 = (playerpos[0]+playerrot.get_rect().width/2, playerpos[1]+playerrot.get_rect().height/2)#计算玩家旋转角度后的位置

screen.blit(playerrot, playerpos1)#玩家移动到鼠标指定位置

#6.3 添加箭头

for projectile inarrows:

arrow1= pygame.transform.rotate(arrow, 360-projectile[0]*57.29)

screen.blit(arrow1, (projectile[1], projectile[2]))for bullet inarrows:

index=0

vlex= math.cos(bullet[0])*10vley= math.sin(bullet[0])*10

#10 是箭头的速度

bullet[1] +=vlex

bullet[2] +=vleyif bullet[1]640 or bullet[2]480:#如果超出屏幕,删除箭头

arrows.pop(index)

index+= 1

#6.4 添加坏蛋

#6.4.1 show the badguys

#将坏蛋列表的坏蛋显示到屏幕

for badguy inbadguys:

screen.blit(badguyimg,badguy)#6.4.2 next badguy

#定时结束,添加坏蛋到坏蛋列表

if badtimer ==0:

badguys.append([640,random.randint(50,430)])

badtimer= 100-(badtimer1*2)#定时器设置:先慢后快

if badtimer1>=35:

badtimer= 35

else:

badtimer1+= 5

#6.4.3 attack castle

index =0for badguy inbadguys:#坏蛋以速度7,向前行进

badguy[0] -= 7

#炸碉堡的坏蛋被删除,碉堡掉健康值

badrect =pygame.Rect(badguyimg.get_rect())

badrect.top= badguy[1]

badrect.left=badguy[0]if badrect.left<64:

hit.play()

healthvalue-= random.randint(5,20)

badguys.pop(index)#check for collisions

index1 =0for bullet inarrows:

bullrect=pygame.Rect(arrow.get_rect())

bullrect.left= bullet[1]

bullrect.top= bullet[2]ifbadrect.colliderect(bullrect):#检测两个对象是否重叠

enemy.play()

acc[0]+= 1badguys.pop(index)

arrows.pop(index1)

index1+= 1index+= 1

#6.5 添加血槽

screen.blit(healthbar,(5,5))#先画一个全红色的生命值条

for health1 inrange(healthvalue):

screen.blit(health,(health1+8,8))#根据承包的生命值往生命条里添加绿色

#7、update the screen

#7 - 更新屏幕

#添加一个计时器:游戏90秒倒计时

font = pygame.font.Font(None,24)

survivedtext= font.render(str(int((90000-pygame.time.get_ticks())/60000)).zfill(2)+":"+str(int((90000-pygame.time.get_ticks())/1000%60)).zfill(2),

True,(0,0,0))

textRect=survivedtext.get_rect()

textRect.topright= [635,5]

screen.blit(survivedtext,textRect)#更新屏幕显示

pygame.display.flip()#8、loop through the ecents

#8 - 检查一些新的事件,如果有退出命令,则终止程序的运行

#pygame里,用给按键添加事件的方法,来检测按键。

for event inpygame.event.get():if event.type ==pygame.QUIT:#check if event is quit the game

pygame.quit()

exit(0)if event.type ==pygame.KEYDOWN:#check if event is the X button,it is for moving

if event.key ==K_w:

keys[0]=Trueelif event.key ==K_a:

keys[1] =Trueelif event.key ==K_s:

keys[2] =Trueelif event.key ==K_d:

keys[3] =Trueif event.type ==pygame.KEYUP:if event.key ==K_w:

keys[0]=Falseelif event.key ==K_a:

keys[1] =Falseelif event.key ==K_s:

keys[2] =Falseelif event.key ==K_d:

keys[3] =Falseif event.type ==pygame.MOUSEBUTTONDOWN:#鼠标如果被点击,得到鼠标位置并计算箭头旋转角度

shoot.play()

position=pygame.mouse.get_pos()

acc[1] += 1arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32])#9、move player

ifkeys[0]:

playerpos[1]-=10

elif keys[2]:

playerpos[1]+=10

elif keys[1]:

playerpos[0]-=10

elif keys[3]:

playerpos[0]+=10

#10、Win/Lose check

if pygame.time.get_ticks()>=90000:

running=0

exitcode= 1

if healthvalue <=0:

running=0

exitcode=0if acc[1] !=0:

accuracy= format(acc[0]/acc[1]*100,'.2f')else:

accuracy=0#11、Win/Lose display#显示胜负图片

if running == 0 and exitcode ==0:

screen.blit(gameover,(0,0))else:

screen.blit(youwin,(0,0))#显示胜率

pygame.font.init()

font= pygame.font.Font(None,24)

text= font.render("Accuracy:"+str(accuracy)+"%",True,(255,0,0))

textRect=text.get_rect()

textRect.topright= [350,250]

screen.blit(text,textRect)#刷新

while 1:for event inpygame.event.get():if event.type ==pygame.QUIT:

pygame.quit()

exit(0)

pygame.display.flip()

pygame.mixer.music.stop()

python打地鼠游戏代码100行_PythonStudy_打地鼠游戏代码相关推荐

  1. python打地鼠脚本_制作一个打地鼠的小游戏!100行Python代码轻松搞定

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理. 以下文章来源于萝卜大杂烩 ,作者周萝卜 游戏画面 首先先进行游戏画面排版, classTopWin ...

  2. python数独游戏源代码100行_python实现解数独程序代码

    偶然发现linux系统附带的一个数独游戏,打开玩了几把.无奈是个数独菜鸟,以前没玩过,根本就走不出几步就一团浆糊了. 于是就打算借助计算机的强大运算力来暴力解数独,还是很有乐趣的. 下面就记录一下我写 ...

  3. python必背100代码-100行Python代码实现一款高精度免费OCR工具

    近期Github开源了一款基于Python开发.名为 Textshot 的截图工具,刚开源不到半个月已经500+Star. 很多人学习python,不知道从何学起. 很多人学习python,掌握了基本 ...

  4. python画图代码100行_用100行Python代码告诉你国庆那些景点爆满!

    阅读本文大约需要 7 分钟 本文转载自zone7 概述 前言 思考 统计结果 爬虫技术分析 爬虫代码实现 爬虫分析实现 后记 前言 举国欢庆的国庆节马上就要到来了,你想好去哪里看人山人海了吗?还是窝在 ...

  5. python100行代码-100行Python代码自动抢火车票!

    今年你不妨自己写一段代码来抢回家的火车票,是不是很Cool. 先准备好: 1)12306网站用户名和密码 2)chrome浏览器及下载chromedriver 3)下载Python代码,来自网络整理 ...

  6. python数独游戏源代码100行_python实现自动解数独小程序

    跟朋友最近聊起来数独游戏,突发奇想使用python编写一个自动计算数独解的小程序. 数独的规则不再过多阐述,在此描述一下程序的主要思路: (当前程序只针对于简单的数独,更复杂的还待深入挖掘) 1.计算 ...

  7. python实现贪吃蛇100行代码

    import copy # 拷贝 import turtle # 画板 import tkinter.messagebox as msgbox # 弹窗提示 from random import ra ...

  8. 【Python】如何清空命令行交互界面的代码

    问题描述 清空命令行很简单,用cls命令即可.但在命令行交互界面写了很多 Python 代码,看着很不舒服,如何清空后从头开始写? 解决方案 os.system(command)使得在子 shell ...

  9. python输出1到100整数_python第一个代码程序打印1到100整数

    原博文 2019-05-30 07:36 − def main(): #打印1到100的整数 i=1 while i<=100: print(i) i+=1if __name__=='__mai ...

最新文章

  1. 蚂蚁金服天街:OceanBase 在大促 5 年来的技术演进
  2. 资源论文非系统论文,NLP 圈同行评审存在的六大固化误区!
  3. mysql主从复制延时性问题_MySQL主从同步延迟原因及解决办法
  4. C语言实现类似QQ聊天界面抖动功能
  5. 前端 CSS Framework --- NEC (网易)
  6. [css] 为什么伪类的content不能被选中?
  7. html提现页面模板,提现记录.html
  8. java生成动态验证码_java动态生成验证码
  9. Git CMD - diff: Show changes between commits, commit and working tree, etc
  10. bilstmcrf词性标注_深度学习--biLSTM_CRF 命名实体识别
  11. ai带来的革命_Covid-19将加速AI医疗保健革命
  12. iTextSharp笔记
  13. WPS表格乘的结果和计算机的不一样,同版本wps两个电脑显示不一样怎么办
  14. creo绘图属性模板_creo绘图属性
  15. Unity 3D 入门小游戏 小球酷跑(上)
  16. 网络安全学习笔记6(批处理编写)
  17. 微信小程序如何保存图片到相册
  18. 【数据结构】线性表之单向链表的八大基操
  19. 4x root 红米_小米红米Note4X获取root权限教程
  20. python新闻内容爬虫专用包newspaper详细教程

热门文章

  1. CTF--misc 零宽度字符隐写
  2. python中的类和对象
  3. 计算机丢失tlps,win7系统下PS提示计算机丢失tlpsplib10.dll文件的解决方法
  4. python distutils模块(貌似是用来打包发布自定义python包的)
  5. 修改labelImg软件的yolo标注写入格式(.txt文件不换行的解决办法)(将换行符'\n'替换成'\r\n')
  6. 什么是尾递归?测试python尾递归
  7. python numpy.arange() 函数的使用方法 (在给定间隔内返回均匀间隔的值)
  8. 将所有单个json标注文件合并成一个总的json标注文件(COCO数据集格式)
  9. java找哪一天是星期几_七夕节是几月几日农历时间 2019年七夕情人节哪一天星期几...
  10. 华为鸿蒙系统可以用在哪里,【图片】华为鸿蒙系统的厉害之处在于 你可能非用不可 !【手机吧】_百度贴吧...