In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to put some rewards in your Python platform games". In daily operation, I believe many people have doubts about how to put some rewards in your Python platform games. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to put some rewards in your Python platform games". Next, please follow the editor to study!
Create a reward function
Rewards are so similar to platforms that you don't even need a reward class. You can reuse the Platform class and call the result a "reward".
Since the type and location of awards may vary from level to level, if you don't already have one, please create a new function called loot in your Level. Because the reward item is not a platform, you must also create a new loot_list group, and then add the reward item. Like platforms, ground, and enemies, this group is used to check player collisions:
Def loot (lvl,lloc): if lvl = = 1: loot_list = pygame.sprite.Group () loot = Platform
You can add as many reward objects as you want; remember to add each to your reward list. The arguments to the Platform class are the X position, Y position, width, and height of the reward icon (it is usually easiest to keep your reward wizard the same size as all other squares), as well as the image you want to use as the reward. The placement of awards can be as complex as the mapping platform, so use the level design documents you need to create levels.
Call the new reward function in the settings section of the script. In the following code, the first three lines are the context, so just add the fourth line:
Enemy_list = Level.bad (1, eloc) ground_list = Level.ground (1Magna glocje TX Magnty) plat_list = Level.platform (1m TX Magnty) loot_list = Level.loot (1m TX Magnty)
As you now know, the reward will not be displayed on the screen unless you include it in your main loop. Add the last line of the following code example to the loop:
Enemy_list.draw (world) ground_list.draw (world) plat_list.draw (world) loot_list.draw (world)
Start your game and see what happens.
Loot in Python platformer
Your rewards will be displayed, but they won't do anything when your players touch them, and they won't scroll when your players pass by them. Next, solve these problems.
Rolling reward
Like the platform, the reward must scroll as the player moves in the game world. The logic is the same as platform scrolling. To scroll the award forward, add the last two lines:
For e in enemy_list: e.rect.x-= scroll for l in loot_list: l.rect.x-= scroll
To scroll backward, add the last two lines:
For e in enemy_list: e.rect.x + = scroll for l in loot_list: l.rect.x + = scroll
Start your game again and see that your rewards now behave as if they were in the game world, rather than just drawing on them.
Detect collision
Just like platforms and enemies, you can check for collisions between rewards and players. The logic is the same as other collisions, except that the impact does not (necessarily) affect gravity or health. Instead, a hit will cause the reward item to disappear and increase the player's score.
When your player touches a reward object, you can remove it from the loot_list. This means that when your main loop redraws all reward items in loot_list, it does not redraw that particular object, so it looks like the player has already received the reward item.
Add the following code to the platform collision detection in the update function of the Player class (the last line is for context only):
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)
When a collision occurs, you not only remove the reward from its group, but also give your player a score increase. You haven't created a score variable yet, so please add it to your player property, which is created in the _ _ init__ function of the Player class. In the following code, the first two lines are the context, so just add the score variable:
Self.frame = 0 self.health = 10 self.score = 0
When calling the update function in the main loop, you need to include loot_list:
Player.gravity () player.update ()
As you can see, you have mastered all the basic knowledge. All you have to do now is to use what you know in new ways.
There are some tips in the next article, but at the same time, use what you have learned to make some simple single-level games. It's important to limit the scope of what you're trying to create so that you don't bury yourself. This also makes the final product look and feel easier to complete.
Here's all the code you've written for the Python platform so far:
#! / 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 Magi 9): img = pygame.image.load ('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 pin 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 and self.counter = 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 (200recorder TX loot_list.add, '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 () iTun0 if lvl = = 1: while I < len (gloc): ground = Platform (gloci [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 = [] iTun0 if lvl = = 1: ploc.append ((20) Worldy-ty-128,3)) ploc.append ((300) ploc.append ((500) len (ploc): juni0 while j
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.