-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreakout.py
More file actions
213 lines (183 loc) · 5.85 KB
/
breakout.py
File metadata and controls
213 lines (183 loc) · 5.85 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# encoding: utf-8
import os, sys, random
import pygame as pg ,pygame
from pygame.locals import *
from draw import *
# 視窗大小
canvas_width = 800
canvas_height = 600
# 顏色
block = (0,0,0)
# 磚塊數量串列
bricks_list = []
# 移動速度
dx = 8
dy = -8
# 遊戲狀態
# 0:等待開球
# 1:遊戲進行中
game_mode = 0
#結束程式
def gameover(message):
global running
#顯示訊息
text = ffont.render(message, 1, (255,0,255))
screen.blit(text, (screen.get_width()/2-150,screen.get_height()/2-20))
pg.display.update() #更新畫面
time.sleep(5) #暫停5秒
running = False #結束程式
pg.init()
score = 0 #得分
dfont = pg.font.SysFont("Arial", 20) #下方訊息字體
ffont = pg.font.SysFont("SimHei", 32) #結束程式訊息字體
#-------------------------------------------------------------------------
# 函數:秀字
#-------------------------------------------------------------------------
def showFont( text, x, y):
global canvas
text = font.render(text, 1, (255, 0, 0))
canvas.blit( text, (x,y))
#-------------------------------------------------------------------------
# 函數:碰撞判斷.
# x : x
# y : y
# boxRect : 矩形
#-------------------------------------------------------------------------
def isCollision( x, y, boxRect):
if (x >= boxRect[0] and x <= boxRect[0] + boxRect[2] and y >= boxRect[1] and y <= boxRect[1] + boxRect[3]):
return True;
return False;
#-------------------------------------------------------------------------
# 函數:初始遊戲.
#-------------------------------------------------------------------------
def resetGame():
# 宣告使用全域變數
global game_mode, brick_num, bricks_list, dx, dy
# 磚塊
for bricks in bricks_list:
# 亂數磚塊顏色
r = random.randint(100,200)
g = random.randint(100,200)
b = random.randint(100,200)
bricks.color = [r,g,b]
# 開啟磚塊.
bricks.visivle = True
# 0:等待開球
game_mode = 0
# 磚塊數量.
brick_num = 99
# 移動速度.
dx = 8
dy = -8
# 初始.
pygame.init()
# 顯示Title.
pygame.display.set_caption(u"Breakout Game")
# 建立畫佈大小.
canvas = pygame.display.set_mode((canvas_width, canvas_height))
# 時脈.
clock = pygame.time.Clock()
# 設定字型-新細明體.
font = pygame.font.SysFont('新細明體', 18)
# 底板.
paddle_x = 0
paddle_y = (canvas_height - 48)
paddle = Box(pygame, canvas, "paddle", [paddle_x, paddle_y, 100, 24], (255,255,255))
# 球.
ball_x = paddle_x
ball_y = paddle_y
ball = Circle(pygame, canvas, "ball", [ball_x, ball_x], 8, (255,255,255))
# 建立磚塊
brick_num = 0
brick_x = 70
brick_y = 60
brick_w = 0
brick_h = 0
for i in range( 0, 99):
if((i % 11)==0):
brick_w = 0
brick_h = brick_h + 18
bricks_list.append (Box(pygame, canvas, "brick_"+str(i), [ brick_w + brick_x, brick_h+ brick_y, 58, 16], [255,255,255]))
brick_w = brick_w + 60
# 初始遊戲.
resetGame()
#-------------------------------------------------------------------------
# 主迴圈.
#-------------------------------------------------------------------------
running = True
while running:
#---------------------------------------------------------------------
# 判斷輸入.
#---------------------------------------------------------------------
for event in pygame.event.get():
# 離開遊戲.
if event.type == pygame.QUIT:
running = False
# 判斷按下按鈕
if event.type == pygame.KEYDOWN:
# 判斷按下ESC按鈕
if event.key == pygame.K_ESCAPE:
running = False
# 判斷Mouse.
if event.type == pygame.MOUSEMOTION:
paddle_x = pygame.mouse.get_pos()[0] - 50
if event.type == pygame.MOUSEBUTTONDOWN:
if(game_mode == 0):
game_mode = 1
#---------------------------------------------------------------------
# 清除畫面.
canvas.fill(block)
# 磚塊
for bricks in bricks_list:
# 球碰磚塊.
if(isCollision( ball.pos[0], ball.pos[1], bricks.rect)):
if(bricks.visivle):
# 扣除磚塊.
brick_num = brick_num -1
# 初始遊戲.
if(brick_num <= 0):
resetGame()
break
# 球反彈.
dy = -dy;
# 關閉磚塊.
bricks.visivle = False
# 更新磚塊.
bricks.update()
#顯示磚塊數量.
showFont( u"磚塊數量:"+str(brick_num), 8, 20)
# 秀板子.
paddle.rect[0] = paddle_x
paddle.update()
# 碰撞判斷-球碰板子.
if(isCollision( ball.pos[0], ball.pos[1], paddle.rect)):
# 球反彈.
dy = -dy;
# 球.
# 0:等待開球
if(game_mode == 0):
ball.pos[0] = ball_x = paddle.rect[0] + ( (paddle.rect[2] - ball.radius) >> 1 )
ball.pos[1] = ball_y = paddle.rect[1] - ball.radius
# 1:遊戲進行中
elif(game_mode == 1):
ball_x += dx
ball_y += dy
#判斷死亡.
if(ball_y + dy > canvas_height - ball.radius):
game_mode = 0
# 右牆或左牆碰撞.
if(ball_x + dx > canvas_width - ball.radius or ball_x + dx < ball.radius):
dx = -dx
# 下牆或上牆碰撞
if(ball_y + dy > canvas_height - ball.radius or ball_y + dy < ball.radius):
dy = -dy
ball.pos[0] = ball_x
ball.pos[1] = ball_y
# 更新球.
ball.update()
# 更新畫面.
pygame.display.update()
clock.tick(60)
# 離開遊戲.
pygame.quit()
quit()