Python Platform游戏中的一个问题,即将进入下一个关卡
我已经使用Python3.5制作了一个平台,并且已经达到了第二级 当您触摸蓝色区域(由Python Platform游戏中的一个问题,即将进入下一个关卡,python,python-3.x,pygame,python-3.5,Python,Python 3.x,Pygame,Python 3.5,我已经使用Python3.5制作了一个平台,并且已经达到了第二级 当您触摸蓝色区域(由E表示)时,我想进入第二级。现在,蓝色区域退出代码,只是为了确保触发器被触发 我已经创建了一个数组中的第二级 这是我的代码: def main(): global cameraX, cameraY pygame.init() screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH) timer = pygame.time.C
E
表示)时,我想进入第二级。现在,蓝色区域退出代码,只是为了确保触发器被触发
我已经创建了一个数组中的第二级
这是我的代码:
def main():
global cameraX, cameraY
pygame.init()
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
timer = pygame.time.Clock()
up = down = left = right = running = False
bg = Surface((32,32))
entities = pygame.sprite.Group()
player = Player(32, 700)
platforms = []
x = y = 0
level1 = [ #level as an array
]
level2 = [ #level as an array
]
# build the level
for row in level1:
for col in row:
if col == "P":
p = Platform(x, y)
platforms.append(p)
entities.add(p)
if col == "E":
e = ExitBlock(x, y)
platforms.append(e)
entities.add(e)
if col == "L":
l = Lava(x, y)
platforms.append(l)
entities.add(l)
x += 32
y += 32
x = 0
total_level_width = len(level1[0])*32
total_level_height = len(level2)*32
camera = Camera(complex_camera, total_level_width, total_level_height)
entities.add(player)
while 1:
timer.tick(60)
for e in pygame.event.get():
if e.type == QUIT:
raise SystemExit("ESCAPE")
if e.type == KEYDOWN and e.key == K_UP:
up = True
if e.type == KEYDOWN and e.key == K_DOWN:
down = True
if e.type == KEYDOWN and e.key == K_LEFT:
left = True
if e.type == KEYDOWN and e.key == K_RIGHT:
right = True
if e.type == KEYDOWN and e.key == K_SPACE:
running = True
if e.type == KEYUP and e.key == K_UP:
up = False
if e.type == KEYUP and e.key == K_DOWN:
down = False
if e.type == KEYUP and e.key == K_RIGHT:
right = False
if e.type == KEYUP and e.key == K_LEFT:
left = False
# draw background
for y in range(32):
for x in range(32):
screen.blit(bg, (x * 32, y * 32))
camera.update(player)
# update player, draw everything else
player.update(up, down, left, right, running, platforms)
for e in entities:
screen.blit(e.image, camera.apply(e))
pygame.display.update()
class Camera(object):
def __init__(self, camera_func, width, height):
self.camera_func = camera_func
self.state = Rect(0, 0, width, height)
def apply(self, target):
return target.rect.move(self.state.topleft)
def update(self, target):
self.state = self.camera_func(self.state, target.rect)
def simple_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
return Rect(-l+HALF_WIDTH, -t+HALF_HEIGHT, w, h)
def complex_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
l, t, _, _ = -l+HALF_WIDTH, -t+HALF_HEIGHT, w, h
l = min(0, l) # stop scrolling at the left edge
l = max(-(camera.width-WIN_WIDTH), l) # stop scrolling at the right edge
t = max(-(camera.height-WIN_HEIGHT), t) # stop scrolling at the bottom
t = min(0, t) # stop scrolling at the top
return Rect(l, t, w, h)
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Player(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.xvel = 0
self.yvel = 0
self.onGround = False
self.image = Surface((32,32))
self.image.fill(Color("#ff0000"))
self.image.convert()
self.rect = Rect(x, y, 32, 32)
def update(self, up, down, left, right, running, platforms):
if up:
# only jump if on the ground
if self.onGround:
self.yvel -= 8
if down:
pass
if running:
self.xvel = 12
if left:
self.xvel = -8
if right:
self.xvel = 8
if not self.onGround:
# only accelerate with gravity if in the air
self.yvel += 0.3
# max falling speed
if self.yvel > 100: self.yvel = 100
if not(left or right):
self.xvel = 0
# increment in x direction
self.rect.left += self.xvel
# do x-axis collisions
self.collide(self.xvel, 0, platforms)
# increment in y direction
self.rect.top += self.yvel
# assuming we're in the air
self.onGround = False;
# do y-axis collisions
self.collide(0, self.yvel, platforms)
def collide(self, xvel, yvel, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if isinstance(p, ExitBlock):
pygame.event.post(pygame.event.Event(QUIT))
if isinstance(p, Lava):
main()
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
class Platform(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color("#7cfc00"))
self.rect = Rect(x, y, 32, 32)
def update(self):
pass
class ExitBlock(Platform):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.image.fill(Color("#0033FF"))
class Lava(Platform):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.image.fill(Color("#ff0000"))
if __name__ == "__main__":
main()
def main():
全球摄影机
pygame.init()
screen=pygame.display.set_模式(显示、标志、深度)
timer=pygame.time.Clock()
向上=向下=左=右=运行=错误
bg=表面((32,32))
entities=pygame.sprite.Group()
玩家=玩家(32700)
平台=[]
x=y=0
level1=[#作为数组的级别
]
level2=[#作为数组的级别
]
#建立水平
对于级别1中的行:
对于行中的列:
如果col==“P”:
p=平台(x,y)
平台。附加(p)
实体.添加(p)
如果列==“E”:
e=退出锁定(x,y)
平台。附加(e)
实体.添加(e)
如果列==“L”:
l=熔岩(x,y)
平台。附加(l)
实体.添加(l)
x+=32
y+=32
x=0
总水平宽度=长度(水平1[0])*32
总高度=长度(2级)*32
照相机=照相机(复杂照相机、总水平宽度、总水平高度)
实体。添加(玩家)
而1:
计时器。滴答(60)
对于pygame.event.get()中的e:
如果e.type==退出:
升起系统出口(“逃生”)
如果e.type==KEYDOWN和e.key==K_UP:
向上=正确
如果e.type==KEYDOWN和e.key==K_DOWN:
向下=真
如果e.type==KEYDOWN和e.key==K_LEFT:
左=真
如果e.type==KEYDOWN和e.key==K_RIGHT:
右=真
如果e.type==KEYDOWN和e.key==K_空间:
运行=真
如果e.type==KEYUP和e.key==K\u-UP:
向上=错误
如果e.type==KEYUP和e.key==K_DOWN:
向下=错误
如果e.type==KEYUP和e.key==K_RIGHT:
右=假
如果e.type==KEYUP和e.key==K_LEFT:
左=假
#画背景
对于范围(32)内的y:
对于范围(32)内的x:
屏幕光点(背景,(x*32,y*32))
摄像机更新(播放器)
#更新玩家,绘制其他所有内容
更新(上、下、左、右、运行、平台)
对于电子商务实体:
屏幕。blit(如图像、照相机、应用(e))
pygame.display.update()
类摄影机(对象):
定义初始值(自身、相机功能、宽度、高度):
self.camera\u func=camera\u func
self.state=Rect(0,0,宽度,高度)
def应用(自身、目标):
返回target.rect.move(self.state.topleft)
def更新(自我、目标):
self.state=self.camera_func(self.state,target.rect)
def简易摄像头(摄像头、目标摄像头):
l、 t,u,u=目标
_,uw,h=摄像机
返回矩形(-l+半宽,-t+半高,宽,高)
def复合摄像头(摄像头、目标摄像头):
l、 t,u,u=目标
_,uw,h=摄像机
l、 t,uu,u=-l+半宽,-t+半高,w,h
l=min(0,l)#停止在左边缘滚动
l=最大值(((摄影机宽度-WIN#U宽度),l)#在右边缘停止滚动
t=max(-(摄像机高度-WIN_高度),t)#在底部停止滚动
t=min(0,t)#停止在顶部滚动
返回矩形(l、t、w、h)
类实体(pygame.sprite.sprite):
定义初始化(自):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
职业玩家(实体):
定义初始化(self,x,y):
实体。u u初始化(自)
self.xvel=0
self.yvel=0
self.onGround=False
self.image=曲面((32,32))
self.image.fill(颜色(“#ff0000”))
self.image.convert()
self.rect=rect(x,y,32,32)
def更新(自我、上、下、左、右、运行、平台):
如果出现以下情况:
#只有在地面上才能跳
如果self.onGround:
self.yvel-=8
如果关闭:
通过
如果正在运行:
self.xvel=12
如果留下:
self.xvel=-8
如果正确:
self.xvel=8
如果不是自我循环:
#只有在空气中时,才能在重力作用下加速
self.yvel+=0.3
#最大下降速度
如果self.yvel>100:self.yvel=100
如果不是(左或右):
self.xvel=0
#x方向上的增量
self.rect.left+=self.xvel
#做x轴碰撞
self.collide(self.xvel,0,platforms)
#y方向上的增量
self.rect.top+=self.yvel
#假设我们在空中
self.onGround=False;
#进行y轴碰撞
self.collide(0,self.yvel,平台)
def碰撞(自身、xvel、yvel、平台):
对于平台中的p:
如果pygame.sprite.collide_rect(self,p):
如果存在(p,ExitBlock):
pygame.event.post(pygame.event.event(退出))
如果存在(p,熔岩):
main()
如果xvel>0:
self.rect.right=p.rect.left
如果xvel<0:
self.rect.left=p.rect.right
如果yvel>0:
self.rect.bottom=p.rect.top
self.onGround=True
self.yvel=0
如果yvel<0:
self.rect.top=p.rect.bottom
类平台(实体):
定义初始化(self,x,y):
实体。u u初始化(自)
self.image=曲面((32,32))
self.image.convert()
self.image.f
levelNum == 3:
buildLevel(level3)
import pygame
from pygame import *
levelNum = 1
WIN_WIDTH = 1000
WIN_HEIGHT = 640
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)
DISPLAY = (WIN_WIDTH, WIN_HEIGHT)
DEPTH = 32
FLAGS = 0
CAMERA_SLACK = 30
def buildLevel(level):
plats = []
ents = pygame.sprite.Group()
x,y = 0,0
for row in level:
for col in row:
if col == "P":
p = Platform(x, y)
plats.append(p)
ents.add(p)
if col == "E":
e = ExitBlock(x, y)
plats.append(e)
ents.add(e)
if col == "L":
l = Lava(x, y)
plats.append(l)
ents.add(l)
x += 32
y += 32
x = 0
return plats,ents
def main():
global level2,player
global cameraX, cameraY
pygame.init()
screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH)
timer = pygame.time.Clock()
up = down = left = right = running = False
bg = Surface((32,32))
bg.convert()
bg.fill(Color("#87ceeb"))
entities = pygame.sprite.Group()
player = Player(32, 700)
platforms = []
x = y = 0
level1 = [
"P PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
"P PE P",
"P PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP P P",
"P P P",
"P P P",
"P P P",
"P P P",
"P L L P P",
"P PPPPPPPPPPPPPPPPPPPPPPPPPPP P",
"P LLL P",
"P LLL P",
"P PPPP P",
"P P",
"P P P",
"P PPPP PPPPPP P",
"P PP P",
"P P",
"P PPPPPPPPPP P",
"P P",
"P PPPP P",
"P PP P",
"P E PP P",
"P PPPPLLLLLPP P",
"P PPPPPPLLLLLPPLLLLLLLLLLLLLLLLLLLLLLLLLLLP",
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",]
level2 = [
"P PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",
"P PE P",
"P PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP P P",
"P P P",
"P P P",
"P P P",
"P P P",
"P L L P P",
"P PPPPPPPPPPPPPPPPPPPPPPPPPPP P",
"P LLL P",
"P LLL P",
"P PPPP P",
"P P",
"P P P",
"P PPPP PPPPPP P",
"P PP P",
"P P",
"P PPPPPPPPPP P",
"P P",
"P PPPP P",
"P P",
"P P",
"P P",
"P P",
"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP",]
# build the level
if levelNum ==1:
platforms,entities = buildLevel(level1)
elif levelNum == 2:
platforms,entities = buildLevel(level2)
total_level_width = len(level1[0])*32
total_level_height = len(level2)*32
camera = Camera(complex_camera, total_level_width, total_level_height)
entities.add(player)
while 1:
timer.tick(60)
for e in pygame.event.get():
if e.type == QUIT:
raise SystemExit("ESCAPE")
if e.type == KEYDOWN and e.key == K_UP:
up = True
if e.type == KEYDOWN and e.key == K_DOWN:
down = True
if e.type == KEYDOWN and e.key == K_LEFT:
left = True
if e.type == KEYDOWN and e.key == K_RIGHT:
right = True
if e.type == KEYDOWN and e.key == K_SPACE:
running = True
if e.type == KEYUP and e.key == K_UP:
up = False
if e.type == KEYUP and e.key == K_DOWN:
down = False
if e.type == KEYUP and e.key == K_RIGHT:
right = False
if e.type == KEYUP and e.key == K_LEFT:
left = False
# draw background
for y in range(32):
for x in range(32):
screen.blit(bg, (x * 32, y * 32))
camera.update(player)
# update player, draw everything else
player.update(up, down, left, right, running, platforms)
for e in entities:
screen.blit(e.image, camera.apply(e))
pygame.display.update()
class Camera(object):
def __init__(self, camera_func, width, height):
self.camera_func = camera_func
self.state = Rect(0, 0, width, height)
def apply(self, target):
return target.rect.move(self.state.topleft)
def update(self, target):
self.state = self.camera_func(self.state, target.rect)
def simple_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
return Rect(-l+HALF_WIDTH, -t+HALF_HEIGHT, w, h)
def complex_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
l, t, _, _ = -l+HALF_WIDTH, -t+HALF_HEIGHT, w, h
l = min(0, l) # stop scrolling at the left edge
l = max(-(camera.width-WIN_WIDTH), l) # stop scrolling at the right edge
t = max(-(camera.height-WIN_HEIGHT), t) # stop scrolling at the bottom
t = min(0, t) # stop scrolling at the top
return Rect(l, t, w, h)
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Player(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.xvel = 0
self.yvel = 0
self.onGround = False
self.image = Surface((32,32))
self.image.fill(Color("#ff0000"))
self.image.convert()
self.rect = Rect(x, y, 32, 32)
def update(self, up, down, left, right, running, platforms):
if up:
# only jump if on the ground
if self.onGround:
self.yvel -= 8
if down:
pass
if running:
self.xvel = 12
if left:
self.xvel = -8
if right:
self.xvel = 8
if not self.onGround:
# only accelerate with gravity if in the air
self.yvel += 0.3
# max falling speed
if self.yvel > 100: self.yvel = 100
if not(left or right):
self.xvel = 0
# increment in x direction
self.rect.left += self.xvel
# do x-axis collisions
self.collide(self.xvel, 0, platforms)
# increment in y direction
self.rect.top += self.yvel
# assuming we're in the air
self.onGround = False;
# do y-axis collisions
self.collide(0, self.yvel, platforms)
def collide(self, xvel, yvel, plats):
global levelNum
for p in plats:
if pygame.sprite.collide_rect(self, p):
if isinstance(p, ExitBlock):
levelNum+=1
main()
#pygame.event.post(pygame.event.Event(QUIT))
if isinstance(p, Lava):
main()
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
class Platform(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.image = Surface((32, 32))
self.image.convert()
self.image.fill(Color("#7cfc00"))
self.rect = Rect(x, y, 32, 32)
def update(self):
pass
class ExitBlock(Platform):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.image.fill(Color("#0033FF"))
class Lava(Platform):
def __init__(self, x, y):
Platform.__init__(self, x, y)
self.image.fill(Color("#ff0000"))
if __name__ == "__main__":
main()