使用pygame库的简单python游戏的问题

2024-04-26 15:13:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python和Pygame的新手,我开始制作一个简单的游戏,有点像网球游戏,但每次球都在矩形下跳跃+-5像素并阻挡。我认为问题出在pXY和bXY上

import sys, pygame
from pygame.locals import *



pygame.init()
pygame.display.set_mode((500,500)) # ustawiwanie wielkosci
pygame.display.set_caption(("little shit")) #ustawianie nazwy

okienko = pygame.display.get_surface() # pobieranie płaszczyzny


 # obiekt
prostokat = pygame.Surface((80,20)) # tworzenie prostokąta / tulpa, szerokość / wysokość
prostokat.fill((128, 15, 220))  # zmiana koloru prostokąta / r:g:b
pXY = prostokat.get_rect() # pobranie wymiarów prostokąta
pXY.x = 225 # wartość x
pXY.y = 460 # wartość y


kolko = pygame.image.load("./ball.png")
bXY = kolko.get_rect()
bXY.x = 120 # POŁOŻENIE OBIEKTU
bXY.y = 200 # POŁOŻENIE OBIEKTU

bx,by = 5,5 # o ile sie przesuwamy
px = 3

bAB = kolko.get_rect()
bA = 25
bB = 25
kolko = pygame.transform.scale(kolko,(bA,bB))


pygame.display.flip() # wyświetlenie/odrysowanie całego okna


fps = pygame.time.Clock() # ile czasu minęło od wykonywania instrukcji


while True:


    okienko.fill((128, 128, 128))  # zmiana koloru płaszczyzny na szary

    pXY.x += px

    if pXY.x > 420 or pXY.x < 0:
        px *= -1
    okienko.blit(prostokat, pXY)

    bXY.x +=bx
    if bXY.x > 475 or bXY.x < 0:
        bx*= -1
    bXY.y +=by
    if bXY.y > 475 or bXY.y < 0:
        by*= -1
    if pXY.colliderect(bXY): # KOLIDACJA OBIEKTOW
        by=5

    okienko.blit(kolko, bXY)
    pygame.display.update() # update okienka
    fps.tick(30) # odswiezanie obrazu, 30 fps


    for zdarzenie in pygame.event.get():
        if zdarzenie.type == pygame.QUIT:
            pygame.quit()
            exit()
        if zdarzenie.type == KEYDOWN:
            if zdarzenie.key == K_LEFT:
                px=-7
            if zdarzenie.key == K_RIGHT:
                px=7



while True: # pętla do zamykania okienka
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                exit()

当球在矩形下方,跳跃+-5像素并阻挡时,球在左侧时不能离开此区域


Tags: eventgetbyifdisplaypygametapx
1条回答
网友
1楼 · 发布于 2024-04-26 15:13:45

您的碰撞检测逻辑不会给出物理上准确的结果。例如,无论球在哪里与桨碰撞,它都将以5像素/帧的速度开始向下移动。这意味着,当球从上面撞击时,它会穿过桨,但如果从下面撞击,它会“反弹”。这就是导致球的行为方式。如果桨叶和球发生碰撞,这条线就是设定速度的地方:

if pXY.colliderect(bXY): # KOLIDACJA OBIEKTOW
    by=5

一个稍微好一点的方法是,如果球与桨叶相撞,则反转球的方向。但这仍然使球在y轴上仅反转方向,无论球在桨上的哪个位置碰撞(上、下、左、右)。可以将上述代码更改为此代码以获得此效果:

if pXY.colliderect(bXY): # KOLIDACJA OBIEKTOW
    by*=-1

最后一段代码被清理一点并翻译成英语。它使用上面的第二个代码块将球从桨上弹起:

import sys

import pygame


pygame.init()

window = pygame.display.set_mode((500, 500))
pygame.display.set_caption('new caption')

paddle = pygame.Surface((80, 20))
paddle.fill((128, 15, 220))
paddle_rect = paddle.get_rect()
paddle_rect.x = 225
paddle_rect.y = 460

ball = pygame.Surface((25, 25))
ball.fill((255, 0, 0))
ball_rect = ball.get_rect()
ball_rect.x = 120
ball_rect.y = 200

ball_velocity_x = 5
ball_velocity_y = 5
paddle_velocity_x = 3

clock = pygame.time.Clock()

while True:
    # event processing code
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                paddle_velocity_x = -7
            elif event.key == pygame.K_RIGHT:
                paddle_velocity_x = 7

    # update code
    # update the position of the paddle and bounce it off the edges of the window
    paddle_rect.x += paddle_velocity_x
    if paddle_rect.x > 420 or paddle_rect.x < 0:
        paddle_velocity_x *= -1

    # update the position of the ball and bounce it off th eedges of the window
    ball_rect.x += ball_velocity_x
    ball_rect.y += ball_velocity_y
    if ball_rect.x > 475 or ball_rect.x < 0:
        ball_velocity_x *= -1
    if ball_rect.y > 475 or ball_rect.y < 0:
        ball_velocity_y *= -1

    if paddle_rect.colliderect(ball_rect):
        ball_velocity_y *= -1



    # drawing code
    window.fill((128, 128, 128))
    window.blit(paddle, paddle_rect)
    window.blit(ball, ball_rect)
    pygame.display.update()

    clock.tick(30)

相关问题 更多 >