Alex-用python和pygame进行接小球游戏

本文档详细介绍了如何使用Python的Pygame库来开发一款接小球的游戏。从基本概念到具体实现,逐步讲解每个部分,包括游戏初始化、绘制场景、小球运动逻辑以及用户交互等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.0

import pygame
from pygame.locals import *
import sys

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BG_COLOR = (0,255,255)

Ball_x = 0
Ball_y = 0
Ball_color = (255,0,0)
Ball_RADIUS = 45
Ball_speed_x = 100
Ball_speed_y = 100

BANG_color = (0,0,0)
BANG_W = 300
BANG_H = 75

BANG_X = SCREEN_WIDTH/2-BANG_W/2
BANG_y = SCREEN_HEIGHT-BANG_H
BANG_speed_x=1

score =0

lives = 300000

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption('ball games')
gamefont = pygame.font.Font("ziti.ttf",30)

while True:
    if lives == 0:
        break

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_SPACE]:
        while True:
            events = pygame.event.get()
            for e in events:
                if e.type == QUIT:
                    pygame.quit()
                    sys.exit()
            imgMenu = gamefont.render("回到游戏模式请按 Esc" ,True,(255,255,255))
            screen.blit(imgMenu,(SCREEN_WIDTH/2,SCREEN_HEIGHT/2))
            pygame.display.update()

            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_SPACE]:
                break

    events = pygame.event.get()
    for e in events:
        if e.type == QUIT:
            pygame.quit()
            sys.exit()

    screen.fill(BG_COLOR)
    Ball_x += Ball_speed_x
    Ball_y += Ball_speed_y
    if Ball_x >SCREEN_WIDTH:
        Ball_speed_x = -1
    if Ball_x < 0:
        Ball_speed_x = 1
    if Ball_y > SCREEN_HEIGHT:
        Ball_x = 0
        Ball_y = 0
        lives = lives - 1

    if Ball_y < 0:
        Ball_speed_y = 1

    pygame.draw.circle(screen,Ball_color,(Ball_x,Ball_y),Ball_RADIUS)

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_LEFT]:
        BANG_X -=BANG_speed_x
    if key_pressed[K_RIGHT]:
        BANG_X += BANG_speed_x
    if BANG_X >SCREEN_WIDTH-BANG_W:
        BANG_X = SCREEN_WIDTH - BANG_W
    if BANG_X < 0:
        BANG_X = 0

    if BANG_X < Ball_x and BANG_X<(BANG_X+BANG_W) and (BANG_y - Ball_y) <5:
        BANG_speed_y = -1
        score += 1

    pygame.draw.rect(screen,BANG_color,(BANG_X,BANG_y,BANG_W,BANG_H))
    imlives = gamefont.render('命数{}'.format(lives),True,(255,0,0))

    imgScore = gamefont.render('得分{}'.format(score),True,(255,0,0))
    screen.blit(imgScore,(SCREEN_WIDTH-imgScore.get_width(),0))
    screen.blit(imlives, (0, 0))
    pygame.display.update()






2.0

import pygame
from pygame.locals import *
import sys
import random

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BG_COLOR = (0,255,255)

Ball_x = random.randint(1,800)
Ball_y = 0
Ball_color = (255,0,0)
Ball_RADIUS = 45
Ball_speed_x = 10   # 横向速度
Ball_speed_y = 1   # 纵向速度

BANG_color = (0,0,0)
BANG_W = 300
BANG_H = 75

BANG_X = SCREEN_WIDTH/2-BANG_W/2
BANG_y = SCREEN_HEIGHT-BANG_H
BANG_speed_x=1

score =0

