日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
在你的Python平臺(tái)類游戲中放一些獎(jiǎng)勵(lì)

這部分是關(guān)于在使用 Python 的 Pygame 模塊開(kāi)發(fā)的視頻游戲總給你的玩家提供收集的寶物和經(jīng)驗(yàn)值的內(nèi)容。

從網(wǎng)站建設(shè)到定制行業(yè)解決方案,為提供做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)服務(wù)體系,各種行業(yè)企業(yè)客戶提供網(wǎng)站建設(shè)解決方案,助力業(yè)務(wù)快速發(fā)展。創(chuàng)新互聯(lián)將不斷加快創(chuàng)新步伐,提供優(yōu)質(zhì)的建站服務(wù)。

這是正在進(jìn)行的關(guān)于使用 Python 3 的 Pygame 模塊創(chuàng)建視頻游戲的系列文章的第十部分。以前的文章有:

  • 通過(guò)構(gòu)建一個(gè)簡(jiǎn)單的擲骰子游戲去學(xué)習(xí)怎么用 Python 編程
  • 使用 Python 和 Pygame 模塊構(gòu)建一個(gè)游戲框架
  • 如何在你的 Python 游戲中添加一個(gè)玩家
  • 用 Pygame 使你的游戲角色移動(dòng)起來(lái)
  • 如何向你的 Python 游戲中添加一個(gè)敵人
  • 在 Pygame 游戲中放置平臺(tái)
  • 在你的 Python 游戲中模擬引力
  • 為你的 Python 平臺(tái)類游戲添加跳躍功能
  • 使你的 Python 游戲玩家能夠向前和向后跑

如果你已經(jīng)閱讀了本系列的前幾篇文章,那么你已經(jīng)了解了編寫游戲的所有基礎(chǔ)知識(shí)?,F(xiàn)在你可以在這些基礎(chǔ)上,創(chuàng)造一個(gè)全功能的游戲。當(dāng)你第一次學(xué)習(xí)時(shí),遵循本系列代碼示例,這樣的“用例”是有幫助的,但是,用例也會(huì)約束你?,F(xiàn)在是時(shí)候運(yùn)用你學(xué)到的知識(shí),以新的方式應(yīng)用它們了。

如果說(shuō),說(shuō)起來(lái)容易做起來(lái)難,這篇文章展示了一個(gè)如何將你已經(jīng)了解的內(nèi)容用于新目的的例子中。具體來(lái)說(shuō),就是它涵蓋了如何使用你以前的課程中已經(jīng)了解到的來(lái)實(shí)現(xiàn)獎(jiǎng)勵(lì)系統(tǒng)。

在大多數(shù)電子游戲中,你有機(jī)會(huì)在游戲世界中獲得“獎(jiǎng)勵(lì)”或收集到寶物和其他物品。獎(jiǎng)勵(lì)通常會(huì)增加你的分?jǐn)?shù)或者你的生命值,或者為你的下一次任務(wù)提供信息。

游戲中包含的獎(jiǎng)勵(lì)類似于編程平臺(tái)。像平臺(tái)一樣,獎(jiǎng)勵(lì)沒(méi)有用戶控制,隨著游戲世界的滾動(dòng)進(jìn)行,并且必須檢查與玩家的碰撞。

創(chuàng)建獎(jiǎng)勵(lì)函數(shù)

獎(jiǎng)勵(lì)和平臺(tái)非常相似,你甚至不需要一個(gè)獎(jiǎng)勵(lì)的類。你可以重用 Platform 類,并將結(jié)果稱為“獎(jiǎng)勵(lì)”。

由于獎(jiǎng)勵(lì)類型和位置可能因關(guān)卡不同而不同,如果你還沒(méi)有,請(qǐng)?jiān)谀愕?Level 中創(chuàng)建一個(gè)名為 loot 的新函數(shù)。因?yàn)楠?jiǎng)勵(lì)物品不是平臺(tái),你也必須創(chuàng)建一個(gè)新的 loot_list 組,然后添加獎(jiǎng)勵(lì)物品。與平臺(tái)、地面和敵人一樣,該組用于檢查玩家碰撞:

 
 
 
 
  1. def loot(lvl,lloc):
  2. if lvl == 1:
  3. loot_list = pygame.sprite.Group()
  4. loot = Platform(300,ty*7,tx,ty, 'loot_1.png')
  5. loot_list.add(loot)
  6. if lvl == 2:
  7. print(lvl)
  8. return loot_list

