为啥不能运行???

smt123 2024-08-09 21:18:38 2024-08-09 21:20:40 33

import pygame as pg

from random import randint

import sys

import time

pg.init()

game_window=pg.display.set_mode((600,500))

pg.display.set_caption("接球游戏")

score=0

font=pg.font.Font(None,60)

window_color=(0,0,0)

ball_color=(0,255,0)

rect_color=(255,0,0)

move_x=1

move_y=1

ball_x=randint(20,580)

ball_y=randint(20,480)

points=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,10))

    ball_x+=move_x

    ball_y+=move_y

    my_score=font.render(str(score),False,(255,255,0))

    game_window.blit(my_score,(500,30))

    ball_x+=move_x

    ball_y+=move_y

    if ball_y<=20 or ball_x>=580:

        move_y=-move_y

        if ball_y=-move_y
        elif (ball_x>mouse_x-20 and ball_x<mouse_x+120) and ball_y>=470:

           move_y=-move_y

           score+=points

           count+=1

           if count==5:

               count==0

        points+=points

        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>=+120):

                ball_y=490

                break

            pg.display.update()

            time.sleep(0.005)

//弹板游戏

{{ vote && vote.total.up }}

共 3 条回复

Joshua Microsoft

你有一些小错误和改进点在你的代码中。这里是修正后的版本:

import pygame as pg
from random import randint
import sys
import time

pg.init()

game_window = pg.display.set_mode((600, 500))
pg.display.set_caption("接球游戏")

score = 0
font = pg.font.Font(None, 60)
window_color = (0, 0, 0)
ball_color = (0, 255, 0)
rect_color = (255, 0, 0)

move_x = 1
move_y = 1
ball_x = randint(20, 580)
ball_y = randint(20, 480)
points = 1
count = 0

while True:
    game_window.fill(window_color)

    for event in pg.event.get():
        if event.type == pg.QUIT:
            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 - 50, 490, 100, 10))

    ball_x += move_x
    ball_y += move_y

    my_score = font.render(str(score), False, (255, 255, 0))
    game_window.blit(my_score, (500, 30))

    if ball_y <= 20 or ball_x >= 580 or ball_x <= 20:
        move_y = -move_y

    if ball_x > mouse_x - 50 and ball_x < mouse_x + 50 and ball_y >= 470:
        move_y = -move_y
        score += points
        count += 1

        if count == 5:
            count = 0

        points += 1

        if move_x > 0:
            move_x += 1
        else:
            move_x -= 1

        if move_y > 0:
            move_y += 1
        else:
            move_y -= 1

    if ball_y > 490 and (ball_x < mouse_x - 50 or ball_x > mouse_x + 50):
        ball_x = randint(20, 580)
        ball_y = randint(20, 480)
        move_x = 1
        move_y = 1
        score = 0

    pg.display.update()
    time.sleep(0.005)

主要更改了:

修正了缩进错误。 改正了错误的if和elif逻辑。 调整了球和挡板的碰撞逻辑。 纠正了挡板的位置计算方式。

smt123

@root,安装了的,还是不能运行,说是这里错了game_window.fill(window_color)

root 站长

pygame 这个库需要安装 pip install pygame 运行这个命令