lives = 3

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption('ball games')
gamefont = pygame.font.Font("ziti.ttf",30)
BALL = pygame.image.load("cai.png")  # 加载图片
while True:
    if lives == 0:
        break

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_SPACE]:
        while True:
            events = pygame.event.get()
            for e in events:
                if e.type == QUIT:
                    pygame.quit()
                    sys.exit()
            imgMenu = gamefont.render("回到游戏模式请按 Esc" ,True,(255,255,255))
            screen.blit(imgMenu,(SCREEN_WIDTH/2,SCREEN_HEIGHT/2))
            pygame.display.update()

            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_SPACE]:
                break

    events = pygame.event.get()
    for e in events:
        if e.type == QUIT:
            pygame.quit()
            sys.exit()
        elif e.type == pygame.MOUSEMOTION:
            BANG_X,_ = e.pos

    screen.fill(BG_COLOR)
    # Ball_x += Ball_speed_x
    Ball_y += Ball_speed_y
    if Ball_x >SCREEN_WIDTH:
        Ball_speed_x = -1
    if Ball_x < 0:
        Ball_speed_x = 1
    if Ball_y > SCREEN_HEIGHT:
        Ball_x = random.randint(1,800)      # 随机下落
        Ball_y = 0
        lives = lives - 1

    if Ball_y < 0:
        Ball_speed_y = 1

    # pygame.draw.circle(screen,Ball_color,(Ball_x,Ball_y),Ball_RADIUS)
    screen.blit(BALL, (Ball_x, Ball_y)) # 放置图片

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_LEFT]:
        BANG_X -=BANG_speed_x
    if key_pressed[K_RIGHT]:
        BANG_X += BANG_speed_x
    if BANG_X >SCREEN_WIDTH-BANG_W:
        BANG_X = SCREEN_WIDTH - BANG_W
    if BANG_X < 0:
        BANG_X = 0

    # 判断得分
    if BANG_X < Ball_x and Ball_x<(BANG_X+BANG_W) and BANG_y < Ball_y:
        Ball_x = random.randint(0,800)
        Ball_y = 0
        score += 1

    pygame.draw.rect(screen,BANG_color,(BANG_X,BANG_y,BANG_W,BANG_H))
    imlives = gamefont.render('命数{}'.format(lives),True,(255,0,0))

    imgScore = gamefont.render('得分{}'.format(score),True,(255,0,0))
    screen.blit(imgScore,(SCREEN_WIDTH-imgScore.get_width(),0))
    screen.blit(imlives, (0, 0))
    pygame.display.update()


3.0

import pygame
from pygame.locals import *
import sys
import random

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BG_COLOR = (0,255,255)

Ball_x = random.randint(1,800)
Ball_y = 0
Ball_color = (255,0,0)
Ball_RADIUS = 45
Ball_speed_x = 10   # 横向速度
Ball_speed_y = 1   # 纵向速度

BANG_color = (0,0,0)
BANG_W = 300
BANG_H = 75

BANG_X = SCREEN_WIDTH/2-BANG_W/2
BANG_y = SCREEN_HEIGHT-BANG_H
BANG_speed_x=1

score =0

lives = 3

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption('ball games')
gamefont = pygame.font.Font("ziti.ttf",30)
BALL = pygame.image.load("cai.png")  # 加载图片
BALL1 = pygame.image.load("ball1.png")
BALL2 = pygame.image.load("ball2.png")
BALL3 = pygame.image.load("ball3.png")


while True:
    if lives == 0:
        break

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_SPACE]:
        while True:
            events = pygame.event.get()
            for e in events:
                if e.type == QUIT:
                    pygame.quit()
                    sys.exit()
            imgMenu = gamefont.render("回到游戏模式请按 Esc" ,True,(255,255,255))
            screen.blit(imgMenu,(SCREEN_WIDTH/2,SCREEN_HEIGHT/2))
            pygame.display.update()

            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_SPACE]:
                break

    events = pygame.event.get()
    for e in events:
        if e.type == QUIT:
            pygame.quit()
            sys.exit()
        elif e.type == pygame.MOUSEMOTION:
            BANG_X,_ = e.pos

    screen.fill(BG_COLOR)
    # Ball_x += Ball_speed_x
    Ball_y += Ball_speed_y
    if Ball_x >SCREEN_WIDTH:
        Ball_speed_x = -1
    if Ball_x < 0:
        Ball_speed_x = 1
    if Ball_y > SCREEN_HEIGHT:
        Ball_x = random.randint(1,800)      # 随机下落
        Ball_y = 0
        lives = lives - 1

    if Ball_y < 0:
        Ball_speed_y = 1

    # pygame.draw.circle(screen,Ball_color,(Ball_x,Ball_y),Ball_RADIUS)
    if score < 5:
        screen.blit(BALL1, (Ball_x, Ball_y)) # 放置图片
    elif 10 > score >= 5  :
        screen.blit(BALL2, (Ball_x, Ball_y))
    else:
        screen.blit(BALL, (Ball_x, Ball_y))  # 放置图片

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_LEFT]:
        BANG_X -=BANG_speed_x
    if key_pressed[K_RIGHT]:
        BANG_X += BANG_speed_x
    if BANG_X >SCREEN_WIDTH-BANG_W:
        BANG_X = SCREEN_WIDTH - BANG_W
    if BANG_X < 0:
        BANG_X = 0

    # 判断得分
    if BANG_X < Ball_x and Ball_x<(BANG_X+BANG_W) and BANG_y < Ball_y:
        Ball_x = random.randint(0,800)
        Ball_y = 0
        score += 1

    pygame.draw.rect(screen,BANG_color,(BANG_X,BANG_y,BANG_W,BANG_H))
    imlives = gamefont.render('命数{}'.format(lives),True,(255,0,0))

    imgScore = gamefont.render('得分{}'.format(score),True,(255,0,0))
    screen.blit(imgScore,(SCREEN_WIDTH-imgScore.get_width(),0))
    screen.blit(imlives, (0, 0))
    pygame.display.update()