你可以隨意添加任意數(shù)量的獎(jiǎng)勵(lì)對(duì)象;記住把每一個(gè)都加到你的獎(jiǎng)勵(lì)清單上。Platform 類的參數(shù)是獎(jiǎng)勵(lì)圖標(biāo)的 X 位置、Y 位置、寬度和高度(通常讓你的獎(jiǎng)勵(lì)精靈保持和所有其他方塊一樣的大小最為簡(jiǎn)單),以及你想要用作的獎(jiǎng)勵(lì)的圖片。獎(jiǎng)勵(lì)的放置可以和貼圖平臺(tái)一樣復(fù)雜,所以使用創(chuàng)建關(guān)卡時(shí)需要的關(guān)卡設(shè)計(jì)文檔。

在腳本的設(shè)置部分調(diào)用新的獎(jiǎng)勵(lì)函數(shù)。在下面的代碼中,前三行是上下文,所以只需添加第四行:

 
 
 
 
  1. enemy_list = Level.bad( 1, eloc )
  2. ground_list = Level.ground( 1,gloc,tx,ty )
  3. plat_list = Level.platform( 1,tx,ty )
  4. loot_list = Level.loot(1,tx,ty)

正如你現(xiàn)在所知道的,除非你把它包含在你的主循環(huán)中,否則獎(jiǎng)勵(lì)不會(huì)被顯示到屏幕上。將下面代碼示例的最后一行添加到循環(huán)中:

 
 
 
 
  1.     enemy_list.draw(world)
  2.     ground_list.draw(world)
  3.     plat_list.draw(world)
  4.     loot_list.draw(world)

啟動(dòng)你的游戲看看會(huì)發(fā)生什么。

Loot in Python platformer

你的獎(jiǎng)勵(lì)將會(huì)顯示出來(lái),但是當(dāng)你的玩家碰到它們時(shí),它們不會(huì)做任何事情,當(dāng)你的玩家經(jīng)過(guò)它們時(shí),它們也不會(huì)滾動(dòng)。接下來(lái)解決這些問(wèn)題。

滾動(dòng)獎(jiǎng)勵(lì)

像平臺(tái)一樣,當(dāng)玩家在游戲世界中移動(dòng)時(shí),獎(jiǎng)勵(lì)必須滾動(dòng)。邏輯與平臺(tái)滾動(dòng)相同。要向前滾動(dòng)獎(jiǎng)勵(lì)物品,添加最后兩行:

 
 
 
 
  1.         for e in enemy_list:
  2.             e.rect.x -= scroll
  3.         for l in loot_list:
  4.             l.rect.x -= scroll

要向后滾動(dòng),請(qǐng)?zhí)砑幼詈髢尚校?/p>

 
 
 
 
  1.         for e in enemy_list:
  2.             e.rect.x += scroll
  3.         for l in loot_list:
  4.             l.rect.x += scroll

再次啟動(dòng)你的游戲,看看你的獎(jiǎng)勵(lì)物品現(xiàn)在表現(xiàn)得像在游戲世界里一樣了,而不是僅僅畫在上面。

檢測(cè)碰撞

就像平臺(tái)和敵人一樣,你可以檢查獎(jiǎng)勵(lì)物品和玩家之間的碰撞。邏輯與其他碰撞相同,除了撞擊不會(huì)(必然)影響重力或生命值。取而代之的是,命中會(huì)導(dǎo)致獎(jiǎng)勵(lì)物品會(huì)消失并增加玩家的分?jǐn)?shù)。

當(dāng)你的玩家觸摸到一個(gè)獎(jiǎng)勵(lì)對(duì)象時(shí),你可以從 loot_list 中移除該對(duì)象。這意味著當(dāng)你的主循環(huán)在 loot_list 中重繪所有獎(jiǎng)勵(lì)物品時(shí),它不會(huì)重繪那個(gè)特定的對(duì)象,所以看起來(lái)玩家已經(jīng)獲得了獎(jiǎng)勵(lì)物品。

Player 類的 update 函數(shù)中的平臺(tái)碰撞檢測(cè)之上添加以下代碼(最后一行僅用于上下文):

 
 
 
 
  1.                 loot_hit_list = pygame.sprite.spritecollide(self, loot_list, False)
  2.                 for loot in loot_hit_list:
  3.                         loot_list.remove(loot)
  4.                         self.score += 1
  5.                 print(self.score)
  6.  
  7.         plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False)

