0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

pygameでキャラを歩かせる

Posted at

pygameでゼルダの伝説みたいな感じのゲームを作ってみたいと思い、色々試作中。
自分用メモ

import pygame
import sys
from pygame.locals import *

player_img = pygame.image.load('chara_img/pipo-charachip017c.png')
tmr = 0
ANIMATION = [1,0,1,2]
pygame.init()
pygame.display.set_caption('メイドさん')
screen = pygame.display.set_mode((400,400))
clock = pygame.time.Clock()
font = pygame.font.Font(None,80)

running = True
while running:
tmr += 1
# イベント処理
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False

# 画面をクリア
screen.fill((0, 0, 0))

#動き方のテスト
screen.blit(player_img,dest=[10,10],area=[(tmr%3)*32,0,32,32])
screen.blit(player_img,dest=[10,50],area=[(tmr%3)*32,32,32,32])
screen.blit(player_img,dest=[10,90],area=[(tmr%3)*32,64,32,32])
screen.blit(player_img,dest=[10,130],area=[(tmr%3)*32,96,32,32])
pygame.draw.rect(screen, (255, 0, 0), (5,5,40,170), width=2)

screen.blit(player_img,dest=[50,10],area=[ANIMATION[tmr%4]*32,0,32,32])
screen.blit(player_img,dest=[50,50],area=[ANIMATION[tmr%4]*32,32,32,32])
screen.blit(player_img,dest=[50,90],area=[ANIMATION[tmr%4]*32,64,32,32])
screen.blit(player_img,dest=[50,130],area=[ANIMATION[tmr%4]*32,96,32,32])
pygame.draw.rect(screen, (0, 0, 255), (51,5,40,170), width=2)

sur = font.render(str(f'{(tmr%4)}:{ANIMATION[tmr%4]}'),True,(255,255,255))
screen.blit(sur,[200,100])

pygame.display.flip()
pygame.display.update()
clock.tick(5)

pygame.quit()
sys.exit()

素材はぴぽや倉庫様より。
アニメーションのコードを参考にしたのは、Pythonで作るゲーム開発入門講座 実践編。

Surface.blit(source, dest, area=None, special_flags = 0): return Rect
→destで表示させたい画像の座標を指定できる。
→areaで読み込んだ画像中から、一部だけ切り取って描画範囲を指定できる。[x座標,y座標,幅,高さ]
32x32ピクセルの画像が3列4行。
始まりのx座標を32×0,32x1,32x3で指定すると、ちょうど1モーション分の画像が描写される。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?