4.0

import pygame
from pygame.locals import *
import sys
import random

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
BG_COLOR = (0,255,255)

Ball_x = random.randint(1,800)
Ball_y = 0
Ball_color = (255,0,0)
Ball_RADIUS = 45
Ball_speed_x = 10   # 横向速度
Ball_speed_y = 1   # 纵向速度

BANG_color = (0,0,0)
BANG_W = 300
BANG_H = 75

BANG_X = SCREEN_WIDTH/2-BANG_W/2
BANG_y = SCREEN_HEIGHT-BANG_H
BANG_speed_x=1

score =0
lives = 3

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH,SCREEN_HEIGHT))
pygame.display.set_caption('ball games')
gamefont = pygame.font.Font("ziti.ttf",30)
BALL = pygame.image.load("cai.png")  # 加载图片
BALL1 = pygame.image.load("ball1.png")
BALL2 = pygame.image.load("ball2.png")
BALL3 = pygame.image.load("ball3.png")
BG = pygame.image.load("bg1.jpg")

# 1.音乐的初始化
# pygame.mixer.init()
# # 2.加载音效
# HIT = pygame.mixer.Sound("hit_wall.wav")
# # 3.音量0-1
# HIT.set_volume(0.4)
# 加载音乐
# pygame.mixer.music.load("hit_wall.wav")
# pygame.mixer.music.set_volume(0.1)
# pygame.mixer.music.play(-1)

while True:
    if lives == 0:
        break

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_SPACE]:
        while True:
            events = pygame.event.get()
            for e in events:
                if e.type == QUIT:
                    pygame.quit()
                    sys.exit()
            imgMenu = gamefont.render("回到游戏模式请按 Esc" ,True,(255,255,255))
            screen.blit(imgMenu,(SCREEN_WIDTH/2,SCREEN_HEIGHT/2))
            pygame.display.update()

            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_SPACE]:
                break

    events = pygame.event.get()
    for e in events:
        if e.type == QUIT:
            pygame.quit()
            sys.exit()
        elif e.type == pygame.MOUSEMOTION:
            BANG_X,_ = e.pos

    # screen.fill(BG_COLOR)
    screen.blit(BG, (0, 0))
    # Ball_x += Ball_speed_x
    Ball_y += Ball_speed_y
    if Ball_x >SCREEN_WIDTH:
        Ball_speed_x = -1
    if Ball_x < 0:
        Ball_speed_x = 1
    if Ball_y > SCREEN_HEIGHT:
        Ball_x = random.randint(1,800)      # 随机下落
        Ball_y = 0
        lives = lives - 1

    if Ball_y < 0:
        Ball_speed_y = 1

    # pygame.draw.circle(screen,Ball_color,(Ball_x,Ball_y),Ball_RADIUS)
    if score < 5:
        screen.blit(BALL1, (Ball_x, Ball_y)) # 放置图片
    elif 10 > score >= 5  :
        screen.blit(BALL2, (Ball_x, Ball_y))
    else:
        screen.blit(BALL, (Ball_x, Ball_y))  # 放置图片

    key_pressed = pygame.key.get_pressed()
    if key_pressed[K_LEFT]:
        BANG_X -=BANG_speed_x
    if key_pressed[K_RIGHT]:
        BANG_X += BANG_speed_x
    if BANG_X >SCREEN_WIDTH-BANG_W:
        BANG_X = SCREEN_WIDTH - BANG_W
    if BANG_X < 0:
        BANG_X = 0

    # 接到小球判断得分
    if BANG_X < Ball_x and Ball_x<(BANG_X+BANG_W) and BANG_y < Ball_y:
        # HIT.play()
        Ball_x = random.randint(0,800)
        Ball_y = 0
        score += 1

    pygame.draw.rect(screen,BANG_color,(BANG_X,BANG_y,BANG_W,BANG_H))
    imlives = gamefont.render('命数{}'.format(lives),True,(255,0,0))

    imgScore = gamefont.render('得分{}'.format(score),True,(255,0,0))
    screen.blit(imgScore,(SCREEN_WIDTH-imgScore.get_width(),0))
    screen.blit(imlives, (0, 0))
    pygame.display.update()





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值