鱼C论坛

 找回密码
 立即注册
查看: 840|回复: 7

[已解决]pygame:用subsurface实现动画的精灵 怎么碰撞检测(进来帮忙@些大佬吧)未解决

[复制链接]
发表于 2020-3-26 08:54:07 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 兔子BUNNY 于 2020-3-26 10:15 编辑
  1. import pygame
  2. from pygame.locals import *
  3. from sys import exit


  4. pygame.init()
  5. bg_size = width,height = 1600,900
  6. screen = pygame.display.set_mode(bg_size)
  7. pygame.display.set_caption("cxk小游戏")
  8. background = pygame.image.load("image/background.jpg")

  9. class Cxk(pygame.sprite.Sprite):
  10.     def __init__(self, filename, row, columns):
  11.         pygame.sprite.Sprite.__init__(self)

  12.         self.main_image = pygame.image.load(filename).convert_alpha()
  13.         self.main_rect = self.main_image.get_rect()
  14.         self.frame_width = self.main_rect.width//columns
  15.         self.frame_height = self.main_rect.height//row
  16.         self.rect = 0, 0, self.frame_width, self.frame_height
  17.         self.columns = columns
  18.         self.last_frame = row * columns - 1
  19.         self.last_time = 0
  20.         self.frame = 0
  21.         self.first_frame = 0
  22.         self.old_frame = 0
  23.         
  24.     def update(self, screen):
  25.         self.frame += 1                                                   # 帧序号 +1
  26.         if self.frame > self.last_frame:
  27.             self.frame = self.first_frame                                 # 循环播放
  28.         if self.frame != self.old_frame:
  29.             frame_x = (self.frame % self.columns) * self.frame_width           # 计算 subsurface 的 x 坐标
  30.             frame_y = (self.frame // self.columns) * self.frame_height         # 计算 subsurface 的 y 坐标
  31.             rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height) # 获取subsurface 的 rect
  32.             self.image = self.main_image.subsurface(rect)                    # 更新self.image
  33.             self.old_frame = self.frame                                        # 更新self.old_frame
  34.         screen.blit(self.image, self.main_rect)                                     # 显示图像
  35.     def move_right(self):
  36.         self.main_rect.left +=20
  37.         
  38.     def move_down(self):
  39.         self.main_rect.top +=20
  40.         
  41. class Cat(pygame.sprite.Sprite):
  42.     def __init__(self,image,):
  43.         pygame.sprite.Sprite.__init__(self)

  44.         self.image = pygame.image.load(image).convert_alpha()
  45.         self.rect = self.image.get_rect()
  46.         self.rect.left, self.rect.right = 450,300
  47.         
  48.         

  49. cxk = Cxk('image/cxk.png',1,16)

  50. cat=Cat('image/cat.png')

  51. framerate = pygame.time.Clock()

  52. while True:
  53.     framerate.tick(30)
  54.     for event in pygame.event.get():
  55.         if event.type == QUIT:
  56.             exit()
  57.     key_press = pygame.key.get_pressed()
  58.     if key_press[K_d]:
  59.         cxk.move_right()
  60.     if key_press[K_s]:
  61.         cxk.move_down()
  62.    
  63.     screen.blit(background, (0, 0))
  64.     screen.blit(cat.image,(450,300))  
  65.     cxk.update(screen)

  66.     if pygame.sprite.collide_rect(cat, cxk):
  67.         print('cxk')
  68.     pygame.display.update()
复制代码



用的是序列图,是1行,16列

这个是重写来进行碰撞检测的
https://pan.baidu.com/s/1FMKSSONrrm2pgdXKjAedew
最佳答案
2020-3-27 17:46:14
本帖最后由 一个账号 于 2020-3-30 17:31 编辑


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

使用道具 举报

发表于 2020-3-26 09:40:18 | 显示全部楼层
https://blog.csdn.net/zzwlyj/article/details/82290004
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-26 09:47:13 | 显示全部楼层
本帖最后由 六小鸭 于 2020-3-26 09:48 编辑

@一个账号
救救孩子!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-26 09:50:09 | 显示全部楼层
请把所用到的素材解压缩发过来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-26 10:15:51 | 显示全部楼层
一个账号 发表于 2020-3-26 09:50
请把所用到的素材解压缩发过来

好了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-27 17:46:14 | 显示全部楼层    本楼为最佳答案   
本帖最后由 一个账号 于 2020-3-30 17:31 编辑


链接打不开
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-3-30 17:30:19 | 显示全部楼层

已经自己搞定了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 00:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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