先ほども質問させていただきましたが、やり方を間違えしまい連投させていただきました。すみません。
↓のコードの☆の個所の数値を変えたら、フレームレートが変わりブロック崩しの玉の速度が速くなるはずなのですが、
難易度選択のためにシーン切り替えのプログラムを追加したら遅くなってしまいました。玉の速度をもっと早くするにはどうすばよいでしょうか。
Python
1#ブロック崩し 2 3import tkinter as tk 4from tkinter import messagebox 5 6#変数(追加) 7score=0 8time=0 9index=0 10 11#キー 12key="" 13def key(e): 14 global key 15 key=e.keysym 16 17#ウィンドウ 18win=tk.Tk() 19win.title("ブロック崩し") 20win.geometry("425x625") 21win.resizable 22win.bind("<KeyPress>", key) 23 24#キャンバス 25can=tk.Canvas(bg="black",width=400,height=600) 26can.place(x=10,y=10) 27 28 29#ゲームオーバー 30def gameOver(): 31 if index==1: 32 messagebox.showinfo("Information","GAME OVER") 33 exit() 34 35#ゲームクリア 36def gameClear(): 37 if index==1: 38 messagebox.showinfo("Information","CONGRATURATIONS") 39 exit() 40 41#ボール 42ball_x=50 43ball_y=500 44bx=5 45by=-5 46def drawBall(): 47 global ball_x 48 global ball_y 49 global bx 50 global by 51 can.create_oval(ball_x,ball_y,ball_x+20,ball_y+20,fill="white") 52 if ball_x<=0 or ball_x>=385: 53 bx *=-1 54 if ball_y<=0: 55 by *= -1 56 if ball_y>=600: 57 gameOver() 58 if ball_y>=560 and ball_x>=rack_x-10 and ball_x<=rack_x+70: 59 by *=-1 60 61 ball_x += bx;ball_y += by 62 63#ラケット 64rack_x=170 65keyPress_R=False 66keyPress_L=False 67def rightKeyPress(event): 68 global keyPress_R 69 keyPress_R=True 70def rightKeyRelease(event): 71 global keyPress_R 72 keyPress_R=False 73def leftKeyPress(event): 74 global keyPress_L 75 keyPress_L=True 76def leftKeyRelease(event): 77 global keyPress_L 78 keyPress_L=False 79win.bind("<KeyPress-Right>",rightKeyPress) 80win.bind("<KeyRelease-Right>",rightKeyRelease) 81win.bind("<KeyPress-Left>",leftKeyPress) 82win.bind("<KeyRelease-Left>",leftKeyRelease) 83 84 85def drawRacket(): 86 global rack_x 87 can.create_rectangle(rack_x,580,rack_x+60,595,fill="white") 88 if keyPress_R==True and rack_x<=350: 89 rack_x +=5 90 if keyPress_L==True and rack_x>=-10: 91 rack_x-=5 92 93#ブロック 94block=[] 95for x in range(5): 96 for y in range(4): 97 block.append({"x":x*80+5,"y":y*40+10,"st":1}) 98def drawBlock(): 99 global ball_x 100 global ball_y 101 global by 102 global score#(追加) 103 block_count=0 104 for i in range(len(block)): 105 x=block[i]["x"] 106 y=block[i]["y"] 107 st=block[i]["st"] 108 if ball_y<=y+30 and ball_x>=x-10 and ball_x<=x+60 and st==1: 109 by*=-1 110 block[i]["st"]=0 111 score=score+10#(追加) 112 if st==1: 113 can.create_rectangle(x,y,x+70,y+30,fill="white") 114 block_count+=1 115 if block_count==0: 116 gameClear() 117 118#スコア(追加) 119def drawScore(): 120 can.create_text(25,550,text="SCORE:"+str(score),fill="white") 121 122#時間 123def drawTime(): 124 can.create_text(25,570,text="Time:"+str(time),fill="white") 125 126#画面推移(追加) 127def gamemain(): 128 global index 129 if index==0: 130 can.create_text(200,100,text="ブロック崩し",anchor="center",font=("HG丸ゴシックM-PRO",24),fill="white") 131 can.create_text(200,500,text="Press [E]easy",anchor="center",font=("HG丸ゴシックM-PRO",24),fill="white") 132 can.create_text(200,535,text="[N]Normal",anchor="center",font=("HG丸ゴシックM-PRO",24),fill="white") 133 can.create_text(200,570,text="[H]Hard",anchor="center",font=("HG丸ゴシックM-PRO",24),fill="white") 134 if key=="e": 135 index=1 136 elif key=="n": 137 index=2 138 elif key=="h": 139 index=3 140 can.after(15,gamemain) 141 142 elif index==1: 143 def gameLoop1(): 144 can.delete("all") 145 drawBall() 146 drawRacket() 147 drawBlock() 148 drawScore()#(追加) 149 drawTime()#追加 150 win.after(30,gameLoop1)#☆☆☆☆☆☆☆☆☆☆ 151 gameLoop1() 152 153 elif index==2: 154 def gameLoop2(): 155 can.delete("all") 156 drawBall() 157 drawRacket() 158 drawBlock() 159 drawScore()#(追加) 160 drawTime()#追加 161 win.after(15,gameLoop2)#☆☆☆☆☆☆☆☆☆☆ 162 gameLoop2() 163 164 elif index==3: 165 def gameLoop3(): 166 can.delete("all") 167 drawBall() 168 drawRacket() 169 drawBlock() 170 drawScore()#(追加) 171 drawTime()#追加 172 win.after(5,gameLoop3)#☆☆☆☆☆☆☆☆☆☆ 173 gameLoop3() 174 175#表示ループ 176gamemain() 177 178 179#ウィンドウループ 180win.mainloop 181
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2021/10/16 17:07
2021/10/16 17:25
2021/10/16 18:12
2021/10/17 01:59