Deqin - 接小球游戏

1.0

import pygame
# pygame专门用来做游戏的python工具包
# 等待画面 初始化 loading
# 1。初始化
pygame.init()
# 2。界面化      display
screen = pygame.display.set_mode((600, 500))   # 设置窗口大小
ball_x = 300
ball_y = 250
while True:
    screen.fill((0, 0, 0))
    ball_y = ball_y + 1
    if ball_y>500:
        ball_y=250
    pygame.draw.circle(screen, (20, 175, 222), (ball_x,ball_y), 50  )   # 三原色 颜料ryb   三基色 光 rgb 0-255
    pygame.display.update()

2.0


import pygame

pygame.init()
screen = pygame.display.set_mode((666, 500))
pygame.display.set_caption("蔡旭坤接小球")  # 设置标题
ball_x = 333
ball_y = 166
down = 1 # 1 == "向下"
up = 2  # == "向上"
left = 3 # == "向左"
right = 4 # == "向右"
rect_x,rect_y,rect_w,rect_h = 300,460,120,40
while True:
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            pygame.quit()

    screen.fill((100, 200, 0))
    pygame.draw.circle(screen, (20, 40, 60), (ball_x, ball_y), 50)
    pygame.draw.rect(screen, (30,90,60), (rect_x,rect_y,rect_w,rect_h)) # 画挡板

    direction = input("请输入数字1234")
    direction = int(direction)

    if down == direction:
        ball_y += 10
        if ball_y > 333:
            ball_y = 166
    elif up == direction:
        pygame.draw.circle(screen, (20, 40, 60), (ball_x, ball_y), 50)
        ball_y = ball_y - 10
        if ball_y < 0:
            ball_y = 166
    elif left == direction:
        pygame.draw.circle(screen, (20, 40, 60), (ball_x, ball_y), 50)
        ball_x = ball_x - 10
        if ball_x < 0:
            ball_x = 166
    elif right == direction:
        pygame.draw.circle(screen, (20, 40, 60), (ball_x, ball_y), 50)
        ball_x = ball_x + 10
        if ball_x > 666:
            ball_x = 166
    else:
        print("系统未升级")


    pygame.draw.circle(screen, (255, 255, 255), (ball_x, ball_y), 50)
    pygame.display.update()


pygame.quit()   # 退出游戏


3.0

import random
import pygame
pygame.init()
screen = pygame.display.set_mode((666, 500))
pygame.display.set_caption("蔡旭坤接小球")  # 设置标题
ball_x = 333
ball_y = 166
rect_x,rect_y,rect_w,rect_h = 300,460,120,40
score = 0    # 分数
while True:
    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_LEFT:
                rect_x = rect_x - 50
            elif event.key == pygame.K_RIGHT:
                rect_x = rect_x + 50
        elif event.type == pygame.MOUSEMOTION:
            rect_x,_ = event.pos

    screen.fill((100, 200, 0))
    ball_y = ball_y +1
    if ball_y > 500:
        ball_y = 0
        ball_x = random.randint(1,666)

    if rect_x + 120 > 666:
        rect_x = 666-120
    elif rect_x < 0:
        rect_x = 0

    # 小球的x坐标是不是在板的两个x坐标之间
    # 小球的y坐标是不是在板的两个y坐标之间
    if rect_x < ball_x < rect_x+rect_w and rect_y< ball_y<rect_y+rect_h:
        score+=1
        ball_y = 0
        ball_x = random.randint(1, 666)
        print("这是第%d次接住"%score)
    pygame.draw.rect(screen, (30,90,60), (rect_x,rect_y,rect_w,rect_h)) # 画挡板
    pygame.draw.circle(screen, (255, 255, 255), (ball_x, ball_y), 25)
    pygame.display.update()

pygame.quit()   # 退出游戏

4.0

