九歌当下 发表于 2024-12-28 11:28:36

pygame弹球游戏


运行界面

import pygame
import random
pygame.init()
screen = pygame.display.set_mode()
pygame.display.set_caption("pingpong_game")
font = pygame.font.SysFont("微软雅黑", 40)
pygame.mixer.init()
pop = pygame.mixer.Sound("snd_bubu_meme.wav")
win = pygame.mixer.Sound("snd_player_win_flash.wav")
die = pygame.mixer.Sound("snd_rockdoor_open.wav")
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
board_x = 200
board_y = 550
board_weight = 200
board_hight = 25
posx = 50
posy = 50
xvel = 5
yvel = 5
keep_going = True
##keydown = False
##keypress_a = False
##keypress_d = False
score = 0
lifes = 5
clock = pygame.time.Clock()
#游戏主循环
while keep_going:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            keep_going = False
      if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_F1:
                posx = 50
                posy = 50
                xvel = 5
                yvel = 5
                score = 0
                lifes = 5

   #屏幕清空         
    screen.fill(BLACK)
    #挡板显示
    board_x = pygame.mouse.get_pos() - board_weight/2
    pygame.draw.rect(screen, WHITE, (board_x, board_y, board_weight, board_hight))
    #弹球显示
    posx += xvel
    posy += yvel
    if posx <= 50 or posx >= 750:
      xvel = -xvel * 1.1
    if posy <= 50:
      yvel = -yvel + 1
    if posy >= 550:
      lifes += -1
      yvel = -5
      xvel = 5
      posy = 550
      die.play()
    if posy + 25 >= board_y and posy + 25 <= board_y + board_hight and yvel > 0:
      if posx >= board_x and posx <= board_x + board_weight:
            score += 1
            yvel = -yvel
            win.play()
    pygame.draw.circle(screen, RED, (posx, posy), 50)
    #玩家数值显示
    print_text = f"Lifes: {lifes} -- Scores: {score}"
    if lifes < 1:
      xvel = yvel = 0
      print_text = f"Game Over. Your score was: {score}. Press F1 to play again"
      
    text = font.render(print_text, True, WHITE)
    text_rect = text.get_rect()
    text_rect.centerx = screen.get_rect().centerx
    text_rect.y = 10
    screen.blit(text, text_rect)
    #游戏界面更新
    pygame.display.update()
    clock.tick(60)
pygame.quit()


源码下载:

FishC 发表于 2024-12-28 11:28:46

要创建一个简单的pygame弹球游戏,以下是一些基本步骤:

1. 初始化pygame模块

import pygame
pygame.init()

2. 设置游戏窗口

screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("弹球游戏")

3. 定义游戏中使用的颜色和球的初始位置、速度

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
ball_pos =
ball_speed =
ball_radius = 10

4. 创建游戏循环以持续更新游戏状态

running = True
while running:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            running = False
   
    # 更新球的位置
    ball_pos += ball_speed
    ball_pos += ball_speed
   
    # 碰到边界反弹
    if ball_pos <= ball_radius or ball_pos >= width - ball_radius:
      ball_speed = -ball_speed
    if ball_pos <= ball_radius or ball_pos >= height - ball_radius:
      ball_speed = -ball_speed
   
    # 填充背景
    screen.fill(BLACK)
   
    # 画球
    pygame.draw.circle(screen, WHITE, ball_pos, ball_radius)
   
    # 更新显示
    pygame.display.flip()
   
    # 控制帧率
    pygame.time.Clock().tick(60)

pygame.quit()

总结: 以上代码创建了一个基本的弹球游戏。游戏将在黑色背景上显示一个白色的球,该球将从屏幕边界反弹。您可以调整`ball_speed`和`ball_radius`来改变游戏效果。希望这些信息能帮助您开始使用pygame创建有趣的弹球游戏!如有其他问题,欢迎随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

九歌当下 发表于 2024-12-28 11:34:09

不是,鱼C助手是什么帖子都回吗{:10_266:}? 而且程序比我写得还精简{:10_266:}

FishC 发表于 2024-12-28 11:34:24

九歌当下 发表于 2024-12-28 11:34
不是,鱼C助手是什么帖子都回吗{:10_266:}? 而且程序 ...

看起来您在调试您的pygame弹球游戏时遇到了一些问题。我会尽量帮助您解决这些问题。首先,让我们来处理您代码中的错误和改进部分:

import pygame
import random

pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("pingpong_game")
font = pygame.font.SysFont("微软雅黑", 40)
pygame.mixer.init()

# 声音加载
pop = pygame.mixer.Sound("snd_bubu_meme.wav")
win = pygame.mixer.Sound("snd_player_win_flash.wav")
die = pygame.mixer.Sound("snd_rockdoor_open.wav")

# 颜色定义
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)

# 挡板初始参数
board_x = 200
board_y = 550
board_weight = 200
board_height = 25

# 球初始参数
posx = 50
posy = 50
xvel = 5
yvel = 5

# 其他参数
keep_going = True
score = 0
lifes = 5
clock = pygame.time.Clock()

# 游戏主循环
while keep_going:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
            keep_going = False
      if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_F1:
                posx = 50
                posy = 50
                xvel = 5
                yvel = 5
                score = 0
                lifes = 5

    # 屏幕清空
    screen.fill(BLACK)

    # 获取鼠标位置更新挡板位置
    board_x = pygame.mouse.get_pos() - board_weight / 2

    # 挡板显示
    pygame.draw.rect(screen, WHITE, (board_x, board_y, board_weight, board_height))

    # 更新球的位置
    posx += xvel
    posy += yvel

    # 球碰到墙壁反转方向
    if posx <= 0 or posx >= 800 - 25:
      xvel = -xvel
      pop.play()

    if posy <= 0:
      yvel = -yvel
      pop.play()

    # 球击中挡板
    if posy + 25 >= board_y and yvel > 0:
      if board_x <= posx <= board_x + board_weight:
            yvel = -yvel
            score += 1
            pop.play()

    # 球掉到底部,减去生命
    if posy >= 600:
      lifes -= 1
      posx = 50
      posy = 50
      yvel = -5
      xvel = 5
      die.play()

      if lifes <= 0:
            keep_going = False

    # 显示分数和生命
    score_text = font.render(f"Score: {score}", True, WHITE)
    lifes_text = font.render(f"Lifes: {lifes}", True, WHITE)
    screen.blit(score_text, (10, 10))
    screen.blit(lifes_text, (10, 50))

    # 弹球显示
    pygame.draw.ellipse(screen, RED, (posx, posy, 25, 25))

    # 更新屏幕
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

关键修正点:
1. 更正了`if posx = 750`和其他地方的`=`为`==`。
2. 处理了挡板与球的碰撞检测逻辑。
3. 增加了生命判断,若生命值为0时游戏结束。

尝试运行上面的代码,并确保所有必要的资源(如声音文件)都存在于正确的目录中。如有更多问题,欢迎继续讨论!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。

不二如是 发表于 2024-12-28 16:06:15

有意思~~

zyx2012 发表于 2024-12-29 16:00:13

太好了
页: [1]
查看完整版本: pygame弹球游戏