- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的脚本,它需要制作一个碰撞盒,以便球从物体上弹开。这是一个非常基本的乒乓球游戏。我尝试过坐标并尝试观看其他视频和论坛,我真的很感激有人能帮助我。我根本不明白,这是一项学校作业。我很懒,但是有人可以为我做一个碰撞盒吗?或者至少帮助我。
这是脚本:
import pygame
pygame.init()
width = 500
height = 400
#pygame.title("Ping Pong Level 1")
screen = pygame.display.set_mode((width, height))
y = 30
g = 30
x = 40
f = 100
velxX = 2
velfF = 2
velX = 50
velF = 20
clock = pygame.time.Clock()
def draw():
screen.fill((0, 0, 0))
player = pygame.draw.rect(screen, (255, 255, 255), (30, y, 30, 100))
player1 = pygame.draw.rect(screen, (255, 255, 255), (440, g, 30, 100))
line = pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
pressed = pygame.key.get_pressed()
if pressed[pygame.K_w] and y > 0:
y -= 5
if pressed[pygame.K_s] and y < height - 100:
y += 5
if pressed[pygame.K_UP] and g > 0:
g -= 5
if pressed[pygame.K_DOWN] and g < height - 100:
g += 5
velF += velfF
velX += velxX
if velF < 0 or velF > height - 5:
velfF *= -1
if velX < 0 or velX > width - 5:
velxX *= -1
pygame.draw.circle(screen, (0, 255, 0), (velX, velF), 5)
clock.tick(60)
pygame.display.flip()
draw()
screen.mainloop()
最佳答案
好的,让我们逐步完成这一步。
首先,让我们重新排序您的代码,这样我们就有了一个不错的标准主循环:事件处理 -> 状态更新 -> 绘制
。我们看到您有一个 draw
函数,但是您并没有在这个函数中绘制所有内容。此外,您在 draw
之前调用了 pygame.display.flip()
。 screen.mainloop()
没有意义,sys.exit()
也不会工作,因为您没有导入 sys
。
第一步:
import pygame
def main():
pygame.init()
width = 500
height = 400
#pygame.title("Ping Pong Level 1")
screen = pygame.display.set_mode((width, height))
y = 30
g = 30
x = 40
f = 100
velxX = 2
velfF = 2
velX = 50
velF = 20
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
pressed = pygame.key.get_pressed()
if pressed[pygame.K_w] and y > 0:
y -= 5
if pressed[pygame.K_s] and y < height - 100:
y += 5
if pressed[pygame.K_UP] and g > 0:
g -= 5
if pressed[pygame.K_DOWN] and g < height - 100:
g += 5
velF += velfF
velX += velxX
if velF < 0 or velF > height - 5:
velfF *= -1
if velX < 0 or velX > width - 5:
velxX *= -1
screen.fill((0, 0, 0))
pygame.draw.circle(screen, (0, 255, 0), (velX, velF), 5)
player = pygame.draw.rect(screen, (255, 255, 255), (30, y, 30, 100))
player1 = pygame.draw.rect(screen, (255, 255, 255), (440, g, 30, 100))
line = pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
接下来,让我们看看你的游戏状态和绘图。您有一堆名称奇怪的变量,以及您不使用或没有意义的变量(例如 velxX
、f
或 player1
) 您还绘制了一些矩形,所以让我们使用适当的数据结构来解决问题:有用的 Rect
和 Vector2
类。
第 2 步:
import pygame
def main():
pygame.init()
screen = pygame.display.set_mode((500, 400))
screen_rect = screen.get_rect()
left_paddle = pygame.Rect((30, 30, 30, 100))
right_paddle = pygame.Rect((440, 30, 30, 100))
ball_position = pygame.Vector2((50, 20))
ball_direction = pygame.Vector2((1, 1)).normalize()
ball_speed = 5
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
pressed = pygame.key.get_pressed()
if pressed[pygame.K_w]: left_paddle.move_ip(0, -5)
if pressed[pygame.K_s]: left_paddle.move_ip(0, 5)
if pressed[pygame.K_UP]: right_paddle.move_ip(0, -5)
if pressed[pygame.K_DOWN]: right_paddle.move_ip(0, 5)
# keep paddles on the screen
left_paddle.clamp_ip(screen_rect)
right_paddle.clamp_ip(screen_rect)
ball_position += ball_direction * ball_speed
# flip direction on edge
if ball_position[1] < 0 or ball_position[1] > screen_rect.height - 5:
ball_direction = pygame.Vector2(ball_direction[0], -ball_direction[1])
if ball_position[0] < 0 or ball_position[0] > screen_rect.width - 5:
ball_direction = pygame.Vector2(-ball_direction[0], ball_direction[1])
screen.fill((0, 0, 0))
pygame.draw.rect(screen, (255, 255, 255), left_paddle)
pygame.draw.rect(screen, (255, 255, 255), right_paddle)
pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
pygame.draw.circle(screen, (0, 255, 0), [int(v) for v in ball_position], 5)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
到目前为止,还不错。现在实现碰撞检测很容易。我们可以计算下一步球的位置,然后检查它们是否会与桨碰撞。由于我们使用的是 Rect
类,很容易检查碰撞(collidepoint
)并使它们变大(inflate
,因为球的大小大于一个像素)。
第 3 步:
import pygame
def main():
pygame.init()
screen = pygame.display.set_mode((500, 400))
screen_rect = screen.get_rect()
left_paddle = pygame.Rect((30, 30, 30, 100))
right_paddle = pygame.Rect((440, 30, 30, 100))
ball_position = pygame.Vector2((150, 120))
ball_direction = pygame.Vector2((1, 1)).normalize()
ball_speed = 5
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
pressed = pygame.key.get_pressed()
if pressed[pygame.K_w]: left_paddle.move_ip(0, -5)
if pressed[pygame.K_s]: left_paddle.move_ip(0, 5)
if pressed[pygame.K_UP]: right_paddle.move_ip(0, -5)
if pressed[pygame.K_DOWN]: right_paddle.move_ip(0, 5)
# keep paddles on the screen
for paddle in (left_paddle, right_paddle):
paddle.clamp_ip(screen_rect)
ball_position += ball_direction * ball_speed
# flip direction on edge
if ball_position[1] < 0 or ball_position[1] > screen_rect.height - 5:
ball_direction = pygame.Vector2(ball_direction[0], -ball_direction[1])
if ball_position[0] < 0 or ball_position[0] > screen_rect.width - 5:
ball_direction = pygame.Vector2(-ball_direction[0], ball_direction[1])
next_ball_position = ball_position + ball_direction * ball_speed
for paddle in (left_paddle, right_paddle):
if paddle.inflate(5, 5).collidepoint(next_ball_position):
ball_direction = pygame.Vector2(-ball_direction[0], ball_direction[1])
screen.fill((0, 0, 0))
for paddle in (left_paddle, right_paddle):
pygame.draw.rect(screen, (255, 255, 255), paddle)
pygame.draw.rect(screen, (0, 0, 255), (250, 0, 10, 400))
pygame.draw.circle(screen, (0, 255, 0), [int(v) for v in ball_position], 5)
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
请注意,这仅在球击中前面的 Racket 时有效,但您可以轻松检查球相对于 Racket 的位置以确定是否要更改球方向的 y
或 x
轴。
而且由于我们使用矢量表示球的方向,因此让球以 45° 以外的角度移动也很容易。
关于python - 无法为我的 Ping Pong pygame 创建一个 Hitbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55276071/
我使用 Xcode 和 swift 5 构建了一个应用程序。 每次我点击“简单”、“中等”、“困难”或“2 人”按钮时,我都会收到错误消息: Could not cast value of type
所以我和我的搭档正在尝试制作一个玩家对电脑的乒乓球游戏,但我们就是不知道如何让电脑输。 我们已经完成了基本的工作,它运行良好,但计算机永远不会丢失。我们还尝试使用 sleep 来减慢计算机的速度,但是
我应该如何设计“适当的” OO Pong? 正确的想法是可以更改任何元素:球,球场和 Racket 的形状,增加了障碍,甚至是“助力”,例如“可以将球粘在 Racket 上”。 目前,还有两个紧迫的问
我正在尝试用 Java 制作 Pong 游戏,但在球从 Racket 上弹起时遇到了一些问题。 我有两个桨:左桨1,右桨2。 我的想法如下:ball_Y应该在 Racket 的高度之间,ball_X触
我对 Java 还很陌生,需要一些帮助。我已经创建了 Pong 游戏(使用 Eclipse),并且在大多数情况下,它运行得很好。然而,碰撞检测有些不对劲。球从人类控制的 Racket 上反弹得很好,但
我正在打乒乓球,我已经将球的 x 坐标设置为在它碰到 Racket 时立即反转,并在它没有击中 Racket 时停止。此代码在“大部分”时间都有效,但“有时”球会在没有明显原因的情况下一击中 Rack
我正在创建一个 JS Pong 游戏,但 Pong 游戏中的球在几秒钟后开始滞后。我尝试停止动画帧,优化代码以获得更好的性能并重写球的代码,但没有任何效果。有人可以帮我吗? HTML(无 CSS)
我很困惑为什么我的记分板没有在屏幕上更新。分数正在增加(我用调试器检查过,球也正在居中)。但记分牌根本不更新,它不断显示“0:0” 乒乓球类 package com.dheraxysgames.pon
最近我编写了一个pygame 'Pong',我还没有完成它,因为桨还不能移动。 但是,我在这里遇到了一个问题。因为我想要得到的分数等于球击中 window 边缘的次数。对于我的程序,当球撞到墙壁时,得
嗨,我写了这个简单的碰撞检测和弹跳算法,但是碰撞检测到 y 就好像它是 x 轴一样 bool Ball::DetectCollision(Paddle p) { if(GetPosition().y
我正在用 Python 制作经典的 Pong,但我需要帮助。 对于/在这个游戏中,我想计算球的轨迹,因此对于给定的开始点(在左桨上弹跳)和角度(绿色物体),我想计算终点(蓝色 X)。这一切都是为了将
我有一个 UIImageView,其中加载了一个 png 图像序列。 我的问题是 - 你知道有什么方法可以“乒乓”动画序列吗?这样就从 1-24 向前播放,然后从 24-1 向后播放并循环。 (技术上
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a software
我正在用java制作乒乓球,但敌人的AI没有动。它应该向球移动的方向移动。请问有人可以帮助我吗? 主类: import javax.swing.*; public class Main extends
我正在用java制作一个乒乓球类型的游戏,我试图让球从墙上弹开,但每当球撞到球时,它就会停止,它不会从墙上反射出来,我看不到找出原因。 处理球运动的球类 public class Ball { pri
我正在为我的 Pong 克隆编写一些困难,我写这些是为了熟悉 SFML 和 Xcode。对于最难的难度,我想创建一个人工智能关卡,让计算机立即知道球会去哪里。所以,如果我有 xVelocity 和 y
我是 HTML 5 + Canvas 游戏开发新手。试图在我的 Pong 游戏中为球制作动画。这是我的 code .相关片段: 增加球的速度: Ball.prototype.update = func
好吧,我为此搜索了很多,但我能找到的只是人们说的像 pi * direction,方向是我假设的球进入的角度。但我的问题是,我不知道我是如何得到球进入的角度的,所以我做不到这些。如果有人可以解释我将如
试图在 java 中制作乒乓球但不能同时移动两个 Racket 。您可以移动其中一个,但不能同时移动两者。我是否需要创建具有 2 个不同面板的 2 个线程? 这里是我指定关键事件的地方 public
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我是一名优秀的程序员,十分优秀!