Python 没有精灵的碰撞?

Python 没有精灵的碰撞?,python,pygame,collision,Python,Pygame,Collision,我一直在尝试使用python 3.4.3为一个类创建Pong。我要做的最后一件事是在球和桨之间创建碰撞。另外,我刚刚才了解精灵,但我已经走得太远了,无法重新开始 这是我的密码: #import and start up pygame import pygame import math import time pygame.init() #define colors BLACK = ( 0, 0, 0) WHITE = ( 255, 255, 255) GREEN = ( 0, 255, 0)

我一直在尝试使用python 3.4.3为一个类创建Pong。我要做的最后一件事是在球和桨之间创建碰撞。另外,我刚刚才了解精灵,但我已经走得太远了,无法重新开始

这是我的密码:

#import and start up pygame
import pygame
import math
import time

pygame.init()

#define colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED =   ( 255, 0, 0)
BLUE =  ( 0, 0, 255)
PURPLE = (255, 0, 255)
YELLOW = (255, 255, 0)
TEAL = ( 0, 255, 255)
BROWN = ( 70, 50, 30)

#create window
size = (800, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")

done = False

#manage time
clock = pygame.time.Clock()


#ball movement
rect_x = 375
rect_y = 250
rect_change_x = 3
rect_change_y = 3

#paddle movement
y_speed_1 = 0
y_speed_2 = 0

#paddle coords so we can track them in real time
x_coord_1 = 70
x_coord_2 = 700
y_coord_1 = 200
y_coord_2 = 200

#score
score_player_1 = 0
score_player_2 = 0


time.sleep(1)
#main loop
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        #program that moves the paddles
        if event.type == pygame.KEYDOWN:

        #player one uses the WS keys
        if event.key == pygame.K_w:
            y_speed_1 = -3
        elif event.key == pygame.K_s:
            y_speed_1 = 3

        #player two uses the arrow keys
        elif event.key == pygame.K_DOWN:
            y_speed_2 = 3
        elif event.key == pygame.K_UP:
            y_speed_2 = -3

    #stops the paddle after done pressing key
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_w or event.key == pygame.K_s:
            y_speed_1 = 0

        elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
            y_speed_2 = 0


y_coord_1 += y_speed_1
y_coord_2 += y_speed_2


#ball bounces off of the top and bottom sides
if rect_y > 450 or rect_y < 0:
    rect_change_y = rect_change_y * -1

#player 1 score
elif rect_x > 751: 
    score_player_1 += 1
    rect_x = 375
    rect_y = 250
    rect_change_x = 0
    rect_change_y = 0
    rect_change_x = -4
    rect_change_y = -4
    time.sleep(1)


#player 2 score
elif rect_x < -1:
    score_player_2 += 1
    rect_x = 375
    rect_y = 250
    rect_change_x = 0
    rect_change_y = 0
    rect_change_x = 4
    rect_change_y = 4
    time.sleep(1)

#player 1 collision


#player 2 collision


#paddles program
def draw_paddle_1(x, y, z):
    pygame.draw.rect(screen, WHITE, [x_coord_1, y_coord_1, 30, 100])

def draw_paddle_2(x, y, z):
    pygame.draw.rect(screen, WHITE, [x_coord_2, y_coord_2, 30, 100])

#fill the screen
screen.fill(BLACK)

#ball
pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50, 50])


#move the starting point of the ball
rect_x += rect_change_x
rect_y += rect_change_y

#paddles
draw_paddle_1(screen, x_coord_1, y_coord_1)
draw_paddle_2(screen, x_coord_2, y_coord_2)

#score
def display_score():
    font = pygame.font.SysFont('System Bold', 35, True, False)
    text1 = font.render(str(score_player_1), True, WHITE)
    text2 = font.render(str(score_player_2), True, WHITE)
    screen.blit(text1, [375, 10])
    screen.blit(text2, [425, 10])

def player_win():
    font = pygame.font.SysFont('System Bold', 40, True, False)
    player_1_win = font.render("Player 1 Wins!", True, WHITE)
    player_2_win = font.render("Player 2 Wins!", True, WHITE)

    if score_player_1 == 7:
        screen.blit(player_1_win, [300, 100])
        pygame.display.flip()
        time.sleep(1)
        pygame.quit()

    if score_player_2 == 7:
        screen.blit(player_2_win, [300, 100])
        pygame.display.flip()
        time.sleep(1)
        pygame.quit()

    #call programs
    display_score()
    player_win()

    #make the window display the drawings
    pygame.display.flip()



    #limit to 60 frames per second
    clock.tick(60)


