Download presentation
Presentation is loading. Please wait.
1
電腦遊戲設計-第十組 指導老師:江清水 資三B 01156228 謝孟穎 資三B 01156233 陳沛蓁 資三B 01156237 謝家蓉
2
初始化 pygame.init() width, height = 640, 480
screen=pygame.display.set_mode((width, height)) # 背景顏色 backgroundColor = ( 255 , 255 , 255 ) # 移動玩家 keys = [False, False, False, False] playerpos=[100,100] # 攻擊的臉 acc=[0,0] arrows=[]
3
壞蛋 #計時器,壞人出現的頻率 badtimer=100 badtimer1=0 #壞人位置 badguys=[[640,100]]
#生命值 healthvalue=194
4
壞蛋 # 載入圖片 player = pygame.image.load("player.png")
arrow = pygame.image.load("stone.png") badguyimg1 = pygame.image.load("monster.png") badguyimg=badguyimg1 backgroung = pygame.image.load("background.png") castle = pygame.image.load("honey.png") healthbar = pygame.image.load("healthbar.png") health = pygame.image.load("bar.png") gameover = pygame.image.load("gameover.png") youwin = pygame.image.load("youwin.png")
5
壞蛋 # 退出 def terminate(): pygame.quit() sys.exit() # 循環 running = 1
exitcode = 0 while running: badtimer-=1 # 清空螢幕 screen.fill(0) #黃色背景 screen.blit(backgroung,(0,0)) #畫蜂蜜罐 screen.blit(castle,(0,30)) screen.blit(castle,(0,135)) screen.blit(castle,(0,240)) screen.blit(castle,(0,345 ))
6
改變玩家的位置和旋轉 #偵測滑鼠的位置 position = pygame.mouse.get_pos()
# math.atan2(y, x),playerpos[1] = y 座標 , Return atan(y / x) angle = math.atan2(position[1]-playerpos[1],position[0]-playerpos[0]) #玩家旋轉的角度,rotate ( 要移動的東西, 角度 ),弧度是57.29 = 360/2π 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)
7
畫攻擊的頭 for bullet in arrows: index=0 #10是頭的速度
velx=math.cos(bullet[0])*10 vely=math.sin(bullet[0])*10 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: #rotate(東西,角度) arrow1 = pygame.transform.rotate(arrow, 360-projectile[0]*57.29) screen.blit(arrow1, (projectile[1], projectile[2]))
8
創造壞人 # 檢查timer的值,若為 0 則加壞人 if badtimer==0:
badguys.append([640, random.randint(50,430)]) # 重新計時,用100減去壞人出現的次數,所以badtimer值會越來越小,壞人出現的頻率提高 badtimer=100-(badtimer1*2) # 如果出現的次數大於35就把值固定在35,不然出現頻率會太高 if badtimer1>=35: badtimer1=35 else: badtimer1+=1 index=0 # 更新x的值
9
創造壞人 for badguy in badguys: # 檢查是否超出螢幕外,是的話就刪除他 if badguy[0]<-64:
badguys.pop(index) badguy[0]-=7 # 壞人攻擊蜂蜜罐 badrect=pygame.Rect(badguyimg.get_rect()) badrect.top=badguy[1] badrect.left=badguy[0] # 如果壞人已經到螢幕左邊了,那生命值會減少,然後刪除他 if badrect.left<64: healthvalue -= random.randint(5,20) # 檢查是否有撞到蜂蜜罐
10
創造壞人 index1=0 for bullet in arrows:
bullrect=pygame.Rect(arrow.get_rect()) bullrect.left=bullet[1] bullrect.top=bullet[2] if badrect.colliderect(bullrect): acc[0]+=1 badguys.pop(index) arrows.pop(index1) index1+=1 # 下一個壞人 index+=1 # 畫出壞人 for badguy in badguys: screen.blit(badguyimg, badguy)
11
攻擊 #當滑鼠被按下時 if event.type==pygame.MOUSEBUTTONDOWN: #取得滑鼠位置
position=pygame.mouse.get_pos() acc[1]+=1 #把旋轉的角度,x,y座標存入arrows陣列 arrows.append([math.atan2(position[1]-(playerpos1[1]+32),position[0]-(playerpos1[0]+26)),playerpos1[0]+32,playerpos1[1]+32])
12
壞蛋出現&打壞蛋
13
計時器&生命值 計時器 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) 生命值 (#生命值 healthvalue=194) screen.blit(healthbar, (5,5)) for health1 in range(healthvalue): screen.blit(health, (health1+8,8))
14
計時器&生命值
15
Win or Lose #時間結束了表示贏了 if pygame.time.get_ticks()>=90000: running=0
exitcode=1 #生命值是負的表示輸了 if healthvalue<=0: exitcode=0
16
Win or Lose 輸的畫面 if exitcode==0: pygame.font.init()
font = pygame.font.Font(None, 24) screen.blit(gameover, (0,0))
17
Win or Lose 贏 else: pygame.font.init()
font = pygame.font.Font(None, 24) screen.blit(youwin, (0,0))
18
Win or Lose 讓遊戲停止 while 1: for event in pygame.event.get():
if event.type == pygame.QUIT: pygame.quit() exit(0) pygame.display.flip()
19
END
Similar presentations