簡體   English   中英

簡單的 python/pygame pong 游戲不會使用 y 軸

[英]Simple python/pygame pong game won't use y axis

我正在做一個小項目來了解 Pygame,我正在使用以下教程來介紹自己:

https://www.101computing.net/pong-tutorial-using-pygame-getting-started/

在遵循該教程並進行了我自己的調整(主要是風格,沒有任何功能)之后,每次我運行程序時,球只會在同一個 y 坐標上來回移動,並且不會上下 go。 似乎其他一切都有效,但球垂直運動的變化。

如果需要,我也可以提供我的代碼,但它看起來類似於上面的教程。

編輯:這是代碼

import pygame
#need random integers for velocity changes
import random
Black = (0,0,0)

#create ball object for the game, wil be a sprite object
class Ball(pygame.sprite.Sprite):

    #define the package function and also call the pygame sprite constructor using super()
    def __init__(self, color, width, height):
        super().__init__()
        
        
        self.image = pygame.Surface([width, height])
        self.image.fill(Black)
        self.image.set_colorkey(Black)
        
        #draw the ball
        pygame.draw.rect(self.image, color, [0, 0, width, height])
        
        #set velocity
        self.velocity = [random.randint(4,8), random.randint(-8,8)]
        
        #get rectangle object from image package
        self.rect = self.image.get_rect()
        
    
    def update(self):
        self.rect.x += self.velocity[0]
        self.rect.y += self.velocity[1]
    
    #reverse velocity path of ball hits paddle
    def bounce(self):
        self.velocity[0] = -self.velocity[0]
        self.velocity[1] = random.randint(-8,8)

-----主文件-----

import pygame
#import paddle sprites
from paddle import Paddle
#import ball
from ball import Ball
import time


pygame.init()


#set local colors: Black for background, white for text, blue and red for teams
Black = (0,0,0)
White = (255,255,255)
Red = (255,0,0)
Blue = (0,0,255)

#create paddles using paddle class and add them to a list of sprites
paddleLeft = Paddle(Red, 10, 100)
paddleLeft.rect.x = 20
paddleLeft.rect.y = 200

paddleRight = Paddle(Blue, 10, 100)
paddleRight.rect.x = 870
paddleRight.rect.y = 200

ball = Ball(White, 10, 10)
ball.rect.x = 445
ball.rect.y = 195

allSprites = pygame.sprite.Group()
allSprites.add(paddleLeft)
allSprites.add(paddleRight)
allSprites.add(ball)

#set game window
size = (900,500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Multiplayer Pong")

#to the functionality, we will have a while loop that will listen to user inputs, adding logic to the game (score, boundaries, etc.), and refreshing the program
#global "running" funtion that will control the while loop, simple bool
running = True

#need a clock for refreshing the screen (included in pygame package)
clock = pygame.time.Clock()

#scores for each side
scoreLeft = 0
scoreRight = 0

#start loop
while running:
    
    #--listen for inputs
    for event in pygame.event.get():
        if event.type == pygame.QUIT: #if quit button is pressed, leave
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_x:
                running = False
        
        #keyboard inputs
    key = pygame.key.get_pressed()
    if key[pygame.K_w]:
        paddleLeft.mUp(5)
    if key[pygame.K_s]:
        paddleLeft.mDown(5)
    if key[pygame.K_UP]:
        paddleRight.mUp(5)
    if key[pygame.K_DOWN]:
        paddleRight.mDown(5)

    #--logic
    allSprites.update()
    
    #--drawing here (paddles, screen, scores, boundaries, etc
    screen.fill(Black)
    pygame.draw.line(screen, White, [448, 0], [448, 500], 4)
    
    allSprites.draw(screen)
    
    #check for wall bounce
     #algorithms for bounce look like
        #Hits right or left wall? reverse X-bound velocity
        #Hits top or bottom wall? reverse Y-bound velocity
    if ball.rect.x >= 890:
        scoreLeft += 1
        ball.rect.x = 445
        ball.rect.y = 195
        time.sleep(2)
        ball.velocity[0] = -ball.velocity[0]
    if ball.rect.x <= 0:
        scoreRight += 1
        ball.rect.x = 445
        ball.rect.y = 195
        time.sleep(2)
        ball.velocity[0] = -ball.velocity[0]
        
        #reverse ball angle
    if ball.rect.y >= 490:
        ball.velocity[1] = -ball.velocity[1]
    if ball.rect.y >= 0:
        ball.velocity[1] = -ball.velocity[1]
    
    #check for paddle hit
    if pygame.sprite.collide_mask(ball, paddleLeft) or pygame.sprite.collide_mask(ball, paddleRight):
        ball.bounce()
        
    #display scores
    font = pygame.font.SysFont("impact.ttf", 50)
    text = font.render(str(scoreLeft), 1, Red)
    screen.blit(text, (420,10))
    text = font.render(str(scoreRight), 1, Blue)
    screen.blit(text, (460,10))
    
    #--update screen with drawings
    pygame.display.flip()
    
    #--60 fps limit
    clock.tick(60)


#stop program once main loop is exited
pygame.quit()

經過一些調試后,我意識到 y 值不斷地從某個值上下跳躍,速度似乎起作用,但它不斷地改變符號

問題出在第 103 行:

 if ball.rect.y >= 0:

您必須錯過鍵入它,因為 y 值將始終大於 0,我通過將其切換為來修復它

 if ball.rect.y <= 0:

它奏效了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM