import random
import pygame
import sys
def figure(screen, photo, x, y):
content = pygame.image.load(photo)
screen.blit(content, (x, y))
def ziti(screen, text, x, y, size = 24,color=(232, 206, 144)):
font = pygame.font.Font("ziti.ttf", size)
img_text = font.render(text, True, color) # 設定文字參數,内容,鋸齒話,顔色
screen.blit(img_text, (x, y)) # 将文字放在螢幕上
def run():
screen_w, screen_h = 640, 480
score = 0
hp = 3
speed = 1
ball_x, ball_y = 400, 0
ban_x, ban_y, ban_width, ban_height = 400, screen_h - 40, 220, 100
bg_volume = 0.9
sound_volume = 0.4
start = "請點選開始遊戲"
result = ""
title = "接亞瑟"
sound_score = "C:/Users/Administrator/Desktop/塗博仁程式設計檔案夾(隻能用Python打開的程式)/hit_wall.wav"
ball_figure = "yase.png"
bj_figure = "bj.jpg"
bg_music = "bj.mp3"
ban ="ban5.png"
game_over = True
# 檢測按鍵是不是按着不放
moving_left = False
moving_right = False
moving_up = False
moving_down = False
screen = pygame.display.set_mode((screen_w, screen_h)) # 2.建立一個視窗,設定大小
pygame.display.set_caption(title)
hit = pygame.mixer.Sound(sound_score) # 加載音樂
hit.set_volume(sound_volume) # 設定音量
pygame.mixer.music.load(bg_music)
pygame.mixer.music.set_volume(bg_volume)
pygame.mixer_music.play(-1)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONUP:
if game_over:
game_over = False
score = 0
hp = 4
elif event.type == pygame.MOUSEMOTION:
ban_x, _ = event.pos
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
moving_left = True
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
moving_right = True
elif event.key == pygame.K_w or event.key == pygame.K_UP:
moving_up = True
elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
moving_down = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_a or event.key == pygame.K_LEFT:
moving_left = False
elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
moving_right = False
elif event.key == pygame.K_w or event.key == pygame.K_UP:
moving_up = False
elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
moving_down = False
figure(screen, bj_figure, 0, 0)
if game_over:
ziti(screen, start, 100,screen_h / 2,50)
ziti(screen, result, 100, screen_h / 2 + 50,50)
else:
ball_y = ball_y + speed
if moving_right:
ban_x += 30
elif moving_left:
ban_x -= 30
elif moving_down:
ban_y += 30
elif moving_up:
ban_y -= 30
# 沒接到小球的判定
if ball_y > screen_h:
ball_x = random.randint(0, screen_w)
ball_y = 0
hp = hp - 1
speed = 1
if hp == 0:
start = "重新開始請點選螢幕"
result = "你擊殺了%d個小球" % score
game_over = True
# 接到小球的判定結果
if ban_x <= ball_x <= ban_x + ban_width and ban_y <= ball_y <= ban_y + ban_height:
score = score + 1
speed = speed + 1
hit.play()
ball_x = random.randint(0, 600)
ball_y = 0
ziti(screen, '分數:%d' % score, 0, 0)
ziti(screen, "生命值:%d" % hp, screen_w - 120, 0)
figure(screen, ball_figure, ball_x, ball_y)
#pygame.draw.rect(screen, (100, 255, 0), (ban_x, ban_y, ban_width, ban_height), 0)
pygame.display.update() # 3.不斷循環
def main():
pygame.init() # 1.遊戲初始化
pygame.mixer.init() # 2.音樂初始化
run()
if __name__ == '__main__':
main()