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でRPGをつくってみる7

Last updated at Posted at 2024-10-12

記事全体の目次へ移動

時間の計測

ここをクリックしてください

時間の計測をするコードです。
プレイ時間を表示するために使います。

import time

time_start = time.time()
hour = 0
min = 0
seco = 0

while True:
    time_end = time.time()
    elapsed_time = time_end - time_start

    if elapsed_time >= 1.0:
        seco += 1
        time_start = time.time()
        print(f"{hour}時間 {min}{seco}")

    if seco >= 60:
        min += 1
        seco = 0

    if min >= 60:
        hour += 1
        min = 0



年月日と時刻

ここをクリックしてください

年月日と時刻を計測するコードです。
セーブの更新日時を表示するために使います。

shiracamus(しらかみゅ)さんから頂いたコードです。
コメントアウトはcopilotが書きました。
copilotが言うには上のコードだけで0埋めもできるみたいですね。
すごい短さです。


import datetime
# 現在の日付と時刻を取得し、フォーマットして表示
print(f"{datetime.datetime.now():%Y/%m/%d %H:%M}")

# 特定の日時を設定し、フォーマットして表示
dt = datetime.datetime(2024, 1, 2, 3, 4)
print(f"{dt:%Y/%m/%d %H:%M}")

セーブ画面

ここをクリックしてください

スクリーンショット (702).png

ここでは一つのファイルに保存しています。

import pygame
import sys
from pygame.locals import *
import time
import datetime
import pickle

def draw_text(surface, message, pos, font, color=(255, 255, 255)):
    lines = message.split('\n')
    x, y = pos
    for line in lines:
        text_surface = font.render(line, True, color)
        surface.blit(text_surface, (x, y))
        y += text_surface.get_height()


pygame.init()
screen=pygame.display.set_mode((640,480))
pygame.display.set_caption('セーブ画面')
#ここにフォントのパスを貼り付ける
font = pygame.font.Font(パス,20)
my,mx = 0,0
save_state1 = 0
save_state2 = 0
save_state3 = 0


save_date = {}
save_on1 = ""
save_on2 = ""
save_on3 = ""
text_time1 = ""
text_time2 = ""
text_time3 = ""
now_time1 = ""
now_time2 = ""
now_time3 = ""
zero1 = 0
zero2 = 0
zero3 = 0
map_name1 = ""
map_name2 = ""
map_name3 = ""

map_name = "field"

white = (255,255,255)

status_state = 0

save_text = 0
save_time = 0

#項目
transparent_surface1 = pygame.Surface((100, 20), pygame.SRCALPHA)
transparent_surface1.fill((255,255,255, 128))  # RGBA (黒, 透明度128)

#セーブレクト
transparent_surface2 = pygame.Surface((630, 123), pygame.SRCALPHA)
transparent_surface2.fill((255,255,255, 128))  # RGBA (黒, 透明度128)

move_item1 = 0
move_item2 = 0
move_item3 = 0

seco = 0
min = 0
hour = 0
time_start = time.time()

