python文件下载战

这是正在进行的有关使用Pygame模块在Python 3中创建视频游戏的系列的一部分。 以前的文章是:

  • 通过构建一个简单的骰子游戏,学习如何用Python编程
  • 使用Pygame模块使用Python构建游戏框架
  • 如何将玩家添加到您的Python游戏中
  • 使用Pygame移动游戏角色
  • 没有小人的英雄是什么? 如何在您的Python游戏中添加一个
  • 将平台放入您的Python平台器中
  • 在Python游戏中模拟重力
  • 将跳跃添加到您的Python平台游戏
  • 使您的Python游戏玩家可以向前和向后运行

如果您已阅读本系列的前几篇文章,那么您将了解视频游戏机制编程的所有基础知识。 您可以基于这些基础知识来创建自己的全功能视频游戏。 当您初次学习时,遵循本系列代码示例中的“食谱”会很有帮助,但是最终,食谱成为一种约束。 现在是时候使用您所学的原理并以新的方式应用它们了。

如果说起来容易做起来难,那么本文将演示一个示例,说明如何利用您已经知道的东西来实现新目的。 具体来说,它涵盖了如何实施掠夺系统

在大多数视频游戏中,您都有机会“掠夺”或收集游戏世界中的宝物和其他物品。 战利品通常会增加您的得分或您的健康状况,或者提供导致您进行下一个任务的信息。

在游戏中包含战利品类似于编程平台。 与平台一样,战利品没有用户控件,无法滚动游戏世界,并且必须检查是否与玩家精灵冲突。

创建战利品功能

Loot与平台非常相似,您甚至不需要Loot类。 您可以仅重用Platform类并调用结果战利品。

由于战利品的类型和位置可能因级别而异,所以如果您还没有的话,请在您的Level类中创建一个名为loot的新函数。 由于战利品不是平台,因此您还必须创建一个新的loot_list组,然后向其中添加战利品对象。 与平台,地面和敌人一样,检查碰撞时将使用此组:

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


您可以根据需要添加任意数量的战利品。 只要记住将每个添加到您的战利品清单。 Platform类的参数是战利品精灵的X位置,Y位置,宽度和高度(通常最容易使战利品精灵保持与其他所有图块相同的大小),以及要用作战利品的图像。 战利品的放置可能与映射平台一样复杂,因此请使用在创建关卡时创建的关卡设计文档。

在脚本的“ 设置”部分中调用新的战利品功能。 在以下代码中,前三行用于上下文,因此只需添加第四行:

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 )

如您现在所知,除非您将其包含在主循环中,否则战利品不会被吸引到屏幕上。 将以下代码示例的最后一行添加到循环中:

enemy_list. draw ( world )
ground_list. draw ( world )
plat_list. draw ( world )
loot_list. draw ( world )

启动游戏,看看会发生什么。

生成了战利品,但是当玩家遇到战利品时它们什么也不做,当玩家经过它们时它们也不会滚动。 接下来解决这些问题。

滚动战利品

像平台一样,战利品必须在玩家穿越游戏世界时滚动。 逻辑与平台滚动相同。 要向前滚动战利品,请添加最后两行:

for e in enemy_list:
e. rect . x - = scroll
for l in loot_list:
l. rect . x - = scroll

要向后滚动,请添加最后两行:

for e in enemy_list:
e. rect . x + = scroll
for l in loot_list:
l. rect . x + = scroll

再次启动游戏,以查看战利品对象现在的行为就像它们游戏世界中一样,而不是仅仅绘制在游戏之上。

检测碰撞

与平台和敌人一样,您可以检查战利品和玩家之间的碰撞。 逻辑与其他碰撞相同,除了碰撞不会(必要)影响重力或健康。 相反,命中会导致战利品消失并增加玩家的得分。

当玩家触摸战利品时,可以从loot_list中删除该物。 这意味着当您的主循环重绘了loot_list中的所有战利品时,它不会重绘该特定对象,因此看起来玩家已经抓住了战利品。

Player类的update函数中,在平台冲突检测上方添加以下代码(最后一行仅用于上下文):

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 )


发生冲突时,您不仅会从战利品对象组中删除战利品对象,还会使玩家获得得分上的提升。 您尚未创建得分变量,因此将其添加到在Player类的__init__函数中创建的玩家属性中。 在下面的代码中,前两行是针对上下文的,因此只需添加score变量:

self . frame = 0
self . health = 10
self . score = 0

在主循环中调用update函数时,请包含loot_list

player. gravity ( )
player. update ( )

如您所见,您已经掌握了所有基础知识。 您现在要做的就是以新的方式使用您所知道的。

下一篇文章中还有其他一些技巧,但是与此同时,请使用您所学的知识来制作一些简单的单级游戏。 限制您尝试创建的内容的范围很重要,这样您就不会感到不知所措。 这也使得最终更容易获得外观和感觉完好的成品。

到目前为止,这是您为该Python平台编写的所有代码:

#!/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 pygame
import sys
import 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 = 960
worldy = 720

fps = 40 # frame rate
ani = 4  # animation cycles
clock = 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 player
player. rect . x = 0
player. rect . y = 0
player_list = pygame. sprite . Group ( )
player_list. add ( player )
steps = 10
forwardx = 600
backwardx = 230

