質問編集履歴
2
コードの変更
    
        title	
    CHANGED
    
    | @@ -1,1 +1,1 @@ | |
| 1 | 
            -
            ( | 
| 1 | 
            +
            (pygame)0.1秒ごとのデータをファイルに出力したい
         | 
    
        body	
    CHANGED
    
    | @@ -4,12 +4,8 @@ | |
| 4 4 | 
             
            今は、ドライブゲームでいうところの参加者が動かせる車を赤いrectで書いています。
         | 
| 5 5 |  | 
| 6 6 | 
             
            ### 発生している問題・エラーメッセージ
         | 
| 7 | 
            -
            赤いrect(車)のx座標を0.1秒ごとに取得したいのですが、データ | 
| 7 | 
            +
            赤いrect(車)のx座標を0.1秒ごとに取得したいのですが、最後のx座標のデータしか取得できません…。
         | 
| 8 8 |  | 
| 9 | 
            -
            ```
         | 
| 10 | 
            -
            エラーメッセージ
         | 
| 11 | 
            -
            AttributeError: 'WindowsPath' object has no attribute 'write'
         | 
| 12 | 
            -
            ```
         | 
| 13 9 |  | 
| 14 10 | 
             
            ### 該当のソースコード
         | 
| 15 11 |  | 
| @@ -18,19 +14,10 @@ | |
| 18 14 | 
             
            import pygame
         | 
| 19 15 | 
             
            from pygame.locals import *
         | 
| 20 16 | 
             
            import sys
         | 
| 21 | 
            -
            import pathlib
         | 
| 22 | 
            -
            import os
         | 
| 23 | 
            -
            import pprint
         | 
| 24 17 |  | 
| 25 | 
            -
            os.makedirs('temp', exist_ok=True)
         | 
| 26 | 
            -
             | 
| 27 | 
            -
            p_file = pathlib.Path('temp/file1.txt')
         | 
| 28 | 
            -
            print(p_file)
         | 
| 29 | 
            -
            print(type(p_file))
         | 
| 30 | 
            -
            p_file.touch()
         | 
| 31 | 
            -
             | 
| 32 18 | 
             
            BLACK = (0, 0, 0)
         | 
| 33 19 | 
             
            RED = (255, 0, 0)
         | 
| 20 | 
            +
            WHITE = (255, 255, 255)
         | 
| 34 21 |  | 
| 35 22 | 
             
            pygame.init() #初期化
         | 
| 36 23 | 
             
            screen = pygame.display.set_mode((640, 480)) #画面サイズ
         | 
| @@ -39,12 +26,41 @@ | |
| 39 26 | 
             
            pygame.display.flip()
         | 
| 40 27 |  | 
| 41 28 | 
             
            x = 320
         | 
| 29 | 
            +
            pos_2y=380
         | 
| 30 | 
            +
            pos_3y=480
         | 
| 31 | 
            +
            pos_4y=380
         | 
| 32 | 
            +
            pos_5y=480
         | 
| 33 | 
            +
            pos_6y=330
         | 
| 34 | 
            +
            pos_7y=380
         | 
| 35 | 
            +
            pos_8y=330
         | 
| 36 | 
            +
            pos_9y=380
         | 
| 42 37 |  | 
| 38 | 
            +
            velocity_y=0.1
         | 
| 39 | 
            +
             | 
| 43 40 | 
             
            while True:
         | 
| 41 | 
            +
             with open('sample.txt', 'w') as f:
         | 
| 44 | 
            -
             | 
| 42 | 
            +
              print(x, file=f)
         | 
| 45 | 
            -
              | 
| 43 | 
            +
             pos_2y+=velocity_y
         | 
| 46 | 
            -
             | 
| 44 | 
            +
             pos_3y+=velocity_y
         | 
| 45 | 
            +
             pos_4y+=velocity_y
         | 
| 46 | 
            +
             pos_5y+=velocity_y
         | 
| 47 | 
            +
             pos_6y+=velocity_y
         | 
| 48 | 
            +
             pos_7y+=velocity_y
         | 
| 49 | 
            +
             pos_8y+=velocity_y
         | 
| 50 | 
            +
             pos_9y+=velocity_y
         | 
| 47 51 | 
             
             screen.fill(BLACK)
         | 
| 52 | 
            +
             st1=(330, pos_2y)
         | 
| 53 | 
            +
             en1=(330, pos_3y)
         | 
| 54 | 
            +
             pygame.draw.line (screen, WHITE, st1, en1)
         | 
| 55 | 
            +
             st2=(320, pos_4y)
         | 
| 56 | 
            +
             en2=(320, pos_5y)
         | 
| 57 | 
            +
             pygame.draw.line (screen, WHITE, st2, en2)
         | 
| 58 | 
            +
             st3=(280, pos_6y)
         | 
| 59 | 
            +
             en3=(330, pos_7y)
         | 
| 60 | 
            +
             pygame.draw.line (screen, WHITE, st3, en3)
         | 
| 61 | 
            +
             st4=(270, pos_8y)
         | 
| 62 | 
            +
             en4=(320, pos_9y)
         | 
| 63 | 
            +
             pygame.draw.line (screen, WHITE, st4, en4)
         | 
| 48 64 | 
             
             rect1 = (x, 400, 10, 5) #動かすほう
         | 
| 49 65 | 
             
             pygame.draw.rect(screen, RED, rect1)
         | 
| 50 66 | 
             
             pygame.display.update()
         | 
1
(コード短くしました)
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -1,11 +1,10 @@ | |
| 1 1 | 
             
            ### 前提・実現したいこと
         | 
| 2 2 |  | 
| 3 3 | 
             
            pygameを用いて、アクションゲーム(ドライブゲーム)のようなものを作りたいと思っています。
         | 
| 4 | 
            -
            今は、ドライブゲームでいうところの参加者が動かせる車を赤いrectで | 
| 4 | 
            +
            今は、ドライブゲームでいうところの参加者が動かせる車を赤いrectで書いています。
         | 
| 5 5 |  | 
| 6 6 | 
             
            ### 発生している問題・エラーメッセージ
         | 
| 7 7 | 
             
            赤いrect(車)のx座標を0.1秒ごとに取得したいのですが、データが取得できません…。
         | 
| 8 | 
            -
            (ちなみにまだ道は作成途中です、すみません)
         | 
| 9 8 |  | 
| 10 9 | 
             
            ```
         | 
| 11 10 | 
             
            エラーメッセージ
         | 
| @@ -32,7 +31,6 @@ | |
| 32 31 |  | 
| 33 32 | 
             
            BLACK = (0, 0, 0)
         | 
| 34 33 | 
             
            RED = (255, 0, 0)
         | 
| 35 | 
            -
            WHITE = (255, 255, 255)
         | 
| 36 34 |  | 
| 37 35 | 
             
            pygame.init() #初期化
         | 
| 38 36 | 
             
            screen = pygame.display.set_mode((640, 480)) #画面サイズ
         | 
| @@ -41,42 +39,12 @@ | |
| 41 39 | 
             
            pygame.display.flip()
         | 
| 42 40 |  | 
| 43 41 | 
             
            x = 320
         | 
| 44 | 
            -
            pos_2y=380
         | 
| 45 | 
            -
            pos_3y=480
         | 
| 46 | 
            -
            pos_4y=380
         | 
| 47 | 
            -
            pos_5y=480
         | 
| 48 | 
            -
            pos_6y=330
         | 
| 49 | 
            -
            pos_7y=380
         | 
| 50 | 
            -
            pos_8y=330
         | 
| 51 | 
            -
            pos_9y=380
         | 
| 52 42 |  | 
| 53 | 
            -
            velocity_y=0.01
         | 
| 54 | 
            -
             | 
| 55 43 | 
             
            while True:
         | 
| 56 44 | 
             
             print(x, file=p_file)
         | 
| 57 45 | 
             
             if(time.time()-start>20):
         | 
| 58 46 | 
             
              break
         | 
| 59 | 
            -
             pos_2y+=velocity_y
         | 
| 60 | 
            -
             pos_3y+=velocity_y
         | 
| 61 | 
            -
             pos_4y+=velocity_y
         | 
| 62 | 
            -
             pos_5y+=velocity_y
         | 
| 63 | 
            -
             pos_6y+=velocity_y
         | 
| 64 | 
            -
             pos_7y+=velocity_y
         | 
| 65 | 
            -
             pos_8y+=velocity_y
         | 
| 66 | 
            -
             pos_9y+=velocity_y
         | 
| 67 47 | 
             
             screen.fill(BLACK)
         | 
| 68 | 
            -
             st1=(330, pos_2y)
         | 
| 69 | 
            -
             en1=(330, pos_3y)
         | 
| 70 | 
            -
             pygame.draw.line (screen, WHITE, st1, en1)
         | 
| 71 | 
            -
             st2=(320, pos_4y)
         | 
| 72 | 
            -
             en2=(320, pos_5y)
         | 
| 73 | 
            -
             pygame.draw.line (screen, WHITE, st2, en2)
         | 
| 74 | 
            -
             st3=(280, pos_6y)
         | 
| 75 | 
            -
             en3=(330, pos_7y)
         | 
| 76 | 
            -
             pygame.draw.line (screen, WHITE, st3, en3)
         | 
| 77 | 
            -
             st4=(270, pos_8y)
         | 
| 78 | 
            -
             en4=(320, pos_9y)
         | 
| 79 | 
            -
             pygame.draw.line (screen, WHITE, st4, en4)
         | 
| 80 48 | 
             
             rect1 = (x, 400, 10, 5) #動かすほう
         | 
| 81 49 | 
             
             pygame.draw.rect(screen, RED, rect1)
         | 
| 82 50 | 
             
             pygame.display.update()
         | 
| @@ -95,6 +63,7 @@ | |
| 95 63 |  | 
| 96 64 |  | 
| 97 65 |  | 
| 66 | 
            +
             | 
| 98 67 | 
             
            ```
         | 
| 99 68 |  | 
| 100 69 | 
             
            ### 試したこと
         | 
