我正在制作一个简单的乒乓球游戏,而且我已经编写了大部分代码。
问题是,一旦球落下,如果继续移动球拍,它将从底部反弹回屏幕。一旦它错过了桨,我需要它永久地离开屏幕。
感谢任何反馈!提前谢谢!
L1_base.py(我的基本代码):
import math
import random
import sys, pygame
from pygame.locals import *
import ball
import colors
import paddle
# draw the scene
def draw(screen, ball1, paddle1) :
screen.fill((128, 128, 128))
ball1.draw_ball(screen)
paddle1.draw_paddle(screen)
#function to start up the main drawing
def main():
pygame.init()
width = 600
height = 600
screen = pygame.display.set_mode((width, height))
ball1 = ball.Ball(300, 1, 40, colors.YELLOW, 0, 4)
paddle1 = paddle.Paddle(200, 575, colors.GREEN, 100, 20)
while 1:
for event in pygame.event.get():
if event.type == QUIT: sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
paddle1.update_paddle('right', 35)
if event.key == pygame.K_LEFT:
paddle1.update_paddle('left', 35)
ball1.test_collide_top_ball(0)
ball1.test_collide_bottom_ball(600, paddle1)
ball1.update_ball()
draw(screen, ball1, paddle1)
pygame.display.flip()
if __name__ == '__main__':
main()
ball.py(包含球类/方法):
import pygame
class Ball:
def __init__(self, x, y, radius, color, dx, dy):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.dx = dx
self.dy = dy
def draw_ball(self, screen):
pygame.draw.ellipse(screen, self.color,
pygame.Rect(self.x, self.y, self.radius, self.radius))
def update_ball(self):
self.x += self.dx
self.y += self.dy
def test_collide_top_ball(self, top_height):
if (self.y <= top_height) and (self.dy < 0):
self.dy *= -1
def test_collide_bottom_ball(self, coll_height, paddle):
if (self.y >= coll_height - self.radius - (600 - paddle.y)) and (self.x >= paddle.x) and (self.x <= paddle.x + paddle.width) and (self.dy > 0):
self.dy *= -1
paddle.py(包含paddle类/方法):
import pygame
class Paddle:
def __init__(self, x, y, c, w, h):
self.x = x
self.y = y
self.color = c
self.width = w
self.height = h
def draw_paddle(self, screen):
pygame.draw.rect(screen, self.color,
pygame.Rect(self.x, self.y, self.width, self.height), 0)
def update_paddle(self, dir, dx):
if (dir == 'left'):
self.x = max(self.x - dx, 0)
else:
self.x = min(self.x + dx, 600 - self.width)
def get_left(self):
if (self.x < 300):
return self.x
def get_right(self):
if (self.x >= 300):
return self.x
答案 0 :(得分:0)
你试图将过多的东西塞进if
中的一个test_collide_bottom_ball
语句中,这使得调试变得更加困难。在这种情况下,考虑到{strong>不到self.dy *= -1
的所有原因,您应该更容易理解。切换方向是一种特权划分,应该只在特殊场合发生。
我已经离开了我的调试代码,以澄清我对问题的思考方式,还有另一个错误可以让球卡在桨中,但是你应该能够解决这个问题。请参阅以下代码:
def test_collide_bottom_ball(self, coll_height, paddle):
print paddle.x, paddle.width + paddle.x, self.x
# shoot ball back to top when it has gone off the bottom
if self.y > 600:
self.y = 300
return
# ignore when the ball is far from the y access of the paddle
if not self.y >= coll_height - self.radius - (600 - paddle.y):
return
# check that the current x plus raidus to find "too small" condition
if self.x + self.radius <= paddle.x:
print "self.x too small"
return
# for too big we have to consider the paddle width
if self.x >= paddle.x + paddle.width:
print "self.x too big"
return
# ok, looks like its time to switch directions!
self.dy *= -1
P.S。使用pep8
编码标准实用程序。在pep8
之后编码并不困难,并且会让您头痛。