标签:class font apt ext 定时 isp 版本 type bad
各位朋友大家好,我也是头一回写一篇技术类型的博客,请各位大佬多见谅。
准备工作
首先说下我们的这个项目都需要什么,比如我们下面的图片,图片不全还有3比较大我也就没有公布,主要是开始界面和结束界面个位大佬可以自行百度。
python开发环境2.7.15版本的,我个人觉得开发环境写这种小游戏还是2比较好,3我试了不是太好使。
还有一些背景音乐,我也没有放在上面但是我的代码里是有希望给位小伙伴不要介意。
这个还需要导入一个库pygame和一个random的这个一个随机函数,剩下的代码都是有注释的
图片部分(图片不全希望给我小伙伴自行百度)
下面我可能就上代码了,因为这篇文章我也当做了一个作业写的吧,可能写的不是太好因为时间紧任务多,这个还是我独立完成有点费劲,找网上的资料我也找了好多才写出来这么点东西,通过这次的实践我觉自己太菜,编程能力太弱了 应该多练练项目。
代码部分
1 #coding:utf-8 2 #导入pygame库和一些常用的函数和常量 3 #1 4 import pygame 5 from pygame.locals import * 6 import math 7 import random 8 9 #2 10 #初始化pygame,创建了一个窗口 11 pygame.init() 12 width, height = 640, 480 13 screen=pygame.display.set_mode((width, height)) 14 pygame.display.set_caption("game start") 15 keys = [False, False, False, False] 16 playerpos=[100,100] 17 acc=[0,0] 18 arrows=[] 19 #定义一个定时器,使得游戏里可以经过一段时间后就新建一只獾 20 badtimer=100 21 badtimer1=0 22 badguys=[[640,100]] 23 healthvalue=194 24 pygame.mixer.init() 25 26 #3 27 #加载本地图片 28 player = pygame.image.load("C:/Users/Administrator/Desktop/resources/resources/images/dude.png") 29 grass = pygame.image.load("C:/Users/Administrator/Desktop/resources/resources/images/grass.png") 30 castle = pygame.image.load("C:/Users/Administrator/Desktop/resources/resources/images/castle.png") 31 arrow = pygame.image.load("C:/Users/Administrator/Desktop/resources/resources/images/bullet.png") 32 #加载獾图片 33 badguyimg1 = pygame.image.load("C:/Users/Administrator/Desktop/resources/resources/images/badguy.png") 34 badguyimg=badguyimg1 35 #加载健康图片 36 healthbar = pygame.image.load("C:/Users/Administrator/Desktop/resources/resources/images/healthbar.png") 37 health = pygame.image.load("C:/Users/Administrator/Desktop/resources/resources/images/health.png") 38 #加载输赢图片 39 gameover = pygame.image.load("C:/Users/Administrator/Desktop/resources/resources/images/gameover.png") 40 youwin = pygame.image.load("C:/Users/Administrator/Desktop/resources/resources/images/youwin.png") 41 #加载声音 42 hit = pygame.mixer.Sound("C:/Users/Administrator/Desktop/resources/resources/audio/explode.wav") 43 enemy = pygame.mixer.Sound("C:/Users/Administrator/Desktop/resources/resources/audio/enemy.wav") 44 shoot = pygame.mixer.Sound("C:/Users/Administrator/Desktop/resources/resources/audio/shoot.wav") 45 hit.set_volume(0.05) 46 enemy.set_volume(0.05) 47 shoot.set_volume(0.05) 48 pygame.mixer.music.load(‘C:/Users/Administrator/Desktop/resources/resources/audio/moonlight.wav‘) 49 pygame.mixer.music.play(-1, 0.0) 50 pygame.mixer.music.set_volume(0.25) 51 52 #4 53 # 进入循环 54 running = 1 55 exitcode = 0 56 while running: 57 badtimer-=1 58 59 60 #5 设置黑色背景 61 screen.fill(0) 62 63 #6 64 # 循环画上背景 65 for x in range(width/grass.get_width()+1): 66 for y in range(height/grass.get_height()+1): 67 screen.blit(grass,(x*100,y*100)) 68 #画上四个城堡 69 screen.blit(castle,(0,30)) 70 screen.blit(castle,(0,135)) 71 screen.blit(castle,(0,240)) 72 screen.blit(castle,(0,345 )) 73 #画上小兔子 74 #玩家旋转兔子 75 position = pygame.mouse.get_pos() 76 angle = math.atan2(position[1]-(playerpos[1]+32),position[0]-(playerpos[0]+26)) 77 playerrot = pygame.transform.rotate(player, 360-angle*57.29) 78 playerpos1 = (playerpos[0]-playerrot.get_rect().width/2, playerpos[1]-playerrot.get_rect().height/2) 79 screen.blit(playerrot, playerpos1) 80 #画出箭头 81 for bullet in arrows: 82 index=0 83 velx=math.cos(bullet[0])*10 84 vely=math.sin(bullet[0])*10 85 bullet[1]+=velx 86 bullet[2]+=vely 87 if bullet[1]<-64 or bullet[1]>640 or bullet[2]<-64 or bullet[2]>480: 88 arrows.pop(index) 89 index+=1 90 for projectile in arrows: 91 arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29) 92 screen.blit(arrow1, (projectile[1], projectile[2])) 93 #画上獾 94 if badtimer==0: 95 badguys.append([640, random.randint(50,430)]) 96 badtimer=100-(badtimer1*2) 97 if badtimer1>=35: 98 badtimer1=35 99 else: 100 badtimer1+=5 101 index=0 102 for badguy in badguys: 103 if badguy[0]<-64: 104 badguys.pop(index) 105 badguy[0]-=7 106 #獾冲过来并且在碰到城堡的时候会消失。尽管你看不到,獾实际上会降低你的健康值。 107 badrect=pygame.Rect(badguyimg.get_rect()) 108 badrect.top=badguy[1] 109 badrect.left=badguy[0] 110 if badrect.left<64: 111 hit.play() 112 healthvalue -= random.randint(5,20) 113 badguys.pop(index) 114 #检查箭头 115 index1=0 116 for bullet in arrows: 117 bullrect=pygame.Rect(arrow.get_rect()) 118 bullrect.left=bullet[1] 119 bullrect.top=bullet[2] 120 if badrect.colliderect(bullrect): 121 enemy.play() 122 acc[0]+=1 123 badguys.pop(index) 124 arrows.pop(index1) 125 index1+=1 126 127 index+=1 128 for badguy in badguys: 129 screen.blit(badguyimg, badguy) 130 131 # 添加时钟用来计时 132 font = pygame.font.Font(None, 24) 133 survivedtext = font.render(str((90000-pygame.time.get_ticks())/60000)+":"+str((90000-pygame.time.get_ticks())/1000%60).zfill(2), True, (0,0,0)) 134 textRect = survivedtext.get_rect() 135 textRect.topright=[635,5] 136 screen.blit(survivedtext, textRect) 137 #添加生命值显示 138 screen.blit(healthbar, (5,5)) 139 for health1 in range(healthvalue): 140 screen.blit(health, (health1+8,8)) 141 #7 142 # 刷新屏幕 143 pygame.display.flip() 144 145 #8设置游戏退出条件 146 for event in pygame.event.get(): 147 if event.type == QUIT: 148 exit() 149 #定义玩家wasd四个按键用来控制方向 150 if event.type == pygame.KEYDOWN: 151 if event.key==K_w: 152 keys[0]=True 153 elif event.key==K_a: 154 keys[1]=True 155 elif event.key==K_s: 156 keys[2]=True 157 elif event.key==K_d: 158 keys[3]=True 159 if event.type == pygame.KEYUP: 160 if event.key==pygame.K_w: 161 keys[0]=False 162 elif event.key==pygame.K_a: 163 keys[1]=False 164 elif event.key==pygame.K_s: 165 keys[2]=False 166 elif event.key==pygame.K_d: 167 keys[3]=False 168 #玩家移动按键 169 if keys[0]: 170 playerpos[1]-=5 171 elif keys[2]: 172 playerpos[1]+=5 173 if keys[1]: 174 playerpos[0]-=5 175 elif keys[3]: 176 playerpos[0]+=5 177 #跟踪箭头 178 if event.type==pygame.MOUSEBUTTONDOWN: 179 shoot.play() 180 position=pygame.mouse.get_pos() 181 acc[1]+=1 182 arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32]) 183 184 185 #检查输赢 186 if pygame.time.get_ticks()>=90000: 187 running=0 188 exitcode=1 189 if healthvalue<=0: 190 running=0 191 exitcode=0 192 if acc[1]!=0: 193 accuracy=acc[0]*1.0/acc[1]*100 194 else: 195 accuracy=0 196 # 输赢显示 197 if exitcode==0: 198 pygame.font.init() 199 font = pygame.font.Font(None, 24) 200 text = font.render("Accuracy: "+str(accuracy)+"%", True, (255,0,0)) 201 textRect = text.get_rect() 202 textRect.centerx = screen.get_rect().centerx 203 textRect.centery = screen.get_rect().centery+24 204 screen.blit(gameover, (0,0)) 205 screen.blit(text, textRect) 206 else: 207 pygame.font.init() 208 font = pygame.font.Font(None, 24) 209 text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0)) 210 textRect = text.get_rect() 211 textRect.centerx = screen.get_rect().centerx 212 textRect.centery = screen.get_rect().centery+24 213 screen.blit(youwin, (0,0)) 214 screen.blit(text, textRect) 215 while 1: 216 for event in pygame.event.get(): 217 if event.type == pygame.QUIT: 218 pygame.quit() 219 exit(0) 220 pygame.display.flip()
结语:可能这个项目写的还是不够完美因为我最开始的构思是双人的小游戏,让这个老鼠也能动,但是个人的原因没有那个编程的水平所以没有把这个项目完成,我也做一个自我的检讨吧,还是水平不够啊,我后来做了一个反思吧,还是多学习提升自己的能力吧,这可能是一个简单的游戏编程,之后我也会跟新一些安全方面的知识,感谢给我的小伙伴们的支持。
标签:class font apt ext 定时 isp 版本 type bad
原文地址:https://www.cnblogs.com/lyq123/p/9879129.html