import random
import pygame
import time
pygame.init()
screen = pygame.display.set_mode((666, 500))
pygame.display.set_caption("蔡旭坤接小球")  # 设置标题
ball_x = 333
ball_y = 166
rect_x,rect_y,rect_w,rect_h = 300,460,120,40
score = 0    # 分数
font2 = pygame.font.Font("ziti.ttf", 24)
start_time = time.time()
game_time = 0
while True:
    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_LEFT:
                rect_x = rect_x - 50
            elif event.key == pygame.K_RIGHT:
                rect_x = rect_x + 50
        elif event.type == pygame.MOUSEMOTION:
            rect_x,_ = event.pos

    screen.fill((100, 200, 0))
    game_time = 60 - int(time.time() - start_time)
    if game_time >= 0:
        ball_y = ball_y +1
        if ball_y > 500:
            ball_y = 0
            ball_x = random.randint(1,666)

        if rect_x + 120 > 666:
            rect_x = 666-120
        elif rect_x < 0:
            rect_x = 0

        # 小球的x坐标是不是在板的两个x坐标之间
        # 小球的y坐标是不是在板的两个y坐标之间
        if rect_x < ball_x < rect_x+rect_w and rect_y< ball_y<rect_y+rect_h:
            score+=1
            ball_y = 0
            ball_x = random.randint(1, 666)
            print("这是第%d次接住"%score)
        pygame.draw.rect(screen, (30,90,60), (rect_x,rect_y,rect_w,rect_h)) # 画挡板
        pygame.draw.circle(screen, (255, 255, 255), (ball_x, ball_y), 25)

        # 设置文字
        imgText = font2.render("分数:%d"%score, True, (0, 0, 0))
        # 放置文字在屏幕上
        screen.blit(imgText, (0,0))

        # 游戏时间

        # 设置文字
        r = int(time.time() - start_time) * 4
        SDFSF = font2.render("时间:%d"%game_time,True ,(r,0,0))
        # 放置文字在屏幕上
        screen.blit(SDFSF,(0,20))
        pygame.display.update()
    else:
        print('---------------------%d'%game_time)
        screen.fill((100, 200, 0))
        end_x = font2.render("时间到,得分%d" % score, True, (0, 0, 0))
        screen.blit(end_x,( 333,250))
        pygame.display.update()
pygame.quit()  # 退出游戏

5.0

import random
import pygame
import time
pygame.init()
screen_w,screen_h = 1200,800
screen = pygame.display.set_mode((screen_w,screen_h))
pygame.display.set_caption("蔡旭坤接小球")  # 设置标题
ball_x = 333
ball_y = 166
rect_x,rect_y,rect_w,rect_h = 300,screen_h-40,250,40
score = 0    # 分数
font2 = pygame.font.Font("ziti.ttf", 24)
start_time = time.time()
game_time = 0
ball1 = pygame.image.load("ball1.png")
ball2 = pygame.image.load("ball2.png")
ball3 = pygame.image.load("ball3.png")
cai = pygame.image.load("cai.png")
bg = pygame.image.load("bg2.jpg")
speed = 5
while True:
    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_LEFT:
                rect_x = rect_x - 50
            elif event.key == pygame.K_RIGHT:
                rect_x = rect_x + 50
        elif event.type == pygame.MOUSEMOTION:
            rect_x,_ = event.pos

    screen.fill((100, 200, 0))
    screen.blit(bg, (0, 0))
    game_time = 60 - int(time.time() - start_time)
    if game_time >= 0:
        ball_y = ball_y + speed
        if ball_y > screen_h:
            ball_y = 0
            ball_x = random.randint(1,666)
            speed = 1

        if rect_x + 120 > 666:
            rect_x = 666-120
        elif rect_x < 0:
            rect_x = 0

        # 小球的x坐标是不是在板的两个x坐标之间
        # 小球的y坐标是不是在板的两个y坐标之间
        if rect_x < ball_x+70 < rect_x+rect_w and rect_y< ball_y + 70 <rect_y+rect_h:
            score+=1
            ball_y = 0
            ball_x = random.randint(1, 666)
            rect_w -= 1
            speed += 1
            print("这是第%d次接住"%score)
        pygame.draw.rect(screen, (30,90,60), (rect_x,rect_y,rect_w,rect_h)) # 画挡板
        # pygame.draw.circle(screen, (255, 255, 255), (ball_x, ball_y), 25)
        # 将图片放在屏幕
        if score>60:
            screen.blit(cai, (ball_x, ball_y))
        elif score>40:
            screen.blit(ball3, (ball_x, ball_y))
        elif score>20:
            screen.blit(ball2, (ball_x, ball_y))
        else:
            screen.blit(ball1, (ball_x, ball_y))
        # 设置文字
        imgText = font2.render("分数:%d"%score, True, (0, 0, 0))
        # 放置文字在屏幕上
        screen.blit(imgText, (0,0))

        # 游戏时间

        # 设置文字
        r = int(time.time() - start_time) * 4
        SDFSF = font2.render("时间:%d"%game_time,True ,(r,0,0))
        # 放置文字在屏幕上
        screen.blit(SDFSF,(0,20))
        pygame.display.update()
    else:
        print('---------------------%d'%game_time)
        screen.fill((100, 200, 0))
        end_x = font2.render("时间到,得分%d" % score, True, (0, 0, 0))
        screen.blit(end_x,( 333,250))
        pygame.display.update()
pygame.quit()  # 退出游戏

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值