鱼C论坛

 找回密码
 立即注册
查看: 160|回复: 5

[作品展示] 我用pygame做了一个可移动的地图

[复制链接]
发表于 2024-5-5 08:42:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 zxbin1 于 2024-5-5 08:52 编辑

background.png

  1. #模块#
  2. import pygame
  3. import sys
  4. from pygame.locals import *

  5. pass
  6. #模块#
  7. #精灵定义#
  8. class Jl(pygame.sprite.Sprite):
  9.     def __init__(self,image,speed):
  10.         pygame.sprite.Sprite.__init__(self) #初始化
  11.         self.image = pygame.image.load(image)#获得图片
  12.         self.rects = pygame.Rect(0,0,300,300) #获得图片矩形
  13.         self.speed = speed
  14.         self.width = self.image.get_width()
  15.         self.height = self.image.get_height()
  16.     def move(self):
  17.         self.rects.move_ip(self.speed)
  18.         
  19. #精灵定义#
  20. size = w,h = 300,300#设定背景长度

  21. #初始化 pygame
  22. pygame.init()

  23. #创建指定大小的窗口
  24. screen = pygame.display.set_mode(size)
  25. #设置窗口标题
  26. pygame.display.set_caption("第一个游戏")

  27. #创造精灵#
  28. jl = Jl("background.png",[0,0])
  29. #创造精灵#
  30. #定义控制帧率
  31. clock = pygame.time.Clock()

  32. while True:
  33.     for event in pygame.event.get():
  34.         if event.type == QUIT:
  35.             pygame.quit()
  36.             sys.exit()
  37.         if event.type == KEYDOWN:
  38.             if event.key == K_LEFT:
  39.                 jl.speed = [-10,0]
  40.             if event.key == K_RIGHT:
  41.                 jl.speed = [10,0]
  42.             if event.key == K_UP:
  43.                 jl.speed = [0,-10]
  44.             if event.key == K_DOWN:
  45.                 jl.speed = [0,10]
  46.         if event.type == KEYUP:
  47.             jl.speed = [0,0]
  48.             
  49.     jl.move()
  50.     if jl.rects.left < 0:
  51.         jl.rects.left = 0
  52.     if jl.rects.right > jl.width:
  53.         jl.rects.right = jl.width
  54.     if jl.rects.top < 0:
  55.         jl.rects.top = 0
  56.     if jl.rects.bottom > jl.height:
  57.         jl.rects.bottom = jl.height  
  58.     screen.fill((255,255,255))#背景填充
  59.     screen.blit(jl.image.subsurface(jl.rects),(0,0))
  60.     #更新界面
  61.     pygame.display.flip()
  62.     #延时
  63.     clock.tick(50)#可修改
复制代码

用键盘控制背景的位置
自己试过
十分有效

希望大家可以通过我的启发
作出新的游戏发在下面

评分

参与人数 4荣誉 +19 鱼币 +19 贡献 +17 收起 理由
python爱好者. + 5 + 5 + 3 鱼C有你更精彩 ^_^
~风介~ + 3 + 3 + 3 鱼C有你更精彩^_^
不二如是 + 6 + 6 + 6 鱼C有你更精彩^_^
青出于蓝 + 5 + 5 + 5 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-5-5 08:42:46 | 显示全部楼层
看起来你已经实现了一个可移动的地图,这是个不错的开始!不过我注意到你在代码中有几处缺失,让我帮你填一下:

首先,在精灵定义的部分,你没有给Jl类的初始化方法传递speed参数。我建议你在创建jl对象时,传入一个起始速度值,比如(0, 0),表示一开始jl对象不会移动。

其次,你在事件处理的部分,当按下键盘的上下左右键时,你应该为jl.speed赋予一个合适的速度值,比如(-1, 0)表示向左移动,或者(1, 0)表示向右移动。

最后,当按键弹起时,你可以将jl.speed设置为(0, 0),以停止jl对象的移动。