#close window and quit
pygame.quit()
#导入并启动pygame
导入pygame
输入数学
导入时间
pygame.init()
#定义颜色
黑色=(0,0,0)
白色=(255,255,255)
绿色=(0,255,0)
红色=(255,0,0)
蓝色=(0,0255)
紫色=(255,0255)
黄色=(255,255,0)
TEAL=(0,255,255)
布朗=(70,50,30)
#创建窗口
大小=(800500)
screen=pygame.display.set_模式(大小)
pygame.display.set_标题(“Pong”)
完成=错误
#管理时间
clock=pygame.time.clock()
#球的运动
矩形x=375
矩形y=250
rect_change_x=3
rect_change_y=3
#桨叶运动
y_速度_1=0
y_速度_2=0
#划桨coords,这样我们可以实时跟踪它们
x_坐标_1=70
x_坐标2=700
y_coord_1=200
y_坐标2=200
#得分
得分\玩家\ 1=0
得分\玩家\ 2=0
时间。睡眠(1)
#主回路
虽然没有这样做:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
#移动划桨的程序
如果event.type==pygame.KEYDOWN:
#玩家一使用WS键
如果event.key==pygame.K_w:
y_速度_1=-3
elif event.key==pygame.K_:
y_速度_1=3
#玩家二使用箭头键
elif event.key==pygame.K_向下:
y_速度_2=3
elif event.key==pygame.K_UP:
y_速度_2=-3
#按完键后停止划桨
elif event.type==pygame.KEYUP:
如果event.key==pygame.K_w或event.key==pygame.K_s:
y_速度_1=0
elif event.key==pygame.K_向上或event.key==pygame.K_向下:
y_速度_2=0
y_坐标_1+=y_速度_1
y_坐标_2+=y_速度_2
#球从顶部和底部反弹
如果rect_y>450或rect_y<0:
rect\u change\u y=rect\u change\u y*-1
#球员1分
elif rect_x>751:
得分\u玩家\u 1+=1
矩形x=375
矩形y=250
rect_change_x=0
rect_change_y=0
rect_change_x=-4
rect_change_y=-4
时间。睡眠(1)
#球员2得分
elif rect_x<-1:
得分\玩家\ 2+=1
矩形x=375
矩形y=250
rect_change_x=0
rect_change_y=0
rect_change_x=4
rect_change_y=4
时间。睡眠(1)
#玩家1碰撞
#玩家2碰撞
#划桨程序
def牵引桨1(x,y,z):
pygame.draw.rect(屏幕,白色,[x_-coord_1,y_-coord_1,30100])
def牵引桨2(x,y,z):
pygame.draw.rect(屏幕,白色,[x_坐标2,y_坐标2,30100])
#满屏
屏幕填充(黑色)
#球
pygame.draw.rect(屏幕,白色,[rect_x,rect_y,50,50])
#移动球的起点
rect_x+=rect_change_x
rect_y+=rect_change_y
#划桨
划桨1(屏幕,x坐标1,y坐标1)
划桨2(屏幕,x坐标2,y坐标2)
#得分
def显示_分数():
font=pygame.font.SysFont('System Bold',35,True,False)
text1=font.render(str(score\u player\u 1),真,白色)
text2=font.render(str(score\u player\u 2),真,白色)
screen.blit(text1[375,10])
screen.blit(text2[425,10])
def player_win():
font=pygame.font.SysFont('System Bold',40,True,False)
player_1_win=font.render(“player 1 Wins!”,真,白色)
player_2_win=font.render(“player 2 Wins!”,真,白色)
如果得分_玩家_1==7:
blit(玩家赢[300100])
pygame.display.flip()
时间。睡眠(1)
pygame.quit()
如果得分_玩家_2==7:
blit(玩家赢[300100])
pygame.display.flip()
时间。睡眠(1)
pygame.quit()
#呼叫程序
显示分数()
玩家_win()
#使窗口显示图形
pygame.display.flip()
#限制为每秒60帧
时钟滴答(60)
#关闭窗口并退出
pygame.quit()
也请让我知道,如果我应该做任何其他编辑。
仅供参考,这需要在明天之前完成,所以我真的不能在这上面花太多时间。

嘿,欢迎来到stackoverflow!我得到了你的解决方案,但它不是很好,也不是很多努力,但我会告诉你算法,希望你能做出比我更好的算法。基本上你所要说的就是,如果球的矩形区域,与任何一个球拍相交,将其反弹(在这种情况下,要反弹,只需反转x速度)。要查看它是否在交叉点,与您检查球是否在屏幕内的方式相同,您只需匹配球和桨的x和y值。我把桨做得很薄,这样它们基本上只能在实际的桨上弹跳(告诉我你是否想改变这个,我很乐意这么做!)。这是代码,缩进不好,但我修正了这一点,我建议您使用while循环主要是为了可读性,因为它有点长

#import and start up pygame
import pygame
import math
import time

pygame.init()

#define colors
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED =   ( 255, 0, 0)
BLUE =  ( 0, 0, 255)
PURPLE = (255, 0, 255)
YELLOW = (255, 255, 0)
TEAL = ( 0, 255, 255)
BROWN = ( 70, 50, 30)

