如何在Python 游戏中模拟引力
学习如何使用Python的Pygame模块编程电脑游戏,并开始操作引力。
真实的世界充满了运动和生活。物理学使得真实的生活如此忙碌和动态。物理学是物质在空间中运动的方式。既然一个电脑游戏世界没有物质,它也就没有物理学规律,使用游戏程序员不得不模拟物理学。
从大多数电脑游戏来说,这里基本上仅有两个方面的物理学是重要的:引力和碰撞。
当你添加一个敌人到你的游戏中时,你实现了一些碰撞检测,但是这篇文章要添加更多的东西,因为引力需要碰撞检测。想想为什么引力可能涉及碰撞。如果你不能想到任何原因,不要担心——它会随着你开发示例代码工作而且显然。
在真实世界中的引力是有质量的物体来相互吸引的倾向性。物体(质量)越大,它施加越大的引力作用。在电脑游戏物理学中,你不必创建质量足够大的物体来证明引力的正确;你可以在电脑游戏世界本身中仅编程一个物体落向假设的最大的对象的倾向。
添加一个引力函数
记住你的玩家已经有了一个决定动作的属性。使用这个属性来将玩家精灵拉向屏幕底部。
在Pygame中,较高的数字更接近屏幕的底部边缘。
在真实的世界中,引力影响一切。然而,在平台游戏中,引力是有选择性的——如果你添加引力到你的整个游戏世界,你的所有平台都将掉到地上。反之,你可以仅添加引力到你的玩家和敌人精灵中。
首先,在你的Player类中添加一个gravity函数:
defgravity(self): self.movey+=3.2#玩家掉落的多快
这是一个简单的函数。首先,不管你的玩家是否想运动,你设置你的玩家垂直运动。也就是说,你已经编程你的玩家总是在下降。这基本上就是引力。
为使引力函数生效,你必须在你的主循环中调用它。这样,当每一个处理循环时,Python都应用下落运动到你的玩家。
在这代码中,添加第一行到你的循环中:
player.gravity()#检查引力 player.update()
启动你的游戏来看看会发生什么。要注意,因为它发生的很快:你是玩家从天空上下落,马上掉出了你的游戏屏幕。
你的引力模拟是工作的,但是,也许太好了。
作为一次试验,尝试更改你玩家下落的速度。
给引力添加一个地板
你的游戏没有办法发现你的角色掉落出世界的问题。在一些游戏中,如果一个玩家掉落出世界,该精灵被删除,并在某个新的位置重生。在另一些游戏中,玩家会丢失分数或一条生命。当一个玩家掉落出世界时,不管你想发生什么,你必须能够侦测出玩家何时消失在屏幕外。
在Python中,要检查一个条件,你可以使用一个if语句。
你必需查看你玩家是否正在掉落,以及你的玩家掉落的程度。如果你的玩家掉落到屏幕的底部,那么你可以做一些事情。简化一下,设置玩家精灵的位置为底部边缘上方20像素。
使你的gravity函数看起来像这样:
defgravity(self): self.movey+=3.2#玩家掉落的多快 ifself.rect.y>worldyandself.movey>=0: self.movey=0 self.rect.y=worldy-ty
然后,启动你的游戏。你的精灵仍然下落,但是它停在屏幕的底部。不过,你也许不能看到你在地面层之上的精灵。一个简单的解决方法是,在精灵碰撞游戏世界的底部后,通过添加另一个-ty到它的新Y位置,从而使你的精灵弹跳到更高处:
defgravity(self): self.movey+=3.2#玩家掉落的多快 ifself.rect.y>worldyandself.movey>=0: self.movey=0 self.rect.y=worldy-ty-ty
现在你的玩家在屏幕底部弹跳,恰好在你地面精灵上面。
你的玩家真正需要的是反抗引力的方法。引力问题是,你不能反抗它,除非你有一些东西来推开引力作用。因此,在接下来的文章中,你将添加地面和平台碰撞以及跳跃能力。在这期间,尝试应用引力到敌人精灵。
到目前为止,这里是全部的代码:
#!/usr/bin/envpython3
#drawaworld
#addaplayerandplayercontrol
#addplayermovement
#addenemyandbasiccollision
#addplatform
#addgravity
#GNUAll-PermissiveLicense
#Copyinganddistributionofthisfile,withorwithoutmodification,
#arepermittedinanymediumwithoutroyaltyprovidedthecopyright
#noticeandthisnoticearepreserved.Thisfileisofferedas-is,
#withoutanywarranty.
importpygame
importsys
importos
'''
Objects
'''
classPlatform(pygame.sprite.Sprite):
#xlocation,ylocation,imgwidth,imgheight,imgfile
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
classPlayer(pygame.sprite.Sprite):
'''
Spawnaplayer
'''
def__init__(self):
pygame.sprite.Sprite.__init__(self)
self.movex=0
self.movey=0
self.frame=0
self.health=10
self.score=1
self.images=[]
foriinrange(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()
defgravity(self):
self.movey+=3.2#howfastplayerfalls
ifself.rect.y>worldyandself.movey>=0:
self.movey=0
self.rect.y=worldy-ty-ty
defcontrol(self,x,y):
'''
controlplayermovement
'''
self.movex+=x
self.movey+=y
defupdate(self):
'''
Updatespriteposition
'''
self.rect.x=self.rect.x+self.movex
self.rect.y=self.rect.y+self.movey
#movingleft
ifself.movex<0:
self.frame+=1
ifself.frame>ani*3:
self.frame=0
self.image=self.images[self.frame//ani]
#movingright
ifself.movex>0:
self.frame+=1
ifself.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)
forenemyinenemy_hit_list:
self.health-=1
print(self.health)
ground_hit_list=pygame.sprite.spritecollide(self,ground_list,False)
forginground_hit_list:
self.health-=1
print(self.health)
classEnemy(pygame.sprite.Sprite):
'''
Spawnanenemy
'''
def__init__(self,x,y,img):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(os.path.join('images',img))
#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
defmove(self):
'''
enemymovement
'''
distance=80
speed=8
ifself.counter>=0andself.counter<=distance:
self.rect.x+=speed
elifself.counter>=distanceandself.counter<=distance*2:
self.rect.x-=speed
else:
self.counter=0
self.counter+=1
classLevel():
defbad(lvl,eloc):
iflvl==1:
enemy=Enemy(eloc[0],eloc[1],'yeti.png')#spawnenemy
enemy_list=pygame.sprite.Group()#createenemygroup
enemy_list.add(enemy)#addenemytogroup
iflvl==2:
print("Level"+str(lvl))
returnenemy_list
defloot(lvl,lloc):
print(lvl)
defground(lvl,gloc,tx,ty):
ground_list=pygame.sprite.Group()
i=0
iflvl==1:
whilei
总结
到此这篇关于在你的Python游戏中模拟引力的文章就介绍到这了,更多相关在你的Python游戏中模拟引力内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!