python接球游戏

mport pygame as pg
import sys
import random
import time
pg.init()

game_window=pg.display.set_mode((600,500))
pg.display.set_caption("接球")
window_color = (0,0,255)
ball_color=(255,165,0)
rect_color=(255,0,0)

score=0
font=pg.font.Font(None,70)
ball_x=random.randint(20,580)
ball_y=20
move_x=1
move_y=1
point=1
count=0
while True:
    game_window.fill(window_color)
    for event in pg.event.get():
        if event.type==pg.QUIT:
            sys.exit()
    mouse_x,mouse_y=pg.mouse.get_pos()
    pg.draw.circle(game_window,ball_color,(ball_x,ball_y),20)
    pg.draw.rect(game_window,rect_color,(mouse_x,490,100,200))
    my_text=font.render(str(score),False,(255,255,255))
    game_window.blit(my_text,(500,30))


    ball_x+=move_x
    ball_y+=move_y

    if ball_x<=20 or ball_x>=580:
        move_x=-move_x

    if ball_y <= 20:
        move_y =-move_y
    elif mouse_x-20<ball_x<mouse_x+120 and ball_y>=470:
        move_y=-move_y
        score+=point
        count+=1
        if count==3:
            count=0
            point+=point
            if move_x>0:
                move_x+=1
            else:
                move_x-=1
            move_y-=1
    elif ball_y>=480 and (ball_x<=mouse_x-20 or ball_x>=mouse_x+120):
        break

    pg.display.update()
    time.sleep(0.005)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python接球游戏是一款简单的游戏,玩家需要控制一个接球器来接住从上方掉落的球,每接到一个球得一分,如果接到炸弹则扣一分。以下是一个简单的Python代码示例: ```python import pygame import random # 初始化游戏 pygame.init() # 设置游戏窗口大小 screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("接球游戏") # 定义颜色 BLACK = (0, 0, 0) WHITE = (255, 255, 255) # 加载图片 ball_image = pygame.image.load("ball.png") bomb_image = pygame.image.load("bomb.png") player_image = pygame.image.load("player.png") # 获取图片大小 ball_width = ball_image.get_width() ball_height = ball_image.get_height() bomb_width = bomb_image.get_width() bomb_height = bomb_image.get_height() player_width = player_image.get_width() player_height = player_image.get_height() # 设置玩家初始位置 player_x = (screen_width - player_width) // 2 player_y = screen_height - player_height # 设置球和炸弹的初始位置和速度 ball_x = random.randint(0, screen_width - ball_width) ball_y = 0 ball_speed = 5 bomb_x = random.randint(0, screen_width - bomb_width) bomb_y = 0 bomb_speed = 5 # 设置分数 score = 0 # 游戏主循环 running = True clock = pygame.time.Clock() while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False # 移动玩家 keys = pygame.key.get_pressed() if keys[pygame.K_LEFT]: player_x -= 5 if keys[pygame.K_RIGHT]: player_x += 5 # 更新球和炸弹的位置 ball_y += ball_speed bomb_y += bomb_speed # 判断球是否接到玩家 if ball_y + ball_height >= player_y and ball_x + ball_width >= player_x and ball_x <= player_x + player_width: score += 1 ball_x = random.randint(0, screen_width - ball_width) ball_y = 0 # 判断炸弹是否接到玩家 if bomb_y + bomb_height >= player_y and bomb_x + bomb_width >= player_x and bomb_x <= player_x + player_width: score -= 1 bomb_x = random.randint(0, screen_width - bomb_width) bomb_y = 0 # 绘制游戏界面 screen.fill(BLACK) screen.blit(ball_image, (ball_x, ball_y)) screen.blit(bomb_image, (bomb_x, bomb_y)) screen.blit(player_image, (player_x, player_y)) pygame.display.flip() # 控制游戏帧率 clock.tick(60) # 游戏结束后退出 pygame.quit() ``` 请注意,上述代码中的图片文件需要提前准备好,并与代码文件放在同一目录下。你可以根据自己的需求自定义图片和调整游戏的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值