-
-
Save renyuanL/61e5aa28a7afd1245494 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame, random, sys | |
from pygame.locals import * | |
視窗寬度 = 1200 | |
視窗高度 = 700 | |
初始標題顏色 = (0, 0, 0) | |
背景顏色 = (255, 255, 255) | |
敵方速度 = 40 | |
敵方體型最小值 = 30 | |
敵方體型最大值 = 80 | |
敵方速度最小值 = 1 | |
敵方速度最大值 = 8 | |
敵方生成速度 = 25 | |
玩家速度 = 5 | |
def 終止(): | |
pygame.quit() | |
sys.exit() | |
def 等待使用者按下任意鍵(): | |
while True: | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
終止() | |
if event.type == KEYDOWN:#任意鍵被按下 | |
return | |
def 玩家撞到敵方(玩家位置, 敵人): | |
for b in 敵人: | |
if 玩家位置.colliderect(b['rect']): | |
return True | |
return False | |
def 文字顯示(內容, 字體內容, surface, x軸, y軸): | |
文句物件 = 字體內容.render(內容, 1, 初始標題顏色) | |
文句位置設定 = 文句物件.get_rect() | |
文句位置設定.topleft = (x軸, y軸) | |
surface.blit(文句物件, 文句位置設定) | |
# 設定 pygame, 視窗, 鼠標 | |
pygame.init() | |
標準時距 = pygame.time.Clock() | |
設定視窗介面 = pygame.display.set_mode((視窗寬度, 視窗高度)) | |
設定視窗介面.fill(背景顏色) | |
pygame.display.set_caption('Dodger!Dodger!Sketchman~~')#視窗上面顯示的標題 | |
pygame.mouse.set_visible(False)#顯示鼠標 | |
# 設定字體 | |
字體內容 = pygame.font.SysFont(None, 48) | |
# 音效設定 | |
失敗音效 = pygame.mixer.Sound('e1.wav') | |
pygame.mixer.music.load('m3.wav') | |
# 圖片設定 | |
玩家圖片 = pygame.image.load('p1.jpg') | |
玩家位置 = 玩家圖片.get_rect() | |
新玩家位置 = 玩家圖片.get_rect() | |
敵方圖片 = pygame.image.load('e1.jpg') | |
# 顯示初始畫面的文字 | |
文字顯示('Dodger!Dodger!Sketchman~~', 字體內容, 設定視窗介面, (視窗寬度 / 3), (視窗高度 / 3)) | |
文字顯示('Press a key to start!', 字體內容, 設定視窗介面, (視窗寬度 / 3) + 60, (視窗高度 / 3) + 50) | |
pygame.display.update() | |
等待使用者按下任意鍵() | |
最高紀錄 = 0 | |
while True: | |
# 初始化遊戲 | |
敵人 = [] | |
分數 = 0 | |
玩家位置.topleft = (10, 視窗高度 / 2) | |
左移 = 右移 = 上移 = 下移 = 跑 = False | |
移動 = True | |
敵方歸天 = 敵方慢慢來 = False | |
敵方計數 = 0 | |
玩家圖片暫存 = 1 | |
玩家計數 = 0 | |
pygame.mixer.music.play(-1, 0.0) | |
while True: # 遊戲開始後的迴圈 | |
分數 += 1 # 分數增加 | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
終止() | |
# 按鍵被按"下"後 | |
if event.type == KEYDOWN : | |
if event.key == ord(' '): | |
左移 = 右移 = 上移 = 下移 = 跑 = 移動 = False | |
玩家圖片 = pygame.image.load('p3.jpg') | |
新玩家位置 = 玩家圖片.get_rect() | |
新玩家位置.topleft = 玩家位置.topleft | |
玩家位置 = 新玩家位置 | |
玩家位置.move_ip(0, 45) | |
if event.key == ord('z'): | |
敵方歸天 = True | |
if event.key == ord('x'): | |
敵方慢慢來 = True | |
if 移動 == True: | |
if event.key == K_LEFT or event.key == ord('a'): | |
右移 = False | |
左移 = True | |
跑 = True | |
if event.key == K_RIGHT or event.key == ord('d'): | |
左移 = False | |
右移 = True | |
跑 = True | |
if event.key == K_UP or event.key == ord('w'): | |
下移 = False | |
上移 = True | |
跑 = True | |
if event.key == K_DOWN or event.key == ord('s'): | |
上移 = False | |
下移 = True | |
跑 = True | |
# 按鍵被"提"起後 | |
if event.type == KEYUP: | |
if event.key == ord(' '): | |
移動 = True | |
玩家圖片 = pygame.image.load('p1.jpg') | |
新玩家位置 = 玩家圖片.get_rect() | |
新玩家位置.topleft = 玩家位置.topleft | |
玩家位置 = 新玩家位置 | |
玩家位置.move_ip(0, -45) | |
if event.key == ord('z'): | |
敵方歸天 = False | |
分數 -= 100 | |
if event.key == ord('x'): | |
敵方慢慢來 = False | |
分數 -= 100 | |
if 移動 == True: | |
if event.key == K_ESCAPE: | |
終止() | |
if event.key == K_LEFT or event.key == ord('a'): | |
左移 = False | |
跑 = False | |
if event.key == K_RIGHT or event.key == ord('d'): | |
右移 = False | |
跑 = False | |
if event.key == K_UP or event.key == ord('w'): | |
上移 = False | |
跑 = False | |
if event.key == K_DOWN or event.key == ord('s'): | |
下移 = False | |
跑 = False | |
if 移動 == True: | |
if event.type == MOUSEMOTION: | |
# 用滑鼠來移動 | |
玩家位置.move_ip(event.pos[0] - 玩家位置.centerx, event.pos[1] - 玩家位置.centery) | |
玩家計數 += 1 | |
if (玩家圖片暫存==1) and 玩家計數 >= 3: | |
玩家圖片 = pygame.image.load('p2.jpg') | |
玩家圖片暫存 = 2 | |
玩家計數 = 0 | |
if 玩家圖片暫存==2 and 玩家計數 >= 3: | |
玩家圖片 = pygame.image.load('p1.jpg') | |
玩家圖片暫存 = 1 | |
玩家計數 = 0 | |
# Add new 敵人 at the top of the screen, if needed. | |
if not 敵方歸天 and not 敵方慢慢來: | |
敵方計數 += 1 | |
if 敵方計數 == 敵方生成速度: | |
敵方計數 = 0 | |
敵人大小 = random.randint(敵方體型最小值, 敵方體型最大值) | |
敵方徵兵 = {'rect': pygame.Rect(視窗寬度, random.randint(0, 視窗高度-敵人大小), 敵人大小, 敵人大小), | |
'speed': random.randint(敵方速度最小值, 敵方速度最大值), | |
'surface':pygame.transform.scale(敵方圖片, (敵人大小, 敵人大小)), | |
} | |
敵人.append(敵方徵兵) | |
# 玩家移動 | |
if 跑 : | |
玩家計數 += 1 | |
if (玩家圖片暫存==1) and 玩家計數 >= 3: | |
玩家圖片 = pygame.image.load('p2.jpg') | |
玩家圖片暫存 = 2 | |
玩家計數 = 0 | |
if 玩家圖片暫存==2 and 玩家計數 >= 3: | |
玩家圖片 = pygame.image.load('p1.jpg') | |
玩家圖片暫存 = 1 | |
玩家計數 = 0 | |
if 左移 and 玩家位置.left > 0: | |
玩家位置.move_ip(-1 * 玩家速度, 0) | |
if 右移 and 玩家位置.right < 視窗寬度: | |
玩家位置.move_ip(玩家速度, 0) | |
if 上移 and 玩家位置.top > 0: | |
玩家位置.move_ip(0, -1 * 玩家速度) | |
if 下移 and 玩家位置.bottom < 視窗高度: | |
玩家位置.move_ip(0, 玩家速度) | |
# 維持滑鼠鼠標在玩家目前的所在處 | |
pygame.mouse.set_pos(玩家位置.centerx, 玩家位置.centery) | |
# 當不是使用密技時 正常移動敵人的控制 | |
for b in 敵人: | |
if not 敵方歸天 and not 敵方慢慢來: | |
b['rect'].move_ip(-b['speed'],0) | |
elif 敵方歸天: | |
b['rect'].move_ip(+5, 0) | |
elif 敵方慢慢來: | |
b['rect'].move_ip(-1, 0) | |
# 刪除敵人當敵人掉到底部 | |
for b in 敵人[:]: | |
if b['rect'].left < 0: | |
敵人.remove(b) | |
# 設置視窗 | |
設定視窗介面.fill(背景顏色) | |
# 更改當前分數及最高紀錄 | |
文字顯示('Score: %s' % (分數), 字體內容, 設定視窗介面, 10, 0) | |
文字顯示('Top Score: %s' % (最高紀錄), 字體內容, 設定視窗介面, 10, 40) | |
# 繪製玩家 | |
設定視窗介面.blit(玩家圖片, 玩家位置) | |
# 顯示敵人 | |
for b in 敵人: | |
設定視窗介面.blit(b['surface'], b['rect']) | |
pygame.display.update() | |
# 確認 敵人 是否撞到玩家. | |
if 玩家撞到敵方(玩家位置, 敵人): | |
if 分數 > 最高紀錄: | |
最高紀錄 = 分數 # set new top 分數 | |
break | |
標準時距.tick(敵方速度) | |
# 停止遊戲並顯示結束訊息 | |
pygame.mixer.music.stop() | |
失敗音效.play() | |
文字顯示('GAME OVER', 字體內容, 設定視窗介面, (視窗寬度 / 3), (視窗高度 / 3)) | |
文字顯示('Press a key to play again.', 字體內容, 設定視窗介面, (視窗寬度 / 3) - 80, (視窗高度 / 3) + 50) | |
pygame.display.update() | |
等待使用者按下任意鍵() | |
失敗音效.stop() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment