代码修改bug,添加注释等,獾的速度加快之后很难……

git地址:

https://github.com/Jailman/blowupyrcastle.git

游戏资源使用了文章中附带的下载,原版文章请看这里:

http://blog.jobbole.com/46308/

附一些基本的数学知识

如图比如以角A为例
sinA=对边:斜边=BC:AC
cosA=临边:斜边=AB:AC
tanA=对边:临边=BC:AB
cotA=临边:对边=AB:BC

#!/usr/bin/python
#coding=utf8 # 1 - Import library
import cStringIO
import pygame
import random
import math
from pygame.locals import *
from os import remove # 2 - Initialize the game
pygame.init()
pygame.display.set_caption('捍卫荣誉')
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
keys = [False, False, False, False]
playerpos=[130,240]
acc=[0,0] #[0]是击中数,[1]是发射数
arrows=[] #发射箭头列表,点击鼠标时向其中填充bullet列表
badtimer=100
badtimer1=0
badguys=[[640, random.randint(40,400)]]
healthvalue=194 #总血量
pygame.mixer.init() # 3 - Load images
start = pygame.image.load("resources/images/start.png")
grass = pygame.image.load("resources/images/bg2.png")
castle = pygame.image.load("resources/images/hq1.png")
player = pygame.image.load("resources/images/p1.1.png")
player1 = pygame.image.load("resources/images/p1.1.png")
player2 = pygame.image.load("resources/images/p1.2.png")
arrow = pygame.image.load("resources/images/bullet6.png")
badguyimg1 = pygame.image.load("resources/images/badguy1.1.png")
badguyimg2 = pygame.image.load("resources/images/badguy1.2.png")
badguyimg3 = pygame.image.load("resources/images/badguy1.3.png")
badguyimg4 = pygame.image.load("resources/images/badguy1.4.png")
badguyimgs = [badguyimg1, badguyimg2, badguyimg3, badguyimg4]
# badguyimg = badguyimg1
healthbar = pygame.image.load("resources/images/healthbar.png")
health = pygame.image.load("resources/images/health.png")
gameover = pygame.image.load("resources/images/gameover2.png")
youwin = pygame.image.load("resources/images/youwin.png")
# 3.1 - Load audio
begin = pygame.mixer.Sound("resources/audio/start.wav")
hit = pygame.mixer.Sound("resources/audio/explode.wav")
enemy = pygame.mixer.Sound("resources/audio/enemy.wav")
shoot = pygame.mixer.Sound("resources/audio/shoot.wav")
win = pygame.mixer.Sound("resources/audio/win.wav")
lost = pygame.mixer.Sound("resources/audio/lost.wav")
bgm = 'resources/audio/cowBGM.wav'
hit_castle = pygame.mixer.Sound("resources/audio/explode2.wav")
begin.set_volume(0.50)
hit.set_volume(0.07)
enemy.set_volume(0.20)
shoot.set_volume(0.07)
win.set_volume(0.25)
hit_castle.set_volume(0.18)
pygame.mixer.music.load(bgm)
pygame.mixer.music.play(-1, 0.0)
pygame.mixer.music.set_volume(0.25) #开局画面
running = 1
while running:
screen.fill(0)
screen.blit(start, (0,0))
#提示按键开始
font = pygame.font.Font(None, 30)
text1 = font.render("Press [SPACE] to start", True, (255,0,0))
screen.blit(text1, (230,450)) #调整显示文字位置
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key==K_SPACE:
begin.play()
running = 0
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
pygame.display.flip() # 按下确定键后的透明渐变效果
from PIL import Image
def addTransparency(img, factor):
img = img.convert('RGBA')
img_blender = Image.new('RGBA', img.size, (0,0,0,0))
img = Image.blend(img_blender, img, factor)
return img
img = Image.open("resources/images/start.png")
tran = 1
while tran > 0.8:
img = addTransparency(img, factor = tran)
imgBuf = cStringIO.StringIO(img.tobytes())
imgx = pygame.image.frombuffer(imgBuf.getvalue(), (640,480), "RGBA")
screen.blit(imgx, (0,0))
tran -= 0.001
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
pygame.display.flip() # 4 - keep looping through
running = 1
exitcode = 0
n = 0
while running:
badtimer-=1
# 5 - clear the screen before drawing it again
screen.fill(0)
# 6 - draw the screen elements
for x in range(width/grass.get_width()+1):
for y in range(height/grass.get_height()+1):
screen.blit(grass,(x*100,y*100))
# screen.blit(grass, (0,0))
screen.blit(castle,(0,30))
screen.blit(castle,(0,135))
screen.blit(castle,(0,240))
screen.blit(castle,(0,345 ))
# 6.1 - Set player position and rotation
position = pygame.mouse.get_pos()
angle = math.atan2(position[1]-(playerpos[1]+player.get_height()/2),position[0]-(playerpos[0]+player.get_width()/2))
# print angle
# playerrot = pygame.transform.rotate(player, 360-angle*57.29)
playerrot = pygame.transform.rotate(player, 360-180*angle/math.pi) #180*angle/math.pi为偏转角度
playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2)
screen.blit(playerrot, playerpos1)
# 6.2 - Draw arrows
for bullet in arrows: #arrow中的bullet列表是在按鼠标发射时追加的,bullet[0]是atan(y,x)
# print bullet
index=0
velx=math.cos(bullet[0])*10 #乘数调整子弹横向速度
# print math.cos(bullet[0])
vely=math.sin(bullet[0])*10 #乘数调整子弹竖向速度
# print math.sin(bullet[0])
bullet[1]+=velx
bullet[2]+=vely
if bullet[1]<-64 or bullet[1]>640 or bullet[2]<-64 or bullet[2]>480:
arrows.pop(index)
index+=1
for projectile in arrows:
arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29)
screen.blit(arrow1, (projectile[1], projectile[2]))
# 6.3 - Draw badgers
if badtimer==0:
badguys.append([640, random.randint(50,430)])
badtimer=100-(badtimer1*2)
if badtimer1>=35:
badtimer1=35
else:
badtimer1+=5
index=0
for badguy in badguys:
if badguy[0]<-64:
badguys.pop(index)
badguy[0]-=1 #调整獾的移动速度
# 6.3.1 - Attack castle
badrect=pygame.Rect(badguyimg1.get_rect())
badrect.top=badguy[1]
badrect.left=badguy[0]
if badrect.left<64:
# hit.play()
hit_castle.play()
healthvalue -= random.randint(5,20) #敌人碰撞城堡掉血
badguys.pop(index)
#hit player
playerrect = pygame.Rect(player.get_rect())
playerrect.left = playerpos[0] - 32 #增减一些值使触碰画面看上去更贴合
playerrect.top = playerpos[1] - 10 #增减一些值使触碰画面看上去更贴合
# print playerrect
if badrect.colliderect(playerrect):
hit.play()
healthvalue -= random.randint(3,12) #玩家碰撞敌人掉血
badguys.pop(index)
#6.3.2 - Check for collisions
index1=0
for bullet in arrows:
bullrect=pygame.Rect(arrow.get_rect())
bullrect.left=bullet[1]
bullrect.top=bullet[2]
if badrect.colliderect(bullrect):
enemy.play()
acc[0]+=1
badguys.pop(index)
arrows.pop(index1)
index1+=1
# 6.3.3 - Next bad guy
index+=1
for badguy in badguys:
# screen.blit(badguyimg, badguy)
n = n + 1
if n > (len(badguyimgs)) * 10-1:
n = 0
screen.blit(badguyimgs[int(n/10)], badguy)
# 6.4 - Draw clock
font = pygame.font.Font(None, 24)
#此处为漏洞,当点击窗口标题时,时间依然在流逝,但是游戏却暂停了,导致点击标题即可获胜
survivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)+":"+str((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)
# 6.5 - Draw health bar
screen.blit(healthbar, (5,5))
for health1 in range(healthvalue):
screen.blit(health, (health1+8,8))
#在健康值上方显示文字
hfont = pygame.font.Font(None, 20)
htext = hfont.render("Health", True, (255,0,0))
screen.blit(htext, (50,8)) #调整显示文字位置,覆盖在健康值上方
# 7 - update the screen
pygame.display.flip()
# 8 - loop through the events
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key==K_w:
keys[0]=True
elif event.key==K_a:
keys[1]=True
elif event.key==K_s:
keys[2]=True
elif event.key==K_d:
keys[3]=True
if event.type == pygame.KEYUP:
if event.key==pygame.K_w:
keys[0]=False
elif event.key==pygame.K_a:
keys[1]=False
elif event.key==pygame.K_s:
keys[2]=False
elif event.key==pygame.K_d:
keys[3]=False # check if the event is the X button
if event.type==pygame.QUIT:
# if it is quit the game
pygame.quit()
exit(0)
if event.type==pygame.MOUSEBUTTONDOWN:
shoot.play()
player = player2
position=pygame.mouse.get_pos()
acc[1]+=1
arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32])
if event.type==pygame.MOUSEBUTTONUP:
player = player1
# 9 - Move player
if keys[0]:
if not playerpos[1] < 0:
playerpos[1]-=3
elif keys[2]:
if not playerpos[1] > height:
playerpos[1]+=3
if keys[1]:
if not playerpos[0] < 100:
playerpos[0]-=3
elif keys[3]:
if not playerpos[0] > width:
playerpos[0]+=3 #10 - Win/Lose check
if pygame.time.get_ticks()>=90000:
running=0
exitcode=1
if healthvalue<=0:
running=0
exitcode=0
if acc[1]!=0:
accuracy=acc[0]*1.0/acc[1]*100
else:
accuracy=0
# 11 - Win/lose display
if exitcode==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.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery+24
pygame.mixer.music.set_volume(0)
lost.play()
screen.blit(gameover, (0,0))
screen.blit(text, textRect)
else:
pygame.font.init()
font = pygame.font.Font(None, 24)
text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0))
textRect = text.get_rect()
textRect.centerx = screen.get_rect().centerx
textRect.centery = screen.get_rect().centery+24
screen.blit(youwin, (0,0))
pygame.mixer.music.set_volume(0)
win.play()
screen.blit(text, textRect)
ticksstart = pygame.time.get_ticks()# 作为延时开启背景音乐
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit(0)
ticksend = pygame.time.get_ticks()
ticksdiff = ticksend - ticksstart #计算延时
if ticksdiff == 3500: #开启背景音乐
pygame.mixer.music.set_volume(0.25)
pygame.display.flip()

