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

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

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,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值