Skip to main content
 首页 » 编程设计

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

2024年11月01日852php

我是 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 像素并阻挡时,球在左侧时不能离开此区域

请您参考如下方法:

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

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

稍微好一点的方法是在球与桨碰撞时反转球的方向。但这仍然使球只能在 y 轴上反向,无论球在 Racket 的哪个位置(顶部、底部、左侧、右侧)发生碰撞。上面的代码可以改成这段代码来达到这个效果:

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

这最后一段代码经过一些清理并翻译成英文。它使用上面的第二个代码块将球从 Racket 上弹开:

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)