马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 兔子BUNNY 于 2020-3-26 10:15 编辑 import pygame
from pygame.locals import *
from sys import exit
pygame.init()
bg_size = width,height = 1600,900
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("cxk小游戏")
background = pygame.image.load("image/background.jpg")
class Cxk(pygame.sprite.Sprite):
def __init__(self, filename, row, columns):
pygame.sprite.Sprite.__init__(self)
self.main_image = pygame.image.load(filename).convert_alpha()
self.main_rect = self.main_image.get_rect()
self.frame_width = self.main_rect.width//columns
self.frame_height = self.main_rect.height//row
self.rect = 0, 0, self.frame_width, self.frame_height
self.columns = columns
self.last_frame = row * columns - 1
self.last_time = 0
self.frame = 0
self.first_frame = 0
self.old_frame = 0
def update(self, screen):
self.frame += 1 # 帧序号 +1
if self.frame > self.last_frame:
self.frame = self.first_frame # 循环播放
if self.frame != self.old_frame:
frame_x = (self.frame % self.columns) * self.frame_width # 计算 subsurface 的 x 坐标
frame_y = (self.frame // self.columns) * self.frame_height # 计算 subsurface 的 y 坐标
rect = Rect(frame_x, frame_y, self.frame_width, self.frame_height) # 获取subsurface 的 rect
self.image = self.main_image.subsurface(rect) # 更新self.image
self.old_frame = self.frame # 更新self.old_frame
screen.blit(self.image, self.main_rect) # 显示图像
def move_right(self):
self.main_rect.left +=20
def move_down(self):
self.main_rect.top +=20
class Cat(pygame.sprite.Sprite):
def __init__(self,image,):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.right = 450,300
cxk = Cxk('image/cxk.png',1,16)
cat=Cat('image/cat.png')
framerate = pygame.time.Clock()
while True:
framerate.tick(30)
for event in pygame.event.get():
if event.type == QUIT:
exit()
key_press = pygame.key.get_pressed()
if key_press[K_d]:
cxk.move_right()
if key_press[K_s]:
cxk.move_down()
screen.blit(background, (0, 0))
screen.blit(cat.image,(450,300))
cxk.update(screen)
if pygame.sprite.collide_rect(cat, cxk):
print('cxk')
pygame.display.update()
用的是序列图,是1行,16列
这个是重写来进行碰撞检测的
https://pan.baidu.com/s/1FMKSSONrrm2pgdXKjAedew
本帖最后由 一个账号 于 2020-3-30 17:31 编辑
链接打不开
|