#create window
size = (800, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Pong")

done = False

#manage time
clock = pygame.time.Clock()


#ball movement
rect_x = 375
rect_y = 250
rect_change_x = 3
rect_change_y = 3

#paddle movement
y_speed_1 = 0
y_speed_2 = 0

#paddle coords so we can track them in real time
x_coord_1 = 70
x_coord_2 = 700
y_coord_1 = 200
y_coord_2 = 200

#score
score_player_1 = 0
score_player_2 = 0


time.sleep(1)
def display_score():
    font = pygame.font.SysFont('System Bold', 35, True, False)
    text1 = font.render(str(score_player_1), True, WHITE)
    text2 = font.render(str(score_player_2), True, WHITE)
    screen.blit(text1, [375, 10])
    screen.blit(text2, [425, 10])

def player_win():
    font = pygame.font.SysFont('System Bold', 40, True, False)
    player_1_win = font.render("Player 1 Wins!", True, WHITE)
    player_2_win = font.render("Player 2 Wins!", True, WHITE)

    if score_player_1 == 7:
        screen.blit(player_1_win, [300, 100])
        pygame.display.flip()
        time.sleep(1)
        pygame.quit()

    if score_player_2 == 7:
        screen.blit(player_2_win, [300, 100])
        pygame.display.flip()
        time.sleep(1)
        pygame.quit()

def draw_paddle_1(x, y, z):
    pygame.draw.rect(screen, WHITE, [x_coord_1, y_coord_1, 3, 100])

def draw_paddle_2(x, y, z):
    pygame.draw.rect(screen, WHITE, [x_coord_2, y_coord_2, 3, 100])
def collidep1(x,y):
    if x<= x_coord_1+3:#it will only bounce off one side
        if y+50<= y_coord_1+150 and y+50>=y_coord_1:
            return True
    return False
def collidep2(x,y):
    if x+50>= x_coord_2:#it will only bounce off one side
        if y+50<= y_coord_2+150 and y+50>=y_coord_2:
            return True
    return False


#fill the screen

#main loop
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        #program that moves the paddles
        if event.type == pygame.KEYDOWN:

        #player one uses the WS keys
            if event.key == pygame.K_w:
                y_speed_1 = -3
            elif event.key == pygame.K_s:
                y_speed_1 = 3

        #player two uses the arrow keys
            elif event.key == pygame.K_DOWN:
                y_speed_2 = 3
            elif event.key == pygame.K_UP:
                y_speed_2 = -3

    #stops the paddle after done pressing key
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_w or event.key == pygame.K_s:
                y_speed_1 = 0

            elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                y_speed_2 = 0


    y_coord_1 += y_speed_1
    y_coord_2 += y_speed_2


#ball bounces off of the top and bottom sides
    if rect_y > 450 or rect_y < 0:
        rect_change_y = rect_change_y * -1

#player 1 score
    elif rect_x > 751: 
        score_player_1 += 1
        rect_x = 375
        rect_y = 250
        rect_change_x = 0
        rect_change_y = 0
        rect_change_x = -4
        rect_change_y = -4
        time.sleep(1)


#player 2 score
    elif rect_x < -1:
        score_player_2 += 1
        rect_x = 375
        rect_y = 250
        rect_change_x = 0
        rect_change_y = 0
        rect_change_x = 4
        rect_change_y = 4
        time.sleep(1)

    #player 1 collision
    if collidep1(rect_x,rect_y):
        rect_change_x = 4
    if collidep2(rect_x,rect_y):
        rect_change_x = -4


    screen.fill(BLACK)
    pygame.draw.rect(screen, WHITE, [rect_x, rect_y, 50, 50])


    #move the starting point of the ball
    rect_x += rect_change_x
    rect_y += rect_change_y

    #paddles
    draw_paddle_1(screen, x_coord_1, y_coord_1)
    draw_paddle_2(screen, x_coord_2, y_coord_2)
    #call programs
    display_score()
    player_win()

    #make the window display the drawings
    pygame.display.flip()



    #limit to 60 frames per second
    clock.tick(60)


#paddles program

#ball

#score
#close window and quit
pygame.quit()
#导入并启动pygame
导入pygame
输入数学
导入时间
pygame.init()
#定义颜色
黑色=(0,0,0)
白色=(255,255,255)
绿色=(0,255,0)
红色=(255,0,0)
蓝色=(0,0255)
紫色=(255,0255)
黄色=(255,255,0)
TEAL=(0,255,255)
布朗=(70,50,30)
#创建窗口
大小=(800500)
screen=pygame.display.set_模式(大小)
pygame.display.set_标题(“Pong”)
完成=错误
#管理时间
clock=pygame.time.clock()
#球的运动
矩形x=375
矩形y=250
rect_change_x=3
rect_change_y=3
#桨叶运动
y_速度_1=0
y_速度_2=0
#划桨coords,这样我们可以实时跟踪它们
x_坐标_1=70
x_坐标2=700
y_coord_1=200
y_坐标2=200
#得分
得分\玩家\ 1=0
得分\玩家\ 2=0
时间。睡眠(1)
def显示_分数():
font=pygame.font.SysFont('System Bold',35,True,False)
text1=font.render(str(score\u player\u 1),真,白色)
text2=font.render(str(score\u player\u 2),真,白色)
screen.blit(text1[375,10])
screen.blit(text2[425,10])
def player_win():
font=pygame.font.SysFont('System Bold