嗨,我对编码非常陌生,一直在努力提高自己的流利程度。这就是我为什么开始用Pygame编写乒乓球游戏的原因,因为我想要一些视觉效果。所以我创造了一个带有工作桨和一个球的乒乓球设计,我已经从墙上弹跳但是我很难获得桨式碰撞核心。我有它所以当我的桨和球对齐时它会打印Paddle但它并不总能检测到它。如何使其具有更高的灵敏度并为桨叶添加弹跳。我再一次非常新,所以任何语法更正或使代码更容易阅读或简化的建议都会很棒:代码如下:
import sys, pygame, pygame.mixer, random, math
from pygame.locals import *
pygame.init()
screen_size = width, height = 600, 400
black = 0,0,0
red = (200,0,0)
blue = 0,0,200
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("PONG WANNABE")
screen.fill(black)
paddle = pygame.image.load("paddle.png")
divider = pygame.image.load("divider.png")
def addVectors((angle1, length1), (angle2, length2)):
x = math.sin(angle1) * length1 + math.sin(angle2) * length2
y = math.cos(angle1) * length1 + math.cos(angle2) * length2
length = math.hypot(x, y)
angle = 0.5 * math.pi - math.atan2(y, x)
return (angle, length)
class Ball:
def __init__(self):
self.size = 8
self.x = width/2
self.y = height/2
self.colour = (255, 255, 255)
self.thickness = 0
self.speed = 0.05
self.angle = math.pi/2
def display(self):
pygame.draw.circle(screen,self.colour,int(self.x),int(self.y)),
self.size,self.thickness)
def move(self):
self.x += math.sin(self.angle) * self.speed
self.y -= math.cos(self.angle) * self.speed
def bounce(self):
if self.x > width - self.size:
self.x = 2*(width - self.size) - self.x
self.angle = - self.angle
elif self.x < self.size:
self.x = 2*self.size - self.x
self.angle = - self.angle
if self.y > height - self.size:
self.y = 2*(height - self.size) - self.y
self.angle = math.pi - self.angle
elif self.y < self.size:
self.y = 2*self.size - self.y
self.angle = math.pi - self.angle
ball = Ball()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.display.quit()
sys.exit()
elif event.type == KEYDOWN and event.key == K_SPACE:
black = blue
elif event.type == KEYUP and event.key == K_SPACE:
black = 0,0,0
screen.fill(black)
mx,my = pygame.mouse.get_pos()
screen.blit(divider,(width/2, 0))
screen.blit(paddle,(10,my-35))
screen.blit(paddle,(width-20,365-my))
ball.move()
if ball.x < my + 73 and ball.x > my:
print "Paddle"
ball.bounce()
ball.display()
pygame.display.flip()
答案 0 :(得分:0)
我会诚实地尝试使用矩形并检查矩形是否发生碰撞。完整的文档可以在http://www.pygame.org/docs/ref/rect.html找到,但我仍然会直接解释你的情况。
在Ball
课程中,您可能想要创建一个Rect。
class Ball:
def __init__(self):
#all of your x, y etc.
#add in a react at the end of init
self.rect = pygame.Rect(self.x, self.y, self.size, self.size)
#self.size is the width and height
然后在函数move
的末尾,您需要更新rect
def move(self):
#move ball with what you already have
#now update the rect
self.rect.x = x
self.rect.y = y
现在你想要为桨创建一个矩形,现在我不知道你的桨有多宽,但我只想说10,你可以根据需要调整它。
现在添加矩形。在获得paddle的x和y之后的while循环中,添加此行以创建矩形
mrect = pygame.Rect(mx,my,10,73)
所以现在你有一个矩形可以用来检查碰撞点。现在代替此行if ball.x < my + 73 and ball.x > my:
,您需要更改此行以检查两个矩形之间的碰撞点。我们将通过使用属于名为pygame.Rect
的类colliderect
的函数来实现此目的
因此,将行if ball.x < my + 73 and ball.x > my:
更改为此if ball.rect.colliderect(mrect):
,这将检查矩形,球或桨是否在任何点发生碰撞。
答案 1 :(得分:0)
这是双球乒乓球。名称球实例circle_m和circle_mc。 name paddle instances矩形和rectangle_mc。名称边界boundary1和边界。命名目标goal1和goal2。命名文本框textField_1,textField_2和textField_3。然后使用此代码,玩得开心。 (显然必须用adobe flash制作)
//1.
var xDir2 = 1;
var yDir2 = 1;
stage.addEventListener(Event.ENTER_FRAME, circleHit);
var xDir = 1;
var yDir = 1;
var score = 0;
var score2 = 0;
//2.
function circleHit(event:Event):void
{
{
circle_mc.x += 10 * xDir;
circle_mc.y += 10 * yDir;
circle_m.x += 5 * xDir2;
circle_m.y += 5 * yDir2;
if (circle_mc.hitTestObject(rectangle_mc))
{
xDir *= -1;
}
};
if (circle_mc.hitTestObject(rectangle))
{
xDir *= -1;
}
if (circle_mc.hitTestObject(boundary))
{
yDir *= -1;
}
if (circle_mc.hitTestObject(boundary1))
{
yDir *= -1;
}
if (circle_mc.hitTestObject(goal2))
{
score2++;
circle_mc.y = 161.95;
circle_mc.x = 273;
}
textField_2.text = score2;
textField_1.text = score;
if (circle_mc.hitTestObject(goal1))
{
score++;
circle_mc.y = 161.95;
circle_mc.x = 273;
}
if (circle_m.hitTestObject(goal2))
{
score2++;
circle_m.x = 273;
circle_m.y = 161.95;
}
if (circle_m.hitTestObject(goal1))
{
score++;
circle_m.x = 273;
circle_m.y = 161.95;
}
if (circle_m.hitTestObject(rectangle_mc))
{
xDir2 *= -1;
}
if (circle_m.hitTestObject(rectangle))
{
hSiz2 *= -2;
xDir2 *= -1;
}
if (circle_m.hitTestObject(boundary))
{
yDir2 *= -1;
}
if (circle_m.hitTestObject(boundary1))
{
yDir2 *= -1;
}
}
//This section creates variables to store the condition of each key A(up or down)
var keyUP:Boolean = false;
var keyDOWN:Boolean = false;
var keyRIGHT:Boolean = false;
var keyLEFT:Boolean = false;
此部分创建事件侦听器。前两个检查是否已按下任何键或重新启动。第三个事件监听器控制重复循环
stage.addEventListener(KeyboardEvent.KEY_UP,key_Up);
stage.addEventListener(KeyboardEvent.KEY_DOWN,key_Down);
stage.addEventListener(Event.ENTER_FRAME,loop);
如果键出现,这些函数会改变变量的值。开关语句;检查正在处理的密钥。 case语句为每个键提供了代表键的数字的选项。打破结束案件。
function key_Up(event:KeyboardEvent)
{
switch (event.keyCode)
{
case Keyboard.W :
keyLEFT = false;
break;
case 38 :
keyUP = false;
break;
case Keyboard.S :
keyRIGHT = false;
break;
case 40 :
keyDOWN = false;
}
}
function key_Down(event:KeyboardEvent)
{
switch (event.keyCode)
{
case Keyboard.W :
keyLEFT = true;
break;
case 38 :
keyUP = true;
break;
case Keyboard.S :
keyRIGHT = true;
break;
case 40 :
keyDOWN = true;
}
}
每次输入一帧时,此功能都会重复。它使用变量的值来控制操作。 trace语句将在输出窗口中显示一个单词
function loop(event:Event)
{
if (keyUP)
{
rectangle_mc.y -= 20;
}
if (keyDOWN)
{
rectangle_mc.y -= -20;
}
if (keyLEFT)
{
rectangle.y -= 20;
}
if (keyRIGHT)
{
rectangle.y -= -20;
}
if (score ==10)
{
textField_3.text = "Left side WINS!!!!!";
circle_mc.y = -5000;
circle_mc.x = -5000;
}
if (score2 ==10)
{
textField_3.text = "Right side WINS!!!!!";
circle_mc.y = -5000;
circle_mc.x = -5000;
}
if (score ==10)
{
textField_3.text = "Left side WINS!!!!!";
circle_m.y = -5000;
circle_m.x = -5000;
}
if (score2 ==10)
{
textField_3.text = "Right side WINS!!!!!";
circle_m.y = -5000;
circle_m.x = -5000;
}
}