當(dāng)碰撞發(fā)生時(shí),你不僅要把獎(jiǎng)勵(lì)從它的組中移除,還要給你的玩家一個(gè)分?jǐn)?shù)提升。你還沒(méi)有創(chuàng)建分?jǐn)?shù)變量,所以請(qǐng)將它添加到你的玩家屬性中,該屬性是在 Player 類的 __init__ 函數(shù)中創(chuàng)建的。在下面的代碼中,前兩行是上下文,所以只需添加分?jǐn)?shù)變量:

 
 
 
 
  1.         self.frame = 0
  2.         self.health = 10
  3.         self.score = 0

當(dāng)在主循環(huán)中調(diào)用 update 函數(shù)時(shí),需要包括 loot_list

 
 
 
 
  1.         player.gravity()
  2.         player.update()

如你所見(jiàn),你已經(jīng)掌握了所有的基本知識(shí)。你現(xiàn)在要做的就是用新的方式使用你所知道的。

在下一篇文章中還有一些提示,但是與此同時(shí),用你學(xué)到的知識(shí)來(lái)制作一些簡(jiǎn)單的單關(guān)卡游戲。限制你試圖創(chuàng)造的東西的范圍是很重要的,這樣你就不會(huì)埋沒(méi)自己。這也使得最終的成品看起來(lái)和感覺(jué)上更容易完成。

