Runze-接小球游戏

import random
import pygame
from pygame import *
import time
# loading初始化
pygame.init()  # 1.初始化
screen = pygame.display.set_mode((600,500)) # 3.设置窗口大小
pygame.display.set_caption("接小球游戏")
rect_x, rect_y, rect_w, rect_h = 0, 460, 120, 40
ball_x = 100
ball_y = 100
# 设置一个变量
down = True
while True:
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type==pygame.MOUSEMOTION:
            rect_x,_ = event.pos
    screen.fill((255,255,255))
    # if down:
    #     ball_y = ball_y+1
    #     if ball_y>500:
    #         down = False
    # else:
    #     ball_y = ball_y-1
    #     if ball_y<0:
    #         down = True
    ball_y+=1
    if ball_y >500:
        ball_y=0
        ball_x = random.randint(0,600)
    pygame.draw.circle(screen, (0,255,255),(ball_x ,ball_y),15,0) #锯齿化
    pygame.draw.rect(screen, (30, 0, 0), (rect_x, rect_y, rect_w, rect_h ), 0)
    pygame.display.update() # 刷新页面


pygame.quit()  # 2.退出游戏

# 2.0
```pythom
import random
import pygame
from pygame import *
import time
# loading初始化
pygame.init()  # 1.初始化
screen = pygame.display.set_mode((1200,600)) # 3.设置窗口大小
pygame.display.set_caption("接小球游戏")
rect_x, rect_y, rect_w, rect_h = 0, 540, 220, 40
ball_x = 100
ball_y = 100
speed = 1
# 设置一个变量
down = True
font = pygame.font.Font('ziti.ttf', 24)
lives = 3
score = 0
# 图片
ball = pygame.image.load("cai.png")
bg = pygame.image.load('bg.jpg')

while True:
    for event in pygame.event.get():
        print(event)
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type==pygame.MOUSEMOTION:
            rect_x,_ = event.pos

    # screen.fill((255,255,255))
    screen.blit(bg,(-500,-300))
    ball_y+=speed
    if ball_y > 600:
        lives -= 1
        ball_y=0
        ball_x = random.randint(0,600)
        speed-= 1
    imgText = font.render("score%d"%score,True,(0,0,0))
    screen.blit(imgText,(500,0))
    if rect_x < ball_x < rect_x + 120 and ball_y>rect_y:
        ball_y = 0
        ball_x = random.randint(0, 600)
        score+=1
        speed += 1

    imgText = font.render("生命值:%d"%lives, True, (0,0,0))
    screen.blit(imgText,(0,0))
    screen.blit(ball, (ball_x,ball_y))
    # pygame.draw.circle(screen, (0,255,255),(ball_x ,ball_y),15,0) #锯齿化
    pygame.draw.rect(screen, (30, 0, 0), (rect_x, rect_y, rect_w, rect_h ), 0)
    pygame.display.update() # 刷新页面
    if lives<=0:
        pygame.quit()  # 2.退出游戏

3.0

import random
import pygame
from pygame import *
import time


def figure():
    pass


def myFont(screen, x, y, text, size=24, color=(0, 0, 0)):
    font = pygame.font.Font('ziti.ttf', size)
    imgText3 = font.render(text, True, color)
    screen.blit(imgText3, (x, y))


def game():
    screen_w, screen_h = 1080, 652
    title = "接小球游戏"
    rect_x, rect_y, rect_w, rect_h = 0, 612, 220, 40
    ball_x = 100
    ball_y = 100
    speed = 10
    lives = 3
    score = 0
    game_over = True
    ball_image = "cai.png"
    bg_image = 'bg1.jpg'

    screen = pygame.display.set_mode((screen_w, screen_h))  # 3.设置窗口大小
    pygame.display.set_caption(title)
    ball = pygame.image.load(ball_image)
    bg = pygame.image.load(bg_image)
    # font = pygame.font.Font('ziti.ttf', 24)

    # 音效
    # pygame.mixer.init()
    # hit = pygame.mixer.Sound("hit_wall.wav")
    # hit.set_volume(0.4) #  0-1

    # 背景音乐
    # pygame.mixer.music.load("bgm.mp3")
    # pygame.mixer.music.set_volume(0.3)
    # pygame.mixer.music.play(-1)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            elif event.type == pygame.MOUSEBUTTONUP:
                if game_over:
                    game_over = False
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    rect_x -= 10
                elif event.key == pygame.K_RIGHT:
                    rect_x += 10
            elif event.type == pygame.MOUSEMOTION:
                rect_x, _ = event.pos

        screen.fill((255, 255, 255))
        if game_over:
            myFont(screen, screen_w / 2, screen_h / 2, "点击屏幕开始游戏")

        else:
            #
            screen.blit(bg, (0, 0))
            ball_y += speed
            if ball_y > screen_h:
                lives -= 1
                ball_y = 0
                ball_x = random.randint(0, screen_w)
                speed -= 1
            imgText = font.render("score%d" % score, True, (0, 0, 0))
            screen.blit(imgText, (screen_w - 100, 0))
            if rect_x < ball_x + 82 < rect_x + 120 and ball_y + 50 > rect_y:
                # hit.play()
                ball_y = 0
                ball_x = random.randint(0, screen_w)
                score += 1
                speed += 1

            imgText = font.render("生命值:%d" % lives, True, (0, 0, 0))
            screen.blit(imgText, (0, 0))
            screen.blit(ball, (ball_x, ball_y))
            # pygame.draw.circle(screen, (0,255,255),(ball_x ,ball_y),15,0) #锯齿化
            pygame.draw.rect(screen, (30, 0, 0), (rect_x, rect_y, rect_w, rect_h), 0)
            pygame.display.update()  # 刷新页面
            if lives <= 0:
                pygame.quit()  # 2.退出游戏
        pygame.display.update()


def main():
    pygame.init()  # 1.初始化
    game()


if __name__ == '__main__':
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值