炸掉你的城堡!(pygame獾兔大战)的更多相关文章

  1. python(pygame)滑稽大战(类似飞机大战) 教程

    成品已录制视频投稿B站(本文目前实现了基础的游戏功能),点击观看项目稽忽悠不(github)地址:https://github.com/BigShuang/From-simple-to-Huaji 本 ...

  2. 基于pygame实现飞机大战【面向过程】

    一.简介 pygame 顶级pygame包 pygame.init - 初始化所有导入的pygame模块 pygame.quit - uninitialize所有pygame模块 pygame.err ...

  3. Python+Pygame开发太空大战/飞机大战完整游戏项目(附源代码)

    项目名称:太空大战 开发环境:Python3.6.4 第三方库:Pygame1.9.6 代码编辑器:Sublime Text 先来看一下游戏画面吧!  游戏画面动态且丰富哦!   需求分析 利用Pyt ...

  4. 圣魔大战3(Castle Fantisia)艾伦希亚战记完美攻略

    作为城堡幻想曲系列续作,艾伦希亚战记继承了前作的战棋+养成模式进行游戏. (城堡幻想曲3,纠正大家个错误哦,不是圣魔大战3,圣魔大战是城堡幻想曲2,圣魔大战不是个系列,艾伦西亚战记==艾伦希亚战记,一 ...

  5. 青少年如何使用 Python 开始游戏开发

    这是一篇由教程团队成员Julian Meyer发表的文章,一个12岁的python开发人员.你可以在Google+和Twitter上找到他. 你可曾想过如何创建视频游戏吗?它不像你想象的那么复杂! 在 ...

  6. 使用 Python 开始游戏开发

    使用 Python 开始游戏开发 这是一篇由教程团队成员Julian Meyer发表的文章,一个12岁的python开发人员.你可以在Google+和Twitter上找到他. 你可曾想过如何创建视频游 ...

  7. 小甲鱼零基础入门PYTHON

     000.愉快的开始 00:17:37 ☆  001.我和Python的第一次亲密接触 00:13:26 ★  002.用Python设计第一个游戏 00:24:00 ★  003.小插曲之变量和字符 ...

  8. Python学习最佳路线图

    python语言基础(1)Python3入门,数据类型,字符串(2)判断/循环语句,函数,命名空间,作用域(3)类与对象,继承,多态(4)tkinter界面编程(5)文件与异常,数据处理简介(6)Py ...

  9. 【转】Python学习路线

    Python最佳学习路线图 python语言基础 (1)Python3入门,数据类型,字符串 (2)判断/循环语句,函数,命名空间,作用域 (3)类与对象,继承,多态 (4)tkinter界面编程 ( ...

随机推荐

  1. go 语言字典元素删除

    package main import "fmt" func main() { /* 创建map */ countryCapitalMap := map[string]string ...

  2. shell 余弦值转角度

    范例:余弦值转角度 用 bc -l 计算,可以获得高精度: $ export cos=0.996293; echo "scale=100; a(sqrt(1-$cos^2)/$cos)*18 ...

  3. 《剑指offer》第二十八题(对称的二叉树)

    // 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...

  4. 算法笔记--数位dp

    算法笔记 这个博客写的不错:http://blog.csdn.net/wust_zzwh/article/details/52100392 数位dp的精髓是不同情况下sta变量的设置. 模板: ]; ...

  5. Vue项目骨架屏注入实践

    相比于早些年前后端代码紧密耦合.后端工程师还得写前端代码的时代,如今已发展到前后端分离,这种开发方式大大提升了前后端项目的可维护性与开发效率,让前后端工程师关注于自己的主业.然而在带来便利的同时,也带 ...

  6. box-shadow四周都有阴影

    <style> .shadow{ -webkit-box-shadow: #666 0px 0px 10px; -moz-box-shadow: #666 0px 0px 10px; bo ...

  7. Python map/reduce

    2017-07-31 18:20:59 一.map函数 map():会根据提供的函数对指定序列做映射.第一个参数 function 以参数序列中的每一个元素调用 function 函数,返回包含每次 ...

  8. 【备档】客户端自动化(主Android Appium + python

    之前做分享写的文档,备档~ 0.移动客户端自动化简介 客户端自动化测试的本质 定位对象 · 操作对象 · 校验对象 对象的定位应该是自动化测试的核心,要想操作.校验一个对象,首先应该识别这个对象. 一 ...

  9. 20170624xlVBA正则分割分类汇总

    Sub RegExpSubtotal() '声明变量 Dim Regex As Object '正则对象 Dim Dic As Object '字典对象 Dim Key As String '关键字 ...

  10. Confluence 6 使用 LDAP 授权连接一个内部目录 - 服务器设置

    名字(Name) 名字的描述将会帮助你在目录中识别.例如: Internal directory with LDAP Authentication Corporate LDAP for Authent ...