repl.it
@yulin1127/

python-basic-hw23-7

Pygame

No description

fork
loading
Files
  • main.py
  • airforce.jpeg
  • ghost.jpg
  • nohup.out
main.py
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
import random

import pygame


pygame.init()

window_size = [800, 600]
screen = pygame.display.set_mode(window_size)
screen.fill([255, 255, 255])

raw_airforce = pygame.image.load('./airforce.jpeg')
# 縮小圖片至尺寸為112x112
airforce = pygame.transform.scale(raw_airforce, (112, 112))
# 顯示飛機圖片到視窗上 (0, 300) 位置,記錄好初始位置
airforce_x = 0
airforce_y = 300
airforce_move_x = 0
airforce_move_y = 0

raw_ghost = pygame.image.load('./ghost.jpg')
# 縮小圖片至尺寸為112x112
ghost = pygame.transform.scale(raw_ghost, (120, 120))
# 顯示鬼圖片到視窗上 (300, 300) 位置,記錄好初始位置
ghost_x = 300
ghost_y = 300

# 宣告 font 文字物件
head_font = pygame.font.SysFont(None, 60)
# 渲染方法會回傳 surface 物件
text_surface = head_font.render('airforce vs ghort', True, (0, 0, 0))


# 繪製圖片初始位置
screen.blit(airforce, (airforce_x, airforce_y))
screen.blit(ghost, (ghost_x, ghost_y))

# 這個是決定是否跳出遊戲迴圈的變數
is_game_over = False
# 取出起始時間
time_start = pygame.time.get_ticks()
# 文字模組,用來顯示文字,可用來顯示儀表板資料。第一個參數為文字內容(這邊因為初始值為 None),第二個為大小
point_font = pygame.font.SysFont(None, 30)
time_font = pygame.font.SysFont(None, 30)
# 遊戲分數
points = 0
# 總遊戲時間
total_time = 30

while not is_game_over:
    # 目前時間
    time_now = pygame.time.get_ticks()
    # 取出當下時間,若時間超過 30 秒,則印出 over 30secs 遊戲結束
    during_time = (time_now - time_start) // 1000

    if during_time >= 30:
        print('over 30 secs\ngame over')
        is_game_over = True

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            is_game_over = True
        #使用鍵盤上下左右控制戰鬥機
        keys = pygame.key.get_pressed()
        if keys[pygame.K_UP]:
            airforce_move_y = -10
            airforce_y += airforce_move_y
        elif keys[pygame.K_DOWN]:
            airforce_move_y = +10
            airforce_y += airforce_move_y
        elif keys[pygame.K_RIGHT]:
            airforce_move_x = +10
            airforce_x += airforce_move_x
        elif keys[pygame.K_LEFT]:
            airforce_move_x = -10
            airforce_x += airforce_move_x

        # 取得圖片範圍大小的矩形物件,可以想成是圖片物件,可以進一步取得長寬等屬性。
        ghost_rect = ghost.get_rect()
        airforce_rect = airforce.get_rect()
        # 取出圖片寬度
        ghost_width = ghost_rect.width
        airforce_width = airforce_rect.width
        # 取出圖片長度
        ghost_height = ghost_rect.height
        airforce_heigt = airforce_rect.height
        # 判斷戰鬥機圖片及鬼圖片是否重疊
        if ghost_x + ghost_width > airforce_x and ghost_x < airforce_x + airforce_width and ghost_y + ghost_height > airforce_y and ghost_y < airforce_y + airforce_heigt:
            points += 5
            # 當被點擊後鬼圖片隨機產生新位置
            ghost_x = random.randint(0, 800 - ghost_width)
            ghost_y = random.randint(0, 600 - ghost_height)
            screen.blit(ghost, (ghost_x, ghost_y))


    screen.fill([255, 255, 255])
    # 遊戲剩餘時間
    remaining_time = total_time -  during_time
    # 製作遊戲分數文字元素
    point_font_surface = point_font.render('Points: {}'.format(points), True, (0, 0, 0))
    # 繪製遊戲分數文字到視窗
    screen.blit(point_font_surface, (10, 0))
    # 製作剩餘時間文字元素
    time_font_surface = time_font.render('Time: {}'.format(remaining_time), True, (0, 0, 0))
    # 繪製剩餘時間文字到視窗
    screen.blit(time_font_surface, (10, 30))
    # 繪製圖片
    screen.blit(airforce, (airforce_x, airforce_y))
    screen.blit(ghost, (ghost_x, ghost_y))
    # 記得更新畫面(有點像是 tkinter 的 update)
    pygame.display.flip()
    

# 最後記得關閉 pygame
pygame.quit()