這篇文章主要介紹“怎么在你的Python平臺(tái)類游戲中放一些獎(jiǎng)勵(lì)”,在日常操作中,相信很多人在怎么在你的Python平臺(tái)類游戲中放一些獎(jiǎng)勵(lì)問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么在你的Python平臺(tái)類游戲中放一些獎(jiǎng)勵(lì)”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、浦北網(wǎng)絡(luò)推廣、成都微信小程序、浦北網(wǎng)絡(luò)營(yíng)銷、浦北企業(yè)策劃、浦北品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供浦北建站搭建服務(wù),24小時(shí)服務(wù)熱線:13518219792,官方網(wǎng)址:www.rwnh.cn
獎(jiǎng)勵(lì)和平臺(tái)非常相似,你甚至不需要一個(gè)獎(jiǎng)勵(lì)的類。你可以重用 Platform
類,并將結(jié)果稱為“獎(jiǎng)勵(lì)”。
由于獎(jiǎng)勵(lì)類型和位置可能因關(guān)卡不同而不同,如果你還沒有,請(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)、地面和敵人一樣,該組用于檢查玩家碰撞:
def loot(lvl,lloc): if lvl == 1: loot_list = pygame.sprite.Group() loot = Platform(300,ty*7,tx,ty, 'loot_1.png') loot_list.add(loot) if lvl == 2: print(lvl) 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ù)。在下面的代碼中,前三行是上下文,所以只需添加第四行:
enemy_list = Level.bad( 1, eloc )ground_list = Level.ground( 1,gloc,tx,ty )plat_list = Level.platform( 1,tx,ty )loot_list = Level.loot(1,tx,ty)
正如你現(xiàn)在所知道的,除非你把它包含在你的主循環(huán)中,否則獎(jiǎng)勵(lì)不會(huì)被顯示到屏幕上。將下面代碼示例的最后一行添加到循環(huán)中:
enemy_list.draw(world) ground_list.draw(world) plat_list.draw(world) loot_list.draw(world)
啟動(dòng)你的游戲看看會(huì)發(fā)生什么。
Loot in Python platformer
你的獎(jiǎng)勵(lì)將會(huì)顯示出來,但是當(dāng)你的玩家碰到它們時(shí),它們不會(huì)做任何事情,當(dāng)你的玩家經(jīng)過它們時(shí),它們也不會(huì)滾動(dòng)。接下來解決這些問題。
像平臺(tái)一樣,當(dāng)玩家在游戲世界中移動(dòng)時(shí),獎(jiǎng)勵(lì)必須滾動(dòng)。邏輯與平臺(tái)滾動(dòng)相同。要向前滾動(dòng)獎(jiǎng)勵(lì)物品,添加最后兩行:
for e in enemy_list: e.rect.x -= scroll for l in loot_list: l.rect.x -= scroll
要向后滾動(dòng),請(qǐng)?zhí)砑幼詈髢尚校?/p>
for e in enemy_list: e.rect.x += scroll for l in loot_list: l.rect.x += scroll
再次啟動(dòng)你的游戲,看看你的獎(jiǎng)勵(lì)物品現(xiàn)在表現(xiàn)得像在游戲世界里一樣了,而不是僅僅畫在上面。
就像平臺(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ì)象,所以看起來玩家已經(jīng)獲得了獎(jiǎng)勵(lì)物品。
在 Player
類的 update
函數(shù)中的平臺(tái)碰撞檢測(cè)之上添加以下代碼(最后一行僅用于上下文):
loot_hit_list = pygame.sprite.spritecollide(self, loot_list, False) for loot in loot_hit_list: loot_list.remove(loot) self.score += 1 print(self.score) plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False)
當(dāng)碰撞發(fā)生時(shí),你不僅要把獎(jiǎng)勵(lì)從它的組中移除,還要給你的玩家一個(gè)分?jǐn)?shù)提升。你還沒有創(chuàng)建分?jǐn)?shù)變量,所以請(qǐng)將它添加到你的玩家屬性中,該屬性是在 Player
類的 __init__
函數(shù)中創(chuàng)建的。在下面的代碼中,前兩行是上下文,所以只需添加分?jǐn)?shù)變量:
self.frame = 0 self.health = 10 self.score = 0
當(dāng)在主循環(huán)中調(diào)用 update
函數(shù)時(shí),需要包括 loot_list
:
player.gravity() player.update()
如你所見,你已經(jīng)掌握了所有的基本知識(shí)。你現(xiàn)在要做的就是用新的方式使用你所知道的。
在下一篇文章中還有一些提示,但是與此同時(shí),用你學(xué)到的知識(shí)來制作一些簡(jiǎn)單的單關(guān)卡游戲。限制你試圖創(chuàng)造的東西的范圍是很重要的,這樣你就不會(huì)埋沒自己。這也使得最終的成品看起來和感覺上更容易完成。
以下是迄今為止你為這個(gè) Python 平臺(tái)編寫的所有代碼:
#!/usr/bin/env python3# draw a world# add a player and player control# add player movement# add enemy and basic collision# add platform# add gravity# add jumping# add scrolling # GNU All-Permissive License# Copying and distribution of this file, with or without modification,# are permitted in any medium without royalty provided the copyright# notice and this notice are preserved. This file is offered as-is,# without any warranty. import pygameimport sysimport os '''Objects''' class Platform(pygame.sprite.Sprite): # x location, y location, img width, img height, img file def __init__(self,xloc,yloc,imgw,imgh,img): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(os.path.join('images',img)).convert() self.image.convert_alpha() self.rect = self.image.get_rect() self.rect.y = yloc self.rect.x = xloc class Player(pygame.sprite.Sprite): ''' Spawn a player ''' def __init__(self): pygame.sprite.Sprite.__init__(self) self.movex = 0 self.movey = 0 self.frame = 0 self.health = 10 self.collide_delta = 0 self.jump_delta = 6 self.score = 1 self.images = [] for i in range(1,9): img = pygame.image.load(os.path.join('images','hero' + str(i) + '.png')).convert() img.convert_alpha() img.set_colorkey(ALPHA) self.images.append(img) self.image = self.images[0] self.rect = self.image.get_rect() def jump(self,platform_list): self.jump_delta = 0 def gravity(self): self.movey += 3.2 # how fast player falls if self.rect.y > worldy and self.movey >= 0: self.movey = 0 self.rect.y = worldy-ty def control(self,x,y): ''' control player movement ''' self.movex += x self.movey += y def update(self): ''' Update sprite position ''' self.rect.x = self.rect.x + self.movex self.rect.y = self.rect.y + self.movey # moving left if self.movex < 0: self.frame += 1 if self.frame > ani*3: self.frame = 0 self.image = self.images[self.frame//ani] # moving right if self.movex > 0: self.frame += 1 if self.frame > ani*3: self.frame = 0 self.image = self.images[(self.frame//ani)+4] # collisions enemy_hit_list = pygame.sprite.spritecollide(self, enemy_list, False) for enemy in enemy_hit_list: self.health -= 1 #print(self.health) loot_hit_list = pygame.sprite.spritecollide(self, loot_list, False) for loot in loot_hit_list: loot_list.remove(loot) self.score += 1 print(self.score) plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False) for p in plat_hit_list: self.collide_delta = 0 # stop jumping self.movey = 0 if self.rect.y > p.rect.y: self.rect.y = p.rect.y+ty else: self.rect.y = p.rect.y-ty ground_hit_list = pygame.sprite.spritecollide(self, ground_list, False) for g in ground_hit_list: self.movey = 0 self.rect.y = worldy-ty-ty self.collide_delta = 0 # stop jumping if self.rect.y > g.rect.y: self.health -=1 print(self.health) if self.collide_delta < 6 and self.jump_delta < 6: self.jump_delta = 6*2 self.movey -= 33 # how high to jump self.collide_delta += 6 self.jump_delta += 6 class Enemy(pygame.sprite.Sprite): ''' Spawn an enemy ''' def __init__(self,x,y,img): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load(os.path.join('images',img)) self.movey = 0 #self.image.convert_alpha() #self.image.set_colorkey(ALPHA) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.counter = 0 def move(self): ''' enemy movement ''' distance = 80 speed = 8 self.movey += 3.2 if self.counter >= 0 and self.counter <= distance: self.rect.x += speed elif self.counter >= distance and self.counter <= distance*2: self.rect.x -= speed else: self.counter = 0 self.counter += 1 if not self.rect.y >= worldy-ty-ty: self.rect.y += self.movey plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False) for p in plat_hit_list: self.movey = 0 if self.rect.y > p.rect.y: self.rect.y = p.rect.y+ty else: self.rect.y = p.rect.y-ty ground_hit_list = pygame.sprite.spritecollide(self, ground_list, False) for g in ground_hit_list: self.rect.y = worldy-ty-ty class Level(): def bad(lvl,eloc): if lvl == 1: enemy = Enemy(eloc[0],eloc[1],'yeti.png') # spawn enemy enemy_list = pygame.sprite.Group() # create enemy group enemy_list.add(enemy) # add enemy to group if lvl == 2: print("Level " + str(lvl) ) return enemy_list def loot(lvl,tx,ty): if lvl == 1: loot_list = pygame.sprite.Group() loot = Platform(200,ty*7,tx,ty, 'loot_1.png') loot_list.add(loot) if lvl == 2: print(lvl) return loot_list def ground(lvl,gloc,tx,ty): ground_list = pygame.sprite.Group() i=0 if lvl == 1: while i < len(gloc): ground = Platform(gloc[i],worldy-ty,tx,ty,'ground.png') ground_list.add(ground) i=i+1 if lvl == 2: print("Level " + str(lvl) ) return ground_list def platform(lvl,tx,ty): plat_list = pygame.sprite.Group() ploc = [] i=0 if lvl == 1: ploc.append((20,worldy-ty-128,3)) ploc.append((300,worldy-ty-256,3)) ploc.append((500,worldy-ty-128,4)) while i < len(ploc): j=0 while j <= ploc[i][2]: plat = Platform((ploc[i][0]+(j*tx)),ploc[i][1],tx,ty,'ground.png') plat_list.add(plat) j=j+1 print('run' + str(i) + str(ploc[i])) i=i+1 if lvl == 2: print("Level " + str(lvl) ) return plat_list '''Setup'''worldx = 960worldy = 720 fps = 40 # frame rateani = 4 # animation cyclesclock = pygame.time.Clock()pygame.init()main = True BLUE = (25,25,200)BLACK = (23,23,23 )WHITE = (254,254,254)ALPHA = (0,255,0) world = pygame.display.set_mode([worldx,worldy])backdrop = pygame.image.load(os.path.join('images','stage.png')).convert()backdropbox = world.get_rect()player = Player() # spawn playerplayer.rect.x = 0player.rect.y = 0player_list = pygame.sprite.Group()player_list.add(player)steps = 10forwardx = 600backwardx = 230 eloc = []eloc = [200,20]gloc = []#gloc = [0,630,64,630,128,630,192,630,256,630,320,630,384,630]tx = 64 #tile sizety = 64 #tile size i=0while i <= (worldx/tx)+tx: gloc.append(i*tx) i=i+1 enemy_list = Level.bad( 1, eloc )ground_list = Level.ground( 1,gloc,tx,ty )plat_list = Level.platform( 1,tx,ty )loot_list = Level.loot(1,tx,ty) '''Main loop'''while main == True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit(); sys.exit() main = False if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT or event.key == ord('a'): print("LEFT") player.control(-steps,0) if event.key == pygame.K_RIGHT or event.key == ord('d'): print("RIGHT") player.control(steps,0) if event.key == pygame.K_UP or event.key == ord('w'): print('jump') if event.type == pygame.KEYUP: if event.key == pygame.K_LEFT or event.key == ord('a'): player.control(steps,0) if event.key == pygame.K_RIGHT or event.key == ord('d'): player.control(-steps,0) if event.key == pygame.K_UP or event.key == ord('w'): player.jump(plat_list) if event.key == ord('q'): pygame.quit() sys.exit() main = False # scroll the world forward if player.rect.x >= forwardx: scroll = player.rect.x - forwardx player.rect.x = forwardx for p in plat_list: p.rect.x -= scroll for e in enemy_list: e.rect.x -= scroll for l in loot_list: l.rect.x -= scroll # scroll the world backward if player.rect.x <= backwardx: scroll = backwardx - player.rect.x player.rect.x = backwardx for p in plat_list: p.rect.x += scroll for e in enemy_list: e.rect.x += scroll for l in loot_list: l.rect.x += scroll world.blit(backdrop, backdropbox) player.gravity() # check gravity player.update() player_list.draw(world) #refresh player position enemy_list.draw(world) # refresh enemies ground_list.draw(world) # refresh enemies plat_list.draw(world) # refresh platforms loot_list.draw(world) # refresh loot for e in enemy_list: e.move() pygame.display.flip() clock.tick(fps)
到此,關(guān)于“怎么在你的Python平臺(tái)類游戲中放一些獎(jiǎng)勵(lì)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
新聞標(biāo)題:怎么在你的Python平臺(tái)類游戲中放一些獎(jiǎng)勵(lì)
文章路徑:http://www.rwnh.cn/article38/jihdsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、、動(dòng)態(tài)網(wǎng)站、品牌網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)