running = True
while running:

    #プレイ時間
    time_end = time.time()
    elapsed_time = time_end - time_start

    if elapsed_time >= 1.0:
        seco += 1
        time_start = time.time()

    if seco >= 60:
        min += 1
        seco = 0

    if min >= 60:
        hour += 1
        min = 0

    


    screen.fill((0, 0, 0))  # 画面を黒色に塗りつぶし
    pygame.draw.rect(screen, white, Rect(5,5,630,100), 3)
    pygame.draw.rect(screen, white, Rect(5,110,630,365), 3)
    status_text = font.render("セーブ", True, white)
    screen.blit(status_text, (448,28))

    if my >= 28 and my <= 48:    
        if mx >= 448 and mx <= 548:
            screen.blit(transparent_surface1, (448, 28))
            status_state = 2

    if status_state == 2:                
        pygame.draw.rect(screen, white, Rect(5,110,630,123), 3)
        pygame.draw.rect(screen, white, Rect(5,230,630,123), 3)
        pygame.draw.rect(screen, white, Rect(5,350,630,125), 3)
        if save_state1 == 0:
            save_on1 = "記録がありません"
            text = font.render(save_on1, True, (255,255,255))
            screen.blit(text, (28, 130))
        elif save_state1 == 1:
            save_on1 = f"セーブデータ1               {text_time1}\n\n{map_name1}                  {now_time1}"
            draw_text(screen,save_on1,(28,130), font)
            
        if save_state2 == 0:
            save_on2 = "記録がありません"
            text = font.render(save_on2, True, (255,255,255))
            screen.blit(text, (28, 250))
        elif save_state2 == 1:
            save_on2 = f"セーブデータ2               {text_time2}\n\n{map_name2}                 {now_time2}"
            draw_text(screen,save_on2,(28,250), font)
            
        if save_state3 == 0:
            save_on3 = "記録がありません"
            text = font.render(save_on3, True, (255,255,255))
            screen.blit(text, (28, 370))
        elif save_state3 == 1:
            save_on3 = f"セーブデータ3               {text_time3}\n\n{map_name3}                  {now_time3}"
            draw_text(screen,save_on3,(28,370), font)
            
        if save_text == 1 and save_time == 0:
            pygame.draw.rect(screen, (0,0,0), (255,250,180,40))
            pygame.draw.rect(screen, white, Rect(255,250,180,40), 3)
            name_text = font.render("セーブしました", True, white)
            screen.blit(name_text, (275, 260))
            save_time = 1
        elif save_text == 1 and save_time == 1:
            time.sleep(1)
            save_text = 0
            save_time = 0

        # 半透明のSurfaceを描画
        if mx >= 5 and mx <= 635:
            if my >= 110  and my <= 228:
                screen.blit(transparent_surface2, (5, 110))
                move_item1 = 1
            else:
                move_item1 = 0
            if my >= 233  and my <= 348:
                screen.blit(transparent_surface2, (5, 230))
                move_item2 = 1
            else:
                move_item2 = 0
            if my >= 353  and my <= 473:
                screen.blit(transparent_surface2, (5, 350))
                move_item3 = 1
            else:
                move_item3 = 0
    #プレイ時間(ゼロ)        
    if hour < 10:
        zero1 = 0
    else:
        zero1 = ""
            
    if min < 10:
        zero2 = 0
    else:
        zero2 = ""
            
    if seco < 10:
        zero3 = 0
    else:
        zero3 = ""

        

    pygame.display.update()
    for event in pygame.event.get():
        mouse_pos = pygame.mouse.get_pos()
        mx = mouse_pos[0]
        my = mouse_pos[1]
        mouse_pressed = pygame.mouse.get_pressed()
        if mouse_pressed[0]:
            if status_state == 1:
                status_state = 2
                print(status_state)
            elif status_state == 2:
                #elif item_arrow == 1:
                    #if status_state == save:
                #セーブデータ1
                if move_item1 == 1:
                    save_state1 = 1
                    text_time1 = f"{zero1}{hour} : {zero2}{min} : {zero3}{seco}"
                    now_time1 = f"{datetime.datetime.now():%Y/%m/%d %H:%M}"
                    map_name1 = map_name
                    # ゲームの状態を辞書として定義  ,"":,
                    save_date = {"save_state1":save_state1,"save_state2":save_state2,"save_state3":save_state3,"zero1":zero1,"zero2":zero2,"zero3":zero3,
                        "save_on1":save_on1,"save_on2":save_on2,"save_on3":save_on3,"text_time1":text_time1,"text_time2":text_time2,"text_time3":text_time3,
                        "now_time1":now_time1,"now_time2":now_time2,"now_time3":now_time3,"map_name1":map_name1,"map_name2":map_name2,"map_name3":map_name3,
                        
                    }

                    # ゲームの状態をファイルに保存
                    with open('save_game.pickle', 'wb') as file:
                        pickle.dump(save_date, file)                                        

                    save_text = 1
                    move_item1 = 0

                #セーブデータ2
                if move_item2 == 1:
                    save_state2 = 1
                    text_time2 = f"{zero1}{hour} : {zero2}{min} : {zero3}{seco}"
                    now_time2 = f"{datetime.datetime.now():%Y/%m/%d %H:%M}"
                    map_name2 = map_name
                    # ゲームの状態を辞書として定義  ,"":
                    save_date = {"save_state1":save_state1,"save_state2":save_state2,"save_state3":save_state3,"zero1":zero1,"zero2":zero2,"zero3":zero3,
                        "save_on1":save_on1,"save_on2":save_on2,"save_on3":save_on3,"text_time1":text_time1,"text_time2":text_time2,"text_time3":text_time3,
                        "now_time1":now_time1,"now_time2":now_time2,"now_time3":now_time3,"map_name1":map_name1,"map_name2":map_name2,"map_name3":map_name3,
                        
                    }

                    # ゲームの状態をファイルに保存
                    with open('save_game.pickle', 'wb') as file:
                        pickle.dump(save_date, file)                                        

                    save_text = 1
                    move_item2 = 0
                    
                #セーブデータ3    
                if move_item3 == 1:
                    save_state3 = 1
                    text_time3 = f"{zero1}{hour} : {zero2}{min} : {zero3}{seco}"
                    now_time3 = f"{datetime.datetime.now():%Y/%m/%d %H:%M}"
                    map_name3 = map_name
                    # ゲームの状態を辞書として定義  ,"":
                    save_date = {"save_state1":save_state1,"save_state2":save_state2,"save_state3":save_state3,"zero1":zero1,"zero2":zero2,"zero3":zero3,
                        "save_on1":save_on1,"save_on2":save_on2,"save_on3":save_on3,"text_time1":text_time1,"text_time2":text_time2,"text_time3":text_time3,
                        "now_time1":now_time1,"now_time2":now_time2,"now_time3":now_time3,"map_name1":map_name1,"map_name2":map_name2,"map_name3":map_name3,
                        
                    }

                    # ゲームの状態をファイルに保存
                    with open('save_game.pickle', 'wb') as file:
                        pickle.dump(save_date, file)                                        

                    save_text = 1
                    move_item3 = 0


        elif event.type == QUIT:
            pygame.quit()
            sys.exit()
        


ここをクリックしてください


ここをクリックしてください


ここをクリックしてください


0
0
2

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?