当前位置: 首页 > 面试题库 >

我在此乒乓球比赛中划了一个边界,但球拍可以划过边界。如何停止呢?

孔砚
2023-03-14
问题内容

我在此乒乓球游戏中做了一个边框,屏幕上的桨可以越过它。我之前在另一段代码中已经做到了,但是现在一切都不同了。我有一个主要的想法如何做,您可能需要一个if语句,但是我没有所有的东西。

您可以删除“ pygame.load.image()”,因为您需要在文件夹中包含带有代码的图像,因此可以将其删除。会更好,因为您可以在python上尝试一下

#import modules
import pygame 
pygame.init()
#setting the variables for the window
WIDTH = 750
HEIGHT = 500
#making the wind\ow
win = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Pong Game")
#setting the most important variables
dest = (0, 0)
dest2 = (200, 200)
dest3 = (200, 50)
white = (255, 255, 255)
black = (0, 0, 0) 
green = (0, 255, 0)
red = (255, 0, 0)
yellow = (255, 255, 222)
#load the images
image = pygame.image.load("birdupbg.png")
image2 = pygame.image.load("birdup.png")
#making the paddle class
class Paddle(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([10, 75])
        self.image.fill(yellow)
        self.rect = self.image.get_rect()
        self.points = 0
#making the ball class
class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface([10, 10])
        self.image.fill(red)
        self.rect = self.image.get_rect()
        self.speed = 15 
        self.dx = 1
        self.dy = 1
        self.dx2 = 1
        self.dy2 = 1
#making the paddles
paddle1 = Paddle()
paddle1.rect.x = 25
paddle1.rect.y = 100

paddle2 = Paddle()
paddle2.rect.x = 715
paddle2.rect.y = 225


paddle3 = Paddle()
paddle3.rect.x = 715
paddle3.rect.y = 100

paddle4 = Paddle()
paddle4.rect.x = 25
paddle4.rect.y = 225
#paddle speed
paddle_speed = 50

pong = Ball()
pong.rect.x = 375
pong.rect.y = 250

ball = Ball()

all_sprites = pygame.sprite.Group()
all_sprites.add(paddle1, paddle2, paddle3, paddle4, pong)

#drawing the score and the word "pong" 
def redraw():
    win.fill(black)
    win.blit(image, dest)
    pygame.draw.line(win, (255, 255, 255), [0, 200], [900, 200], 10)
    font = pygame.font.SysFont("impact", 40)
    text = font.render("PONG", False, white)
    textRect = text.get_rect()
    textRect.center = (750//2, 25)
    win.blit(text, textRect)


    p1_score = font.render(str(paddle1.points), False, white)
    p1Rect = p1_score.get_rect()
    p1Rect.center = (50, 50)
    win.blit(p1_score, p1Rect)

    p2_score = font.render(str(paddle2.points), False, white)
    p2Rect = p2_score.get_rect()
    p1Rect.center = (700, 50)
    win.blit(p2_score, p1Rect)

    all_sprites.draw(win)
    pygame.display.update()
#what happens when the window runs
run = True

while run:
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    #if a certain key is pressed, move something
    key = pygame.key.get_pressed()
    if key[pygame.K_w]:
        paddle1.rect.y += -paddle_speed
    if key[pygame.K_s]:
        paddle1.rect.y +=  paddle_speed
    if key[pygame.K_UP]:
        paddle2.rect.y += -paddle_speed
    if key[pygame.K_DOWN]:
        paddle2.rect.y +=  paddle_speed
    if key[pygame.K_i]:
        paddle3.rect.y += -paddle_speed
    if key[pygame.K_j]:
        paddle3.rect.y += paddle_speed
    if key[pygame.K_1]:
        paddle4.rect.y += -paddle_speed
    if key[pygame.K_2]:
        paddle4.rect.y += paddle_speed
    pong.rect.x += pong.speed * pong.dx
    pong.rect.y += pong.speed * pong.dy
    #scoring system
    if pong.rect.y > 480:
        pong.dy = -1
    if pong.rect.x > 735:
        pong.rect.x, pong.rect.y = 375, 250
        pong.dx = -1
        paddle1.points += 1
        if pong.rect.y < -1:
            pong.dy = 1

    if pong.rect.x < 10:
        pong.rect.x, pong.rect.y = 375, 250
        pong.dx = 1
        paddle2.points += 1

    #if the ball and paddle collide, bounce off it
    if paddle1.rect.colliderect(pong.rect):
        pong.dx = 1
    if paddle2.rect.colliderect(pong.rect):
        pong.dx = -1
    if paddle1.rect.colliderect(pong.rect):
        pong.dx = 1
    if paddle3.rect.colliderect(pong.rect):
        pong.dx = -1
    if paddle4.rect.colliderect(pong.rect):
        pong.dx = 1
    #Call the redraw function
    redraw()  
    #update the window and quit it
    pygame.display.flip()
pygame.quit()

问题答案:

为了使球拍保持在场地上,请在游戏循环中添加一个复选标记,以在球拍到达边缘时阻止球拍移动。

.......
if pong.rect.x < 10:
    pong.rect.x, pong.rect.y = 375, 250
    pong.dx = 1
    paddle2.points += 1

# keep paddles in correct range     # add this section
if paddle1.rect.y < 0 : paddle1.rect.y = 0
if paddle3.rect.y < 0 : paddle3.rect.y = 0
if paddle2.rect.y < 200 : paddle2.rect.y = 200
if paddle4.rect.y < 200 : paddle4.rect.y = 200
if paddle1.rect.y > 200 - 75 : paddle1.rect.y = 200 - 75
if paddle3.rect.y > 200 - 75 : paddle3.rect.y = 200 - 75
if paddle2.rect.y > 500 - 75 : paddle2.rect.y = 500 - 75
if paddle4.rect.y > 500 - 75 : paddle4.rect.y = 500 - 75

#if the ball and paddle collide, bounce off it
if paddle1.rect.colliderect(pong.rect):
    pong.dx = 1
if paddle2.rect.colliderect(pong.rect):
    pong.dx = -1
.....


 类似资料:
  • 所以我试着在处理过程中对乒乓球进行编码,一切正常,我可以完美地上下移动球拍,但是,当你试图同时移动两个球拍时,他们不动/它不让你动(我将把这变成一个两人游戏,这样两个人可以使用同一个键盘,但不同的按键可以玩不同的桨)。 我认为这是使用“key”或“keyPressed”的问题,因为我认为它不能同时检测这两个或其他东西?但我似乎不知道如何解决这个问题或任何替代方案。(请记住,我知道如何移动桨叶,只是

  • 通常只有Y轴运动在这样的游戏中可用,但我决定让它在这样一种方式,X轴桨运动也被允许。 起初,我认为这与像素有关,但我也尝试了多种方式改变桨碰撞,但没有任何积极的结果。无论我试了多少次,球都不会从桨上弹下来。 代码非常简单,我每次只移动1px: 为了移动桨叶,我使用简单的标志,在按键时将移动方向设置为“开”,在按键释放时设置为“关”。我在不定式循环中运行它,所以它不断地被执行。 这就是“run()”

  • 9.1.3 编程案例:乒乓球比赛模拟 众所周知,中国乒乓球项目的技术水平世界第一,以至于所有比赛的冠军几乎都由中国球员包办。为了增强乒乓球运动的吸引力,提高其他国家的人对这项运动的兴趣,国际乒联 想了很多办法来削弱中国球员的绝对优势,例如扩大乒乓球的直径、禁用某些种类的球拍、 改变赛制等等。在本节中,我们将编写程序来模拟乒乓球比赛,以便研究一项针对中国球员 的规则改革是否真的有效。这项改革是:从

  • 本文向大家介绍如果用乒乓球塞满一个教室,请你估算所需乒乓球数量。相关面试题,主要包含被问及如果用乒乓球塞满一个教室,请你估算所需乒乓球数量。时的应答技巧和注意事项,需要的朋友参考一下 先计算乒乓球的体积:由于测量麻烦,就用一杯水,把乒乓球塞进去,测量溢出水的体积。 再计算乒乓球的直径。 测量教室的宽、高。分别用宽/乒乓球直径=n(取整),高/乒乓球直径=m(取整)。 再用教室的长/乒乓球直径=x(

  • 我想做一个正在处理的乒乓球游戏。但是球的移动不是很平稳。我试着改变帧速率并降低球的速度,但是移动速度似乎不是恒定的。这可能是性能问题吗?我是否做错了什么,或者即使对于简单的游戏,处理可能也不是正确的事情?(我的目标是试用processing.js,制作一款没有插件的游戏)。这是我的代码:

  • 附言。一个边界点和圆心之间的距离在这里不合适,因为圆位于它的边界直角内。