以下是迄今為止你為這個(gè) Python 平臺(tái)編寫的所有代碼:

 
 
 
 
  1. #!/usr/bin/env python3
  2. # draw a world
  3. # add a player and player control
  4. # add player movement
  5. # add enemy and basic collision
  6. # add platform
  7. # add gravity
  8. # add jumping
  9. # add scrolling
  10. # GNU All-Permissive License
  11. # Copying and distribution of this file, with or without modification,
  12. # are permitted in any medium without royalty provided the copyright
  13. # notice and this notice are preserved. This file is offered as-is,
  14. # without any warranty.
  15. import pygame
  16. import sys
  17. import os
  18. '''
  19. Objects
  20. '''
  21. class Platform(pygame.sprite.Sprite):
  22. # x location, y location, img width, img height, img file
  23. def __init__(self,xloc,yloc,imgw,imgh,img):
  24. pygame.sprite.Sprite.__init__(self)
  25. self.image = pygame.image.load(os.path.join('images',img)).convert()
  26. self.image.convert_alpha()
  27. self.rect = self.image.get_rect()
  28. self.rect.y = yloc
  29. self.rect.x = xloc
  30. class Player(pygame.sprite.Sprite):
  31. '''
  32. Spawn a player
  33. '''
  34. def __init__(self):
  35. pygame.sprite.Sprite.__init__(self)
  36. self.movex = 0
  37. self.movey = 0
  38. self.frame = 0
  39. self.health = 10
  40. self.collide_delta = 0
  41. self.jump_delta = 6
  42. self.score = 1
  43. self.images = []
  44. for i in range(1,9):
  45. img = pygame.image.load(os.path.join('images','hero' + str(i) + '.png')).convert()
  46. img.convert_alpha()
  47. img.set_colorkey(ALPHA)
  48. self.images.append(img)
  49. self.image = self.images[0]
  50. self.rect = self.image.get_rect()
  51. def jump(self,platform_list):
  52. self.jump_delta = 0
  53. def gravity(self):
  54. self.movey += 3.2 # how fast player falls
  55. if self.rect.y > worldy and self.movey >= 0:
  56. self.movey = 0
  57. self.rect.y = worldy-ty
  58. def control(self,x,y):
  59. '''
  60. control player movement
  61. '''
  62. self.movex += x
  63. self.movey += y
  64. def update(self):
  65. '''
  66. Update sprite position
  67. '''
  68. self.rect.x = self.rect.x + self.movex
  69. self.rect.y = self.rect.y + self.movey
  70. # moving left
  71. if self.movex < 0:
  72. self.frame += 1
  73. if self.frame > ani*3:
  74. self.frame = 0
  75. self.image = self.images[self.frame//ani]
  76. # moving right
  77. if self.movex > 0:
  78. self.frame += 1
  79. if self.frame > ani*3:
  80. self.frame = 0
  81. self.image = self.images[(self.frame//ani)+4]
  82. # collisions
  83. enemy_hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
  84. for enemy in enemy_hit_list:
  85. self.health -= 1
  86. #print(self.health)
  87. loot_hit_list = pygame.sprite.spritecollide(self, loot_list, False)
  88. for loot in loot_hit_list:
  89. loot_list.remove(loot)
  90. self.score += 1
  91. print(self.score)
  92. plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False)
  93. for p in plat_hit_list:
  94. self.collide_delta = 0 # stop jumping
  95. self.movey = 0
  96. if self.rect.y > p.rect.y:
  97. self.rect.y = p.rect.y+ty
  98. else:
  99. self.rect.y = p.rect.y-ty
  100. ground_hit_list = pygame.sprite.spritecollide(self, ground_list, False)
  101. for g in ground_hit_list:
  102. self.movey = 0
  103. self.rect.y = worldy-ty-ty
  104. self.collide_delta = 0 # stop jumping
  105. if self.rect.y > g.rect.y:
  106. self.health -=1
  107. print(self.health)
  108. if self.collide_delta < 6 and self.jump_delta < 6:
  109. self.jump_delta = 6*2
  110. self.movey -= 33 # how high to jump
  111. self.collide_delta += 6
  112. self.jump_delta += 6
  113. class Enemy(pygame.sprite.Sprite):
  114. '''
  115. Spawn an enemy
  116. '''
  117. def __init__(self,x,y,img):
  118. pygame.sprite.Sprite.__init__(self)
  119. self.image = pygame.image.load(os.path.join('images',img))
  120. self.movey = 0
  121. #self.image.convert_alpha()
  122. #self.image.set_colorkey(ALPHA)
  123. self.rect = self.image.get_rect()
  124. self.rect.x = x
  125. self.rect.y = y
  126. self.counter = 0
  127. def move(self):
  128. '''
  129. enemy movement
  130. '''
  131. distance = 80
  132. speed = 8
  133. self.movey += 3.2
  134. if self.counter >= 0 and self.counter <= distance:
  135. self.rect.x += speed
  136. elif self.counter >= distance and self.counter <= distance*2:
  137. self.rect.x -= speed
  138. else:
  139. self.counter = 0
  140. self.counter += 1
  141. if not self.rect.y >= worldy-ty-ty:
  142. self.rect.y += self.movey
  143. plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False)
  144. for p in plat_hit_list:
  145. self.movey = 0
  146. if self.rect.y > p.rect.y:
  147. self.rect.y = p.rect.y+ty
  148. else:
  149. self.rect.y = p.rect.y-ty
  150. ground_hit_list = pygame.sprite.spritecollide(self, ground_list, False)
  151. for g in ground_hit_list:
  152. self.rect.y = worldy-ty-ty
  153. class Level():
  154. def bad(lvl,eloc):
  155. if lvl == 1:
  156. enemy = Enemy(eloc[0],eloc[1],'yeti.png') # spawn enemy
  157. enemy_list = pygame.sprite.Group() # create enemy group
  158. enemy_list.add(enemy) # add enemy to group
  159. if lvl == 2:
  160. print("Level " + str(lvl) )
  161. return enemy_list
  162. def loot(lvl,tx,ty):
  163. if lvl == 1:
  164. loot_list = pygame.sprite.Group()
  165. loot = Platform(200,ty*7,tx,ty, 'loot_1.png')
  166. loot_list.add(loot)
  167. if lvl == 2:
  168. print(lvl)
  169. return loot_list
  170. def ground(lvl,gloc,tx,ty):
  171. ground_list = pygame.sprite.Group()
  172. i=0
  173. if lvl == 1:
  174. while i < len(gloc):
  175. ground = Platform(gloc[i],worldy-ty,tx,ty,'ground.png')
  176. ground_list.add(ground)
  177. i=i+1
  178. if lvl == 2:
  179. print("Level " + str(lvl) )
  180. return ground_list
  181. def platform(lvl,tx,ty):
  182. plat_list = pygame.sprite.Group()
  183. ploc = []
  184. i=0
  185. if lvl == 1:
  186. ploc.append((20,worldy-ty-128,3))
  187. ploc.append((300,worldy-ty-256,3))
  188. ploc.append((500,worldy-ty-128,4))
  189. while i < len(ploc):
  190. j=0
  191. while j <= ploc[i][2]:
  192. plat = Platform((ploc[i][0]+(j*tx)),ploc[i][1],tx,ty,'ground.png')
  193. plat_list.add(plat)
  194. j=j+1
  195. print('run' + str(i) + str(ploc[i]))
  196. i=i+1
  197. if lvl == 2:
  198. print("Level " + str(lvl) )
  199. return plat_list
  200. '''
  201. Setup
  202. '''
  203. worldx = 960
  204. worldy = 720
  205. fps = 40 # frame rate
  206. ani = 4 # animation cycles
  207. clock = pygame.time.Clock()
  208. pygame.init()
  209. main = True
  210. BLUE = (25,25,200)
  211. BLACK = (23,23,23 )
  212. WHITE = (254,254,254)
  213. ALPHA = (0,255,0)
  214. world = pygame.display.set_mode([worldx,worldy])
  215. backdrop = pygame.image.load(os.path.join('images','stage.png')).convert()
  216. backdropbox = world.get_rect()
  217. player = Player() # spawn player
  218. player.rect.x = 0
  219. player.rect.y = 0
  220. player_list = pygame.sprite.Group()
  221. player_list.add(player)
  222. steps = 10
  223. forwardx = 600
  224. backwardx = 230
  225. eloc = []
  226. eloc = [200,20]
  227. gloc = []
  228. #gloc = [0,630,64,630,128,630,192,630,256,630,320,630,384,630]
  229. tx = 64 #tile size
  230. ty = 64 #tile size
  231. i=0
  232. while i <= (worldx/tx)+tx:
  233. gloc.append(i*tx)
  234. i=i+1
  235. enemy_list = Level.bad( 1, eloc )
  236. ground_list = Level.ground( 1,gloc,tx,ty )
  237. plat_list = Level.platform( 1,tx,ty )
  238. loot_list = Level.loot(1,tx,ty)
  239. '''
  240. Main loop
  241. '''
  242. while main == True:
  243. for event in pygame.event.get():
  244. if event.type == pygame.QUIT:
  245. pygame.quit(); sys.exit()
  246. main = False
  247. if event.type == pygame.KEYDOWN:
  248. if event.key == pygame.K_LEFT or event.key == ord('a'):
  249. print("LEFT")
  250. player.control(-steps,0)
  251. if event.key == pygame.K_RIGHT or event.key == ord('d'):
  252. print("RIGHT")
  253. player.control(steps,0)
  254. if event.key == pygame.K_UP or event.key == ord('w'):
  255. print('jump')
  256. if event.type == pygame.KEYUP:
  257. if event.key == pygame.K_LEFT or event.key == ord('a'):
  258. player.control(steps,0)
  259. if event.key == pygame.K_RIGHT or event.key == ord('d'):
  260. player.control(-steps,0)
  261. if event.key == pygame.K_UP or event.key == ord('w'):
  262. player.jump(plat_list)
  263. if event.key == ord('q'):
  264. pygame.quit()
  265. sys.exit()
  266. main = False
  267. # scroll the world forward
  268. if player.rect.x >= forwardx:
  269. scroll = player.rect.x - forwardx
  270. player.rect.x = forwardx
  271. for p in plat_list:
  272. p.rect.x -= scroll
  273. for e in enemy_list:
  274. e.rect.x -= scroll
  275. for l in loot_list:
  276. l.rect.x -= scroll
  277. # scroll the world backward
  278. if player.rect.x <= backwardx:
  279. scroll = backwardx - player.rect.x
  280. player.rect.x = backwardx
  281. for p in plat_list:
  282. p.rect.x += scroll
  283. for e in enemy_list:
  284. e.rect.x += scroll
  285. for l in loot_list:
  286. l.rect.x += scroll
  287. world.blit(backdrop, backdropbox)
  288. player.gravity() # check gravity
  289. player.update()
  290. player_list.draw(world) #refresh player position
  291. enemy_list.draw(world) # refresh enemies
  292. ground_list.draw(world) # refresh enemies
  293. plat_list.draw(world) # refresh platforms
  294. loot_list.draw(world) # refresh loot
  295. for e in enemy_list:
  296. e.move()
  297. pygame.display.flip()
  298. clock.tick(fps)

當(dāng)前題目:在你的Python平臺(tái)類游戲中放一些獎(jiǎng)勵(lì)
本文鏈接:http://www.5511xx.com/article/cogeego.html