天天看點

harry-接小球遊戲1.02.03.04.05.0

1.0

# 1.導入工具包
import pygame
import time
# 2.初始化
pygame.init()
# 3.設定視窗
screen = pygame.display.set_mode((700,600))
# 4,導入圖檔
ball = pygame.image.load('ball.png')
ball_x = 100
ball_y = 350
# 事件
while True:
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            pygame.quit()
    screen.fill((230, 230, 230))
    if ball_y > 600:
        ball_y = 0
    # ball_x = ball_x+1 # 修改橫軸
    ball_y=ball_y+1
    # pygame.draw.circle(screen,(100, 210, 250),(300,350),70)  # 三原色 red green blue 0-255
    screen.blit(ball, (ball_x, ball_y))
    pygame.display.update()  # 4.重新整理
    # time.sleep(0.1)

pygame.quit() # 退出遊戲

           

2.0

# 1.導入工具包
import pygame
import random
import time
# 2.初始化
pygame.init()
# 3.設定視窗
screen = pygame.display.set_mode((960,540))
pygame.display.set_caption('接住三級頭') # 設定遊戲名字
# 4,導入圖檔
ball = pygame.image.load('ball.png')   # 加載圖檔
bg = pygame.image.load('bg.jpg')       # 加載圖檔
ban = pygame.image.load('ban.png')
ban_x = 100
ban_y = 460
ball_x = 100
ball_y = 350
# 事件
while True:
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEMOTION:
            ban_x, ban_y = event.pos

    screen.fill((230, 230, 230))
    if ball_y > 600:
        ball_y = 0
        ball_x = random.randint(0,700)
    # ball_x = ball_x+1 # 修改橫軸
    ball_y=ball_y+5
    # pygame.draw.circle(screen,(100, 210, 250),(300,350),70)  # 三原色 red green blue 0-255
    screen.blit(bg, (0, 0))
    screen.blit(ban, (ban_x, ban_y))
    screen.blit(ball, (ball_x, ball_y))
    pygame.display.update()  # 4.重新整理
    # time.sleep(0.1)
pygame.quit() # 退出遊戲

           

3.0

# 1.導入工具包
import pygame
import random
import time
# 2.初始化
pygame.init()
# 3.設定視窗
screen = pygame.display.set_mode((1280,686))
pygame.display.set_caption('接住三級頭') # 設定遊戲名字
# 4,導入圖檔
ball = pygame.image.load('ball.png')   # 加載圖檔
bg = pygame.image.load('bg.jpg')       # 加載圖檔
ban = pygame.image.load('ban.png')
ban_x = 100
ban_y = 606
ball_x = 100
ball_y = 350

score = 0

font = pygame.font.Font('ziti.ttf',24)
# 事件
while True:
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEMOTION:
            ban_x, _ = event.pos

    screen.fill((230, 230, 230))
    if ball_y > 600:
        ball_y = 0
        ball_x = random.randint(0,700)

    if ban_x < ball_x < ban_x + 418 and ban_y < ball_y <ban_y + 60:
        score = score+1
        ball_y = 0
        ball_x = random.randint(0, 700)


    # ball_x = ball_x+1 # 修改橫軸
    ball_y=ball_y+10
    # pygame.draw.circle(screen,(100, 210, 250),(300,350),70)  # 三原色 red green blue 0-255
    screen.blit(bg, (0, 0))
    screen.blit(ban, (ban_x, ban_y))
    screen.blit(ball, (ball_x, ball_y))


    score += 1
    imgtext = font.render('分數:%d'%score,True,(255,255,0))
    screen.blit(imgtext, (0, 0))
    pygame.display.update()  # 4.重新整理
    # time.sleep(0.1)
pygame.quit() # 退出遊戲
           

4.0

# 1.導入工具包
import pygame
import random
import time
# 2.初始化
pygame.init()
# 3.設定視窗
screen = pygame.display.set_mode((1080,652))
pygame.display.set_caption('接住三級頭') # 設定遊戲名字
# 4,導入圖檔
ball = pygame.image.load('ball.png')   # 加載圖檔
bg = pygame.image.load('bg.jpg')       # 加載圖檔
ban = pygame.image.load('ban.png')
ban_x = 100
ban_y = 592
ball_x = 100
ball_y = 0

score = 0
hp = 3
font = pygame.font.Font('ziti.ttf',24)
# 事件
while True:
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            pygame.quit()  # 退出
        elif event.type == pygame.MOUSEMOTION:
            ban_x, _ = event.pos

    screen.fill((230, 230, 230))

    # 判斷生命值是否小于0,小于就退出
    if hp <= 0:
        pygame.quit()


    if ban_x < ball_x < ban_x + 418 and ban_y < ball_y <ban_y + 60:
        score = score+1
        ball_y = 0
        ball_x = random.randint(0, 700)

    # 判斷沒接到小球
    if ball_y > 652:
        hp = hp - 1
        ball_y = 0
        ball_x = random.randint(0,700)

    # ball_x = ball_x+1 # 修改橫軸
    ball_y=ball_y + score +1
    # pygame.draw.circle(screen,(100, 210, 250),(300,350),70)  # 三原色 red green blue 0-255
    screen.blit(bg, (0, 0))
    screen.blit(ban, (ban_x, ban_y))
    screen.blit(ball, (ball_x, ball_y))

    # 在螢幕顯示分數
    imgtext = font.render('分數:%d'%score,True,(255,255,0))
    screen.blit(imgtext, (0, 0))
    # 在螢幕上顯示生命值
    hptext =font.render("生命值:%d"%hp,True,(255,255,0))
    screen.blit(hptext,(900,0))
    pygame.display.update()  # 4重新整理
    # time.sleep(0.1)
pygame.quit() # 退出遊戲
           

5.0

預想:

小球速度跟得分進行變化

根據分數改變一級頭,二級頭,三級頭

上下左右(wasd)來控制方向