-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampleGame.py
More file actions
167 lines (138 loc) · 5.32 KB
/
sampleGame.py
File metadata and controls
167 lines (138 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import pygame
import sys
import random
import math
from pygame import mouse
from pygame.locals import *
point = 0
#クラス、使えると便利(なくても大丈夫)
class board:
def __init__(self,row,col,posx,posy):
self.row = row
self.col = col
self.isDraw = True
self.posx = posx
self.posy = posy
def delete(self):
self.isDraw = False
def draw(self,screen):
if(self.isDraw):
pygame.draw.line(screen,(0,255,0),(self.posx,self.posy),(self.posx + 70,self.posy),10)
def collision(self,x,y,velx,vely,count):
global point
if(not(self.isDraw)):
return (velx, vely, count)
if((x > self.posx) and (x < self.posx + 70) and (y > self.posy - 5) and (y < self.posy + 5)):
self.delete()
setPoint(100)
return (velx, -vely, count - 1)
return (velx, vely, count)
#得点増加
def setPoint(add):
global point
point = point + add
def main():
global point
#pygameを使用するための文(必須)
pygame.init()
#画面を作成
screen = pygame.display.set_mode((500,400))
#タイマーを作成、フレームレートの固定に使用
ck = pygame.time.Clock()
#マウスの位置変数
mouseX = 40
mouseY = 360
#スピードアップカウント、これが一定以上の値になったら
speedUpCount = 0
#ボードの数、ゼロになったら終了
boardCount = 25
#玉の位置変数
pointX = random.randint(30,470)
pointY = random.randint(150,200)
#玉の速度の角度
velAngle = random.randint(0,360)
#玉の速度変数
pointVelX = math.cos(math.radians(velAngle))
pointVelY = math.sin(math.radians(velAngle))
#テキスト描画用のフォント作成
font = pygame.font.Font(None,32)
font2 = pygame.font.Font(None,24)
isFinish = False
#板のデータを作成
boards = []
for i in range(0,5):
for j in range(0,3):
boards.append(board(i,j,60 + i * 80,100 + j * 40))
while(1):
#イベント(マウスクリックやマウスを動かしたときの処理)
for event in pygame.event.get():
#終了(右上の×ボタン)を押したとき
if(event.type == QUIT):
#pygameの終了
pygame.quit()
#プログラムの終了
sys.exit()
#マウスを動かしたときの処理
if(event.type == MOUSEMOTION):
#マウスの位置を取得
mouseX,mouseY = event.pos
#マウスの位置が枠外なら戻す、y位置を固定
if(mouseX > 460):
mouseX = 460
elif(mouseX < 40):
mouseX = 40
mouseY = 360
#終了していたらループの最初に戻す
if(isFinish):
continue
#フレームレートを30に固定
ck.tick(30)
#カウントを増加
speedUpCount = speedUpCount + 1
#カウントが120以上なら速度を1.2倍しカウントを0にする
if(speedUpCount > 120):
pointVelX = pointVelX * 1.2
pointVelY = pointVelY * 1.2
speedUpCount = 0
#玉を移動させる
pointX = pointX + pointVelX * 4.2
pointY = pointY + pointVelY * 4.2
#玉が左右の線についたら跳ね返す
if((pointX < 25) or (pointX > 475)):
pointVelX = -pointVelX
#玉が上の線についたら跳ね返す
if(pointY < 85):
pointVelY = -pointVelY
if(pointY > 355 and pointY < 365):
if((pointX > mouseX - 20) and (pointX < mouseX + 20)):
pointVelY = -pointVelY
for b in boards:
(pointVelX, pointVelY, boardCount) = b.collision(pointX, pointY, pointVelX, pointVelY, boardCount)
#時間経過で得点を増やす
setPoint(1)
#画面を白で塗りつぶす
screen.fill((255,255,255))
#黒い枠を書く
pygame.draw.rect(screen,(0,0,0),Rect(20,80,460,300))
#↑は中も塗りつぶされるので白で中を塗りつぶして枠にする
pygame.draw.rect(screen,(255,255,255),Rect(22,82,456,296))
#自分の板を描画
pygame.draw.line(screen,(255,0,0),(mouseX - 20,mouseY),(mouseX + 20,mouseY),3)
#玉の描画
pygame.draw.circle(screen,(0,0,255),(int(pointX),int(pointY)),5)
score = font.render("score:" + str(point),True,(0,0,0))
screen.blit(score,[350,40])
#板を表示する
for b in boards:
b.draw(screen)
#玉が下にいったらFinishを表示させる
if(pointY > 400 or boardCount == 0):
txt = font.render("Finish.",True,(0,0,0))
result = font.render(str(point),True,(255,0,0))
screen.blit(txt,[225,200])
screen.blit(result,[225,250])
isFinish = True
#画面を更新、これがないと画面が変化しない
pygame.display.update()
if(__name__ == "__main__"):
main()