下面是修改后的代码:

  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. class Jl(pygame.sprite.Sprite):
  5.     def __init__(self, image, speed):
  6.         pygame.sprite.Sprite.__init__(self)
  7.         self.image = pygame.image.load(image)
  8.         self.rects = pygame.Rect(0, 0, 300, 300)
  9.         self.speed = speed
  10.         self.width = self.image.get_width()
  11.         self.height = self.image.get_height()

  12.     def move(self):
  13.         self.rects.move_ip(self.speed)

  14. size = w, h = 300, 300

  15. pygame.init()
  16. screen = pygame.display.set_mode(size)
  17. pygame.display.set_caption("第一个游戏")

  18. jl = Jl("background.png", (0, 0))

  19. clock = pygame.time.Clock()

  20. while True:
  21.     for event in pygame.event.get():
  22.         if event.type == QUIT:
  23.             pygame.quit()
  24.             sys.exit()
  25.         if event.type == KEYDOWN:
  26.             if event.key == K_LEFT:
  27.                 jl.speed = (-1, 0)
  28.             if event.key == K_RIGHT:
  29.                 jl.speed = (1, 0)
  30.             if event.key == K_UP:
  31.                 jl.speed = (0, -1)
  32.             if event.key == K_DOWN:
  33.                 jl.speed = (0, 1)
  34.         if event.type == KEYUP:
  35.             jl.speed = (0, 0)
  36.             
  37.     jl.move()
  38.     if jl.rects.left < 0:
  39.         jl.rects.left = 0
  40.     if jl.rects.right > jl.width:
  41.         jl.rects.right = jl.width
  42.     if jl.rects.top < 0:
  43.         jl.rects.top = 0
  44.     if jl.rects.bottom > jl.height:
  45.         jl.rects.bottom = jl.height  
  46.     screen.fill((255, 255, 255))
  47.     screen.blit(jl.image.subsurface(jl.rects), (0, 0))
  48.     pygame.display.flip()
  49.     clock.tick(50)
复制代码


希望这次能顺利运行!记得将图片文件 `background.png` 放在正确的路径下,并在代码中指定它的位置。祝你好运!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-5-5 08:46:32 | 显示全部楼层
???
这不一样吗?
???
我用list对象
@fishc用tuple对象
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-5-5 08:46:49 | 显示全部楼层
zxbin1 发表于 2024-05-05 08:46
???
这不一样吗?
???
我用list对象
@fishc ...


非常抱歉,我理解错了你的意思。如果你更喜欢使用列表对象而不是元组对象来表示速度,那么你可以按照你的方式进行修改。下面是使用列表对象的代码示例:

  1. import pygame
  2. import sys
  3. from pygame.locals import *

  4. class Jl(pygame.sprite.Sprite):
  5.     def __init__(self, image, speed):
  6.         pygame.sprite.Sprite.__init__(self)
  7.         self.image = pygame.image.load(image)
  8.         self.rects = pygame.Rect(0, 0, 300, 300)
  9.         self.speed = speed
  10.         self.width = self.image.get_width()
  11.         self.height = self.image.get_height()

  12.     def move(self):
  13.         self.rects.move_ip(self.speed[0], self.speed[1])

  14. size = w, h = 300, 300

  15. pygame.init()
  16. screen = pygame.display.set_mode(size)
  17. pygame.display.set_caption("第一个游戏")

  18. jl = Jl("background.png", [0, 0])

  19. clock = pygame.time.Clock()

  20. while True:
  21.     for event in pygame.event.get():
  22.         if event.type == QUIT:
  23.             pygame.quit()
  24.             sys.exit()
  25.         if event.type == KEYDOWN:
  26.             if event.key == K_LEFT:
  27.                 jl.speed = [-1, 0]
  28.             if event.key == K_RIGHT:
  29.                 jl.speed = [1, 0]
  30.             if event.key == K_UP:
  31.                 jl.speed = [0, -1]
  32.             if event.key == K_DOWN:
  33.                 jl.speed = [0, 1]
  34.         if event.type == KEYUP:
  35.             jl.speed = [0, 0]
  36.             
  37.     jl.move()
  38.     if jl.rects.left < 0:
  39.         jl.rects.left = 0
  40.     if jl.rects.right > jl.width:
  41.         jl.rects.right = jl.width
  42.     if jl.rects.top < 0:
  43.         jl.rects.top = 0
  44.     if jl.rects.bottom > jl.height:
  45.         jl.rects.bottom = jl.height  
  46.     screen.fill((255, 255, 255))
  47.     screen.blit(jl.image.subsurface(jl.rects), (0, 0))
  48.     pygame.display.flip()
  49.     clock.tick(50)
复制代码


这个版本代码与之前的代码唯一不同的地方就是速度列表的表示方式。使用列表和元组都可以实现相同的功能,只是在所使用的数据结构上有细微的差别。希望这次能满足你的需求!如果还有其他问题请随时告诉我。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-5-5 10:46:50 | 显示全部楼层
有趣
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-5-5 16:47:15 | 显示全部楼层
zxbin1 发表于 2024-5-5 08:46
???
这不一样吗?
???

这个就是会自己回复,不必管它
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-19 00:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表