owned this note
owned this note
Published
Linked with GitHub
# 用 python 做遊戲!
使用 pygame 模組製作,它專門用來製作遊戲。可以非常簡單的生成界面,以及載入圖像、聲音,並控制其輸出。

### 生成介面
特別提醒 : 如果沒有loop迴圈的話 介面會馬上關閉喔~
```python=
pygame.display.set_mode((window_width, window_height)
```
### 載入圖片
```python=
pygame.image.load('picture.png')
```
### 將圖片繪製出來
```python=
pygame.display.set_mode((window_width, window_height)
.blit(pygame.image.load('picture.png'),(x,y))
```
### 聲音
```python=
pygame.mixer.music.load("music.mp3")
# 撥放音樂
pygame.mixer.music.play()
# 暫停音樂
pygame.mixer.music.pause()
```
### 偵測鍵盤
```python=
pygame.KEYDOWN
```
用以上功能加上一些邏輯就能做出遊戲來啦
### 實作 接冰激淋
#### 遊戲方法
讓小豬避開維尼把冰激淋都接起來!接到會加分,沒接到會扣分喔
#### 設計邏輯

#### 成果展示

#### 程式碼
```python=
import sys
import pygame
import random
import time
pygame.init()
window_width = 800
window_height = 512
FPS = 30
john_width = 88
john_height = 128
ice_cream_width = 50
ice_cream_height = 82
STEP = 5
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
clock = pygame.time.Clock()
DISPLAY = pygame.display.set_mode((window_width, window_height))
john = pygame.image.load('john.png')
ice_cream = pygame.image.load('ice_cream.png')
jcn = pygame.image.load('jcn.png')
cdd = pygame.image.load('cdd.png')
point_sound = pygame.mixer.Sound('eat.ogg')
smallfont = pygame.font.Font(None, 28)
menufont = pygame.font.Font(None, 48)
overfont = pygame.font.Font(None, 60)
pygame.display.set_caption("現在我有冰激淋")
def gameloop():
start()
pygame.mixer.music.load("bgm.ogg")
pygame.mixer.music.play(-1, 0.0)
points = 0
jonh_x, jonh_y = window_width/2, window_height/2+john_height
ice_cream_x, ice_cream_y = random.randrange(
0.2*window_width, 0.8*window_width, STEP), 0
cdd_x, cdd_y = random.randrange(
0.2*window_width, 0.8*window_width, STEP), 0
direction = "left" # 起始靠左
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
direction = "left"
elif event.key == pygame.K_RIGHT:
direction = "right"
elif event.key == pygame.K_SPACE:
direction = "stop"
elif event.key == pygame.K_ESCAPE:
pause()
# 自動左右
if direction == "left":
jonh_x -= STEP
elif direction == "right":
jonh_x += STEP
DISPLAY.fill(white)
if (eat_ice_cream(jonh_x, jonh_y, ice_cream_x, ice_cream_y)):
ice_cream_x, ice_cream_y = random.randrange(
0.2*window_width, 0.8*window_width, STEP), 0
points += 1
# 東西往下降
ice_cream_y += STEP
cdd_y += STEP
if ice_cream_y >= window_height:
ice_cream_x, ice_cream_y = random.randrange(
0.2*window_width, 0.8*window_width, STEP), 0
points -= 1
if cdd_y >= window_height:
cdd_x, cdd_y = random.randrange(
0.2*window_width, 0.8*window_width, STEP), 0
DISPLAY.blit(ice_cream, (ice_cream_x, ice_cream_y))
DISPLAY.blit(john, (jonh_x, jonh_y))
DISPLAY.blit(cdd, (cdd_x, cdd_y))
text = smallfont.render('Score: ' + str(points), True, black)
DISPLAY.blit(text, (0, 0))
cdd_crash(jonh_x, jonh_y, cdd_x, cdd_y, points)
# 出界
if jonh_x < 0 or jonh_x > 720:
game_over(points)
clock.tick(FPS)
pygame.display.update()
def eat_ice_cream(jonh_x, jonh_y, ice_cream_x, ice_cream_y):
if(jonh_x - ice_cream_x) <= ice_cream_width and (ice_cream_x - jonh_x) <= john_width:
if (jonh_y-ice_cream_y) <= ice_cream_height and (ice_cream_y-jonh_y) <= john_height:
point_sound.play()
return True
def cdd_crash(jonh_x, jonh_y, cdd_x, cdd_y, points):
if(jonh_x-cdd_x) <= john_width and (cdd_x-jonh_x) <= john_width:
if (jonh_y-cdd_y) <= john_height and (cdd_y - jonh_y) <= john_height:
game_over(points)
def start():
point = 0
while True:
DISPLAY.fill(white)
if point == 0:
start = menufont.render('ANY KEY START', True, black)
DISPLAY.blit(start, (window_width/3.2, 5*window_height/10))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
return True
clock.tick(FPS)
pygame.display.update()
def game_over(points):
DISPLAY.fill(red)
gameover = overfont.render('GAME OVER', True, black)
score = overfont.render('SCORE: '+str(points), True, black)
DISPLAY.blit(gameover, (window_width/3.2, window_height/10))
DISPLAY.blit(jcn, (window_width/2.8, 2*window_height/10))
DISPLAY.blit(score, (window_width/3.2, 8*window_height/10))
pygame.display.update()
pygame.mixer.music.pause()
time.sleep(2)
gameloop()
def pause():
pause = overfont.render('PAUSED', True, red)
DISPLAY.blit(pause, (0.4*window_width, window_height/3))
pygame.mixer.music.pause()
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.mixer.music.play()
return
gameloop()
```