gpt4 book ai didi

python - 部分碰撞错误

转载 作者:太空宇宙 更新时间:2023-11-03 17:38:10 27 4
gpt4 key购买 nike

我目前正在尝试使用 pygame 制作 Pong 版本,但在检测球和 Racket 之间的碰撞时遇到一些问题。我对两个 Racket 都进行了检测,但我很快意识到,当球与 Racket 碰撞而没有球穿过时,左侧玩家无法移动 Racket 。右边的球员则没有这样的问题,保留左边的球员仍然可以让球正常反射。

import pygame, sys
from pygame.locals import *

pygame.init()
pygame.mixer.init()
pygame.font.init()

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

screenx = 1080
screeny = 720

clock = pygame.time.Clock()
for pygame.mouse.get_pos in (screenx, screeny):
pygame.mouse.set_visible(False)


# Blueprint for creating player character
class Player:
# Defines initial conditions for player
def __init__(self):
self.xpos = 0
self.ypos = 0
self.width = 20
self.length = 100
self.speed = 10
self.move = 0
self.upkey = 0
self.downkey = 0
self.score = 0
self.rect = pygame.Rect((self.xpos, self.ypos, self.width, self.length))

# if player character, defines paddle movement
def player_move(self, key):
if key[self.upkey]:
self.ypos -= self.speed
self.move = -1
elif key[self.downkey]:
self.ypos += self.speed
self.move = 1
else:
self.move = 0

# Creates screen boundary for players
def player_boundary(self, key):
if self.ypos == 0:
self.speed = 0
self.move = 0
if key[self.downkey]:
self.speed = 10
self.move = -1
elif self.ypos + self.length == screeny:
self.speed = 0
self.move = 0
if key[self.upkey]:
self.speed = 10
self.move = 1

# Draws player rectangle to surface
def draw(self, surface):
self.rect = pygame.draw.rect(surface, WHITE, (self.xpos, self.ypos, self.width, self.length))


# Blueprint for creating the ball
class Ball:
def __init__(self):
self.size = 15
self.speed = 10
self.vy = 0
self.vx = 10
self.ypos = screeny/2
self.xpos = 100
self.rect = pygame.Rect((self.xpos, self.ypos, self.size, self.size))

# Defines movement of ball (in xy components)
def ball_move(self):
self.ypos += self.vy
self.xpos += self.vx

# method for ball bouncing off of player paddle **
def ball_block(self, player):
if player.move > 0:
self.vy += player.speed * 0.3
self.vx = -((self.speed**2 - self.vy**2) ** .5)
elif player.move < 0:
self.vy = player.speed * -0.3
self.vx = -((self.speed**2 - self.vy**2) ** .5)
elif player.move == 0:
self.vx = -self.vx

# for reflecting off of screen boundaries **
def ball_reflect(self, screeny):
if self.ypos == 0:
self.vy = -self.vy
elif self.ypos == screeny - self.size:
self.vy = -self.vy

# Method for detecting when player has scored
def score(self):
if self.xpos == 0:
self.vx = -self.vx
print ('Score')
elif self.xpos == screenx - self.size:
self.vx = -self.vx
print ('Score')

# Draws ball to surface
def draw(self, surface):
self.rect = pygame.draw.rect(surface, WHITE, (self.xpos, self.ypos, self.size, self.size))

def setup():
# Assigns values to player 1 (left side)
player1 = Player()
player1.xpos = 50
player1.ypos = screeny/1.5
player1.upkey = K_w
player1.downkey = K_s

# Assigns values to player 2 (right side)
player2 = Player()
player2.xpos = screenx - player2.width - 50
player2.ypos = screeny/1.5
player2.upkey = K_UP
player2.downkey = K_DOWN

return player1, player2

# Main loop of the game
def main():
player1, player2 = setup()
ball = Ball()
play = True

while play:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()

key = pygame.key.get_pressed()
if key[K_ESCAPE]:
play = False

if ball.rect.colliderect(player1): # For some reason, ball can go through P1 while moving
ball.ball_block(player1)
elif ball.rect.colliderect(player2):
ball.ball_block(player2)

# Had issue with ball going through bottom, fixed by adding screeny to dependencies
if ball.ypos == 0:
ball.ball_reflect(screeny)
elif ball.ypos == screeny - ball.size:
ball.ball_reflect(screeny)

player1.player_move(key)
player2.player_move(key)
ball.ball_move()

# Calls methods for player boundaries and ball scoring
player1.player_boundary(key)
player2.player_boundary(key)
ball.score() # Ball will only score if colliding head on and not on P2's side

# Draws objects to screen
surface.fill(BLACK)
player1.draw(surface)
player2.draw(surface)
ball.draw(surface)

pygame.display.update()
clock.tick(60)

# Sets up screen, calls main loop
surface = pygame.display.set_mode((screenx, screeny))
pygame.display.set_caption('PONG')

main()

据我所知,两个播放器的代码是相同的,但只有一个播放器按照我的预期方式工作。如有任何帮助,我们将不胜感激,并感谢您的宝贵时间。

编辑:通过向 ball_block 方法添加几行来修复

    def ball_block(self, player):
if player.move > 0:
self.vy += player.speed * 0.3
if self.vx < 0:
self.vx = ((self.speed**2 - self.vy**2) ** .5)
elif self.vx > 0:
self.vx = -((self.speed**2 - self.vy**2) ** .5)
elif player.move < 0:
self.vy = player.speed * -0.3
if self.vx < 0:
self.vx = ((self.speed**2 - self.vy**2) ** .5)
elif self.vx > 0:
self.vx = -((self.speed**2 - self.vy**2) ** .5)
elif player.move == 0:
self.vx = -self.vx

最佳答案

如果玩家在碰撞过程中移动,你总是在设置

self.vx = -((self.speed**2 - self.vy**2) ** .5)

所以它总是负数

关于python - 部分碰撞错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30929750/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com