eloc = [ ]
eloc = [ 200 , 20 ]
gloc = [ ]
#gloc = [0,630,64,630,128,630,192,630,256,630,320,630,384,630]
tx = 64 #tile size
ty = 64 #tile size

i = 0
while 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 )


翻译自: https://opensource.com/article/20/1/loot-python-platformer-game

python文件下载战

python文件下载战_在您的Python平台游戏中放一些战利品相关推荐

  1. python编程基础_月隐学python第2课

    python编程基础_月隐学python第2课 学习目标 掌握变量的输入和输出 掌握数据类型的基本概念 掌握算数运算 1.变量的输入和输出 1.1 变量输入 使用input输入 input用于输入数据 ...

  2. 查看Python的版本_查看当前安装Python的版本

    一.查看Python的版本_查看当前安装Python的版本 具体方法: 首先按[win+r]组合键打开运行: 然后输入cmd,点击[确定]: 最后执行[python --version]命令即可. 特 ...

  3. python并行运算库_最佳并行绘图Python库简介:“ HiPlot”

    python并行运算库 HiPlot is Facebook's Python library to support visualization of high-dimensional data ta ...

  4. python opencv手册_教你用Python实现5毛钱特效(给你的视频来点料)

    一.前言 请务必看到最后.Python牛已经不是一天两天的事了,但是我开始也没想到,Python能这么牛.前段时间接触了一个批量抠图的模型库,而后在一些视频中找到灵感,觉得应该可以通过抠图的方式,给视 ...

  5. python新手难点_初学两天python的操作难点总结

    已经学习两天python,将我认为的操作难点进行总结 1 在cmd下 盘与盘之间的切换 直接 D或d: 就好 2 查找当前盘或者文件下面的目录 直接 dir 3 想在一个盘下进去一个文件夹,用cd空格 ...

  6. python内存泄漏_诊断和修复Python中的内存泄漏

    python内存泄漏 Fugue uses Python extensively throughout the Conductor and in our support tools, due to i ...

  7. 趣学python编程下载_《趣学Python编程》——1.2 安装Python

    本节书摘来自异步社区<趣学Python编程>一书中的第1章,第1.2节,作者[美]Jason Briggs,尹哲 译,更多章节内容可以访问云栖社区"异步社区"公众号查看 ...

  8. python基本原理概论_怎样开始自学Python?

    本人才疏学浅,学识大多浅尝辄止,故文章若有错误,不论是文字笔误还是理解有错,烦请您留言以告知,本人必定感激不尽! **Python分类下的系列文章,不断更新中,如果你迫不及待地想要看看写得如何可以先试 ...

  9. 如何提高python的运行效率_几个提升Python运行效率的方法之间的对比

    在我看来,python社区分为了三个流派,分别是python 2.x组织,3.x组织和PyPy组织.这个分类基本上可以归根于类库的兼容性和速度.这篇文章将聚焦于一些通用代码的优化技巧以及编译成C后性能 ...

最新文章

  1. btf-raft共识算法_了解Raft共识算法:学术文章摘要
  2. 10分钟掌握RocketMQ的核心知识
  3. js改变img标签的src属性在IE下没反应的解决方法
  4. 程序员读研如何提高技术之我见
  5. 2.3.2 spring属性注入-注解注入-半注解注入-后序
  6. Order asynchronous mode
  7. c#windows消息循环sendmessage实例
  8. 使用Amazon Web Services(EC2)
  9. 张正友相机标定Opencv实现以及标定流程标定结果评价图像矫正流程解析(附标定程序和棋盘图)
  10. dram和nand哪个难生产_仅300名员工,年产7台,订单排到5年后,比光刻机还难买...
  11. 今晚19:30见!小米12确认全系出厂预装MIUI 13:流畅度提升52%
  12. 终结VC2005分发包版本问题
  13. 制作Win10PE启动盘
  14. 淘宝/天猫/京东/抖音直播年货节抢购秒杀助手更新下载,喵惠抢购助手支持自动免密支付,分享源码共同学习探讨
  15. 十大最热门人工智能技术
  16. 谷歌浏览器正式版(稳定版)离线安装包下载大全
  17. Docker 官方安装文档
  18. C++ : 热血格斗场
  19. html5 canvas消除锯齿,HTML5 Canvas 如何取消反锯齿绘图
  20. 第1-7课:基础开胃菜

热门文章

  1. Linux CentOS7.0 使用root登录桌面
  2. 【红帽认证参考】常见问题解答
  3. 单链表基本操作的实现——前插法与后插法创建单链表
  4. 计算机编程专业的民办大学排名,法国计算机编程专业大学排名(2020年USNEWS)_快飞留学...
  5. 匹配一个字符串的开头和结尾_我如何构建一个应用程序来展示精彩小说的开头和结尾
  6. bitmap的六种压缩方式,Android图片压缩(转)
  7. 技巧_altium中两个PCB文件合并为一个进行加工
  8. php百分比乘加,PHP学习笔记第一篇 基础知识
  9. 微信直播聊天室架构演进
  10. python画三角函数--cosx