aden - 接小球遊戲4.0

import pygame
import time
import random
# 遊戲初始化
pygame.init()
# 設置窗口大小  640長度  窗口寬度500
screen = pygame.display.set_mode((640, 500))
# 設置窗口標題
pygame.display.set_caption("坦福版接球遊戲")

# 文字1. 設置文字字體和大小
font = pygame.font.SysFont("SimHei", 35)


a = 200
b = 200
board_x = 400
board_y = 320
speedy = 1
speedx = 1
score = 0
game_over = True
r, g, bl = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)

while 1 < 100:
    # 事件:我們對電腦的物理操作,按鍵,鼠標(按鍵,移動)
    for event in pygame.event.get():
        # print(event)
        if event.type == pygame.QUIT:
            pygame.quit() # 退出遊戲
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                board_x = board_x - 10
            if event.key == pygame.K_d:
                board_x = board_x + 10
        elif event.type == pygame.MOUSEMOTION:
            board_x,board_y = event.pos
        elif event.type == pygame.MOUSEBUTTONUP:
            if game_over:
                game_over = False
                score = 0
                a,b = 0,0
                t1 = time.time()

    screen.fill((0, 0, 0))

    if game_over:
        # 2.設置文字的內容和顏色
        font3 = font.render("點擊鼠標開始遊戲", True, (0, 255, 0))
        # 3.把文字放在窗口上
        screen.blit(font3, (100, 100))
    else:
        b = b + speedy
        a = a + speedx
        if a > 640:
            speedx = -1
        elif a< 0:
            speedx = 1



        if b > 640:
            b = 0

        if b < 0:
            r,g,bl =random.randint(0,255),random.randint(0,255),random.randint(0,255)
            speedy = 1

        if board_x< a<board_x + 200 and board_y < b < board_y+10:
            score = score +1
            board_y = b - 1
            speedy = -1



        # 畫小球:屏幕,rgb三基色,座標,半徑
        pygame.draw.circle(screen,(r,g,bl),(a,b), 23)
        # time.sleep(1)
        #  畫一個長方形 pygame.draw.rect(屏幕,(r,g,b),(經度,維度,板長,板寬))
        pygame.draw.rect(screen,(105,105,150),(board_x,board_y,200,10))

        # 2.設置文字的內容和顏色
        font2 = font.render("分數:%d"%score, True, (0,255,0))
        # 3.把文字放在窗口上
        screen.blit(font2, (100,100))

        t2 = time.time()
        djs = 30 - (t2-t1)
        # 2.設置文字的內容和顏色
        font4 = font.render("時間:%d"%djs, True, (0,255,0))
        # 3.把文字放在窗口上
        screen.blit(font4, (100,200))
        if djs < 0:
            game_over = True


    # 刷新
    pygame.display.update()




``
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章