如何制作角色并与碰撞一起移动?
我正在进行2D款游戏,到目前为止,我做得很好。我的最后一个问题是制作地图。
这是地图的代码:
import pygame, sys
BLUE = (0, 0, 255)
SAND = (194, 176, 128)
GRASS = (124, 252, 0)
FOREST = (0, 100, 0)
TileColor = {'W': BLUE, 'S': SAND, 'G': GRASS, 'F': FOREST}
map1 = ["WWWWWWWWWWWWWWWWWWWWWWW",
"WWWWWWWWWGGGWWWWWWWWWWW",
"WWWWWGGGGGGGGGGGWWWWWWW",
"WWWWGGGGGFFFGGGGGGWWWWW",
"WWWGGGGGFFFFFFGGGGGWWWW",
"WWWGGGGGGFFFFFGGGGGGWWW",
"WWGGGGGGGGGFFGGGGGGGWWW",
"WWGGGGGGGGGGGGGGGGGGGWW",
"WWGGGGGGSSSSSSSGGGGGGGW",
"WWGGGGSSSSSSSSSSGGGGGGW",
"WGGGGGGGSSGGGGGGGGGGGSW",
"WGGGGGGGGGGGGGGGGGGGSSW",
"WSGGGGGGGGFFGGGGGGGGSSW",
"WSSGGGGGGFFFGGGGGFFGGSW",
"WSSGGGGGFFFFFFGGFFFFFGW",
"WSGGGGFFFFFFFFFFFFFFGGW",
"WWGGGGGFFFFFFFFFFFFGGWW",
"WWGGGGGGGFFFFFFFFGGGWWW",
"WWWWGGGGGGGGFFGGGGGWWWW",
"WWWWWWSSSSSGGGGSSSWWWWW",
"WWWWWWWWWSSSSSSSSWWWWWW",
"WWWWWWWWWWWWWWSWWWWWWWW",
"WWWWWWWWWWWWWWWWWWWWWWW"
]
TILESIZE = 22
MAPWIDTH = 23
MAPHEIGHT = 23
pygame.init()
DISPLAY = pygame.display.set_mode((MAPWIDTH * TILESIZE, MAPHEIGHT * TILESIZE))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
for row in range(MAPHEIGHT):
for col in range(MAPWIDTH):
pygame.draw.rect(
DISPLAY, TileColor[map1[row][col]],
(col * TILESIZE, row * TILESIZE, TILESIZE, TILESIZE))
pygame.display.update()
让我得到了:
(水)。关于我该怎么做的任何建议?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符由网格中的一排列表示:
使用键盘事件更改字符的位置(请参见如何在pygame中获取键盘输入?)。跳过字符运动如果新角色位置不在草地上(
map1 [C_row] [C_COL] =='G'
):在角色的位置绘制矩形:
完整示例:
The character is represented by a row an column in the grid:
Use the keyboard events to change the position of the character (see How to get keyboard input in pygame?). Skip character movement if new character position is not on grass (
map1[c_row][c_col] == 'G'
):Draw a rectangle at the character's position:
Complete example:
这是
最后一个我想提到的pygame的不错的入门代码:
我从这个系列中学到了很多, rubato 和 pyglet 客观上好得多。
只是为了显示,上面的相同代码在Rubato中的行较少的方式可以实现:
Here is some nice starter code for PyGame
Last I'd like to mention two things:
I learned so much from this series by DaFluffyPotato.
If you are coding a game for python, while PyGame has the largest community alternatives like rubato and pyglet are objectively much better.
Just to show, the same code above is achievable in way fewer lines in rubato:
您要做的第一件事是创建一个新的python文件,并将其命名 player.py ,您应该做的下一件事就是将player.py import.py import.py import.py。
现在,请确保您已将pygame导入 player.py 文件
现在让我们创建一个名为“ player”的类(确保其具有资本“ P”)
在此类中,创建函数 init
您要做的接下来要做的就是创建一个函数,该函数在屏幕上绘制播放器(毕竟没有玩家的游戏的乐趣)
下一件事要做的是返回到您的主python文件,并创建一个名为Check的新功能
,请确保在主文件中编写此代码行:
现在返回您的pygame中的以获取事件.................................... /strong>并写下所有这些:
如果您收到任何错误,请给我repl链接,我会尝试修复它!
The first thing you have to do is create a new python file and name it player.py, the next thing you should do is import player.py to the main file.
Now make sure you have imported Pygame to the player.py file
Now lets create a class called "Player"(make SURE it has a CAPITAL "P")
and inside of this class create the function init
The next thing you have to do is create a function that draws the player on the screen(after all what's the fun of a game without the player)
The next thing to do is go back to your main python file and create a new function named check
now make sure in your main file to write this line of code:
now go back to your for event in pygame...... and write all of this:
if you recieve any errors please give me the repl link and I will try to fix it!