我正在用PyGame制作一个小游戏,相机聚焦在一个代表角色的精灵上,当按下箭头键时,地图会围绕它移动。问题是地图不动。我以前多次遇到过这个问题,我想我已经把它缩小到可能的范围,但是我不知道如何真正解决这个问题。
这是代码:
import sys, pygame
from pygame.locals import *
pygame.init()
size = screen_width, screen_height = 800, 600
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
game_is_running = True
class Character(object):
""" Defines player's metadata """
def __init__(self):
self.x = 350
self.y = 250 # at about the center of the screen
self.dx = 0
self.dy = 0
self.width = 50
self.height = 50
self.sprite = pygame.image.load("./../Images/main-char-sprite.png") #the pic representing the character
self.dead_sprite = None #the pic representing the character when dead (doesn't exist yet)
player = Character()
class Map():
"""
Defines environment.
Will be drawn around static arrays for now.
"""
def __init__(self):
self.wall = pygame.image.load("./../Images/dungeon/wall.jpg")
self.floor = pygame.image.load("./../Images/dungeon/floor.jpg")
self.door = pygame.image.load("./../Images/dungeon/door.jpg")
self.null_tile = pygame.image.load("./../Images/blank.png")
self.x = 0
self.y = 0
self.tile_height = 50
self.tile_width = 50
self.center_x = 400
self.center_y = 300
#self.center_of_room = (self.center_x, self.center_y)
self.town = [
[0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0],
[0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
[0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
[0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0,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, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
def draw_floor(self): #will use arrays
for j in range(24): #self.town is 24x24 elements
for k in range(24):
if self.town[j][k]==0:
screen.blit(self.null_tile, (j*player.height, k*player.width))
elif self.town[j][k]==1:
screen.blit(self.wall, (j*player.height, k*player.width))
elif self.town[j][k]==2:
screen.blit(self.floor, (j*player.height, k*player.width))
elif self.town[j][k]==3:
screen.blit(self.door, (j*player.height, k*player.width))
else:
screen.blit(self.null_tile, (j*player.height, k*player.width))
def is_traversable(self):
# if where the player is about to walk is not a floor,
# the land is not traversable and character will not move
if self.town[(player.x + player.dx)//50][(player.y + player.dy)//50]==2:
return True
return False
map = Map()
while game_is_running:
clock.tick(60)
key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT or key[K_ESCAPE] or key[K_RETURN]:
game_is_running = False
if key[K_UP]:
player.dy = player.height-(2*player.height) #turns player.height negative
if map.is_traversable():
map.y -= player.height
elif key[K_DOWN]:
player.dy = player.height
if map.is_traversable():
map.y += player.height
elif key[K_LEFT]:
player.dx = player.width-(2*player.width)
if map.is_traversable():
map.x -= player.width
elif key[K_RIGHT] and map.is_traversable():
player.dx = player.width
if map.is_traversable():
map.x += player.width
else: pass
""" Drawing routines: """
screen.fill((0, 0, 0))
map.draw_floor()
screen.blit(player.sprite, (player.x, player.y))
""" Graphics cleanup: """
pygame.display.update()
pygame.display.flip()
pygame.quit()
sys.exit()
基于我过去的PyGame程序,我总是在移动被绘制到屏幕上的精灵集时遇到问题,比如我在这里所做的。即使是平庸简单的例子也会失败。我想知道的是这是为什么,我怎样才能简明扼要地画出大地图,并且仍然让它们移动?range()
生成一个列表,据我所知,列表元素是可变的,那么为什么映射的位置没有改变呢?
贴图进行阴影处理
是可遍历的
始终为falsex=world\u x camera\u x
和y=world\u y camera\u y
。更改摄影机x/y(或将其视为偏移)会导致地图滚动。然而,对象将其位置存储为全局坐标。(无论是基于像素还是基于平铺)修改后的代码如下。您可以看到它轮询向上箭头,但它从不打印“true!”。
import pygame
from pygame.locals import *
pygame.init()
size = screen_width, screen_height = 800, 600
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
game_is_running = True
def temp_sprite(color, size=50):
# gen Surface, since I don't have your sprites
surf = pygame.Surface([size, size])
surf.fill(Color(color))
return surf
class Character(object):
""" Defines player's metadata """
def __init__(self):
self.x = 350
self.y = 250 # at about the center of the screen
self.dx = 0
self.dy = 0
self.width = 50
self.height = 50
#self.sprite = pygame.image.load("./../Images/main-char-sprite.png") #the pic representing the character
self.sprite = temp_sprite("red")
self.dead_sprite = None #the pic representing the character when dead (doesn't exist yet)
player = Character()
class Map():
"""
Defines environment.
Will be drawn around static arrays for now.
"""
def __init__(self):
self.wall = temp_sprite("blue") #pygame.image.load("./../Images/dungeon/wall.jpg")
self.floor = temp_sprite("green") #pygame.image.load("./../Images/dungeon/floor.jpg")
self.door = temp_sprite("brown") #pygame.image.load("./../Images/dungeon/door.jpg")
self.null_tile = temp_sprite("magenta") # pygame.image.load("./../Images/blank.png")
self.x = 0
self.y = 0
self.tile_height = 50
self.tile_width = 50
self.center_x = 400
self.center_y = 300
#self.center_of_room = (self.center_x, self.center_y)
self.town = [
[0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0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0],
[0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
[0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
def draw_floor(self): #will use arrays
for j in range(24): #self.town is 24x24 elements
for k in range(24):
if self.town[j][k]==0:
screen.blit(self.null_tile, (gamemap.y + j*player.height, gamemap.x + k*player.width))
elif self.town[j][k]==1:
screen.blit(self.wall, (gamemap.y + j*player.height, gamemap.x + k*player.width))
elif self.town[j][k]==2:
screen.blit(self.floor, (gamemap.y + j*player.height, gamemap.x + k*player.width))
elif self.town[j][k]==3:
screen.blit(self.door, (gamemap.y + j*player.height, gamemap.x + k*player.width))
else:
screen.blit(self.null_tile, (gamemap.y + j*player.height, gamemap.x + k*player.width))
def is_traversable(self):
# if where the player is about to walk is not a floor,
# the land is not traversable and character will not move
if self.town[(player.x + player.dx)//50][(player.y + player.dy)//50]!=2:
print "true!"
return True
return False
gamemap = Map()
while game_is_running:
clock.tick(60)
key = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT or key[K_ESCAPE] or key[K_RETURN]:
game_is_running = False
if key[K_UP]:
print '.'
player.dy = player.height-(2*player.height) #turns player.height negative
if gamemap.is_traversable():
gamemap.y -= player.height
elif key[K_DOWN]:
player.dy = player.height
if gamemap.is_traversable():
gamemap.y += player.height
elif key[K_LEFT]:
player.dx = player.width-(2*player.width)
if gamemap.is_traversable():
gamemap.x -= player.width
elif key[K_RIGHT] and gamemap.is_traversable():
player.dx = player.width
if gamemap.is_traversable():
gamemap.x += player.width
else: pass
""" Drawing routines: """
screen.fill((0, 0, 0))
gamemap.draw_floor()
screen.blit(player.sprite, (player.x, player.y))
""" Graphics cleanup: """
pygame.display.update()
pygame.display.flip()
pygame.quit()
sys.exit()
移动精灵 现在你知道了如何展示精灵,但是让它们移动呢?很简单:使用Pixi的ticker。这被称为 游戏循环 。任何在游戏循环里的代码都会1秒更新60次。你可以用下面的代码让 cat 精灵以每帧1像素的速率移动。 function setup() { //Start the game loop by adding the `gameLoop` function to //Pixi's `t
我在Java和Slick2D方面有几年的经验,我正试图将一个游戏移植到libgdx,因为它是一个更好的库。然而,我有一个简单的问题在屏幕上移动精灵。这个应用编程接口一定有一些我不理解的范例。我将我的代码提炼成以下内容。它识别输入,并在我的整个实体和网络系统中运行它,无论是在服务器上还是从服务器上,但是精灵保持在同一个位置。任何帮助都会很棒。 我的create()方法: getCurrentX/Y方
父类:BK.Node 精灵类 成员变量 变量 类型 名称 备注 size Object 大小 anchor Object 锚点 cornerRadius number 圆角 单位为像素 例子: var babaTex =new BK.Texture('GameRes://texture/test.png'); var sp =new BK.Sprite(200,200,babaTex,0,1,1
我创建了一个程序,创建一个正方形并将其绘制在JPanel上。还有控制正方形运动的键绑定(w为上)(s为下)(a为左)(d为右)。我的问题是,当我按下键时,它会移动一个实例,犹豫,然后继续移动。有没有办法防止运动的犹豫。 这是我的面板类。 任何帮助都将不胜感激。非常感谢。
问题内容: 目前,每次按键时,精灵仅移动1个像素。按住左右键时,如何使水暖工精灵不断移动? 问题答案: 您可以使用pygame.key.get_pressed来做到这一点。 例:
Pixi 精灵 现在你就有了一个画布,可以开始往上面放图像了。所有你想在画布上显示的东西必须被加进一个被称作 舞台的Pixi对象中。你能够像这样使用舞台对象: app.stage 这个舞台是一个Pixi 容器对象。你能把它理解成一种将放进去的东西分组并存储的空箱子。 舞台对象是在你的场景中所有可见对象的根容器。所有你放进去的东西都会被渲染到canvas中。现在舞台是空的,但是很快我们就会放进去一点