Python 3.x 试图在球离开屏幕后让球回到中间

Python 3.x 试图在球离开屏幕后让球回到中间,python-3.x,pygame,pong,Python 3.x,Pygame,Pong,我试着重新制作一个乒乓球游戏只是为了好玩 这是我现在的全部代码 import pygame, random, math pygame.init() #colours:------------------------------------------------------------- R = random.randrange(1,255) B = random.randrange(1,255) G = random.randrange(1,255) WHITE = (255, 255,

我试着重新制作一个乒乓球游戏只是为了好玩

这是我现在的全部代码

import pygame, random, math

pygame.init()

#colours:-------------------------------------------------------------
R = random.randrange(1,255)
B = random.randrange(1,255)
G = random.randrange(1,255)
WHITE = (255, 255, 255)
GREEN = (39, 133, 20)
YELLOW = (252, 252, 25)
BLACK = (0, 0, 0)
BLUE = (30, 100, 225)
RED = (255,0,0)
RANDOM_COLOR = (R, B, G)

#Surface:-------------------------------------------------------------
width = 700
height = 600
size = (width, height)
screen = pygame.display.set_mode(size)
screen_rect = screen.get_rect()

pygame.display.set_caption("Pong Remake")

background = pygame.image.load("background.png").convert()
background = pygame.transform.scale(background, (width, height))

logo = pygame.image.load("logo.png").convert()
logo.set_colorkey((BLACK))
credits = pygame.image.load("credits.png")
credits.set_colorkey((BLACK))

#variables:-----------------------------------------------------------
clock = pygame.time.Clock()
done = False
text = pygame.font.Font(None,25)
display_instructions = True
instruction_page = 1
start_font = pygame.font.Font("C:\Windows\Fonts\BAUHS93.TTF", 35)
instruction_font = pygame.font.Font(None, 17)
win_lose_font = pygame.font.Font("C:\Windows\Fonts\BAUHS93.TTF",50)
score = pygame.font.Font(None, 100)
bounce = pygame.mixer.Sound("bounce.wav")
playerOne_score = 0
playerTwo_score = 0

playerOne = ""
playerTwo = ""
x = 350
y = 300

ball_rect = pygame.Rect(x,y,10,10)
paddleOne_rect = pygame.Rect(10, 250, 20, 60)
paddleTwo_rect = pygame.Rect(670, 250, 20, 60)

x_speed = random.randrange(5, 10)
y_speed = random.randrange(5,10)

def draw_background(screen, pic, x,y):
    screen.blit(pic, (x,y))


#main loop


    #INPUT v ---------------------------------------------------------
    #Start Page
while not done and display_instructions:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        if event.type == pygame.MOUSEBUTTONDOWN:
            instruction_page += 1
            if instruction_page == 2:
                display_instructions = False
        #Shows the start of the page
    if instruction_page == 1:
            draw_background(screen, logo, 100,-150)  
            draw_background(screen, credits, 100,50)
            instruction_text = instruction_font.render("How to Play. The objective to this game is to score the ball on the other side before the opponent can.", False, WHITE)
            instruction_text_three = instruction_font.render("First Player to get 10 points wins, Have Fun and Good Luck!", False, WHITE)
            instruction_text_two = instruction_font.render("For Player One, use the a and the z keys to move up and down, For Player Two, use the k and m keys.", False, WHITE)
            continue_text= start_font.render("Click to Play...",True, WHITE)


            screen.blit(continue_text, [200, 400])
            screen.blit(instruction_text, [0,500])
            screen.blit(instruction_text_three, [0,532])
            screen.blit(instruction_text_two,[0,516])

    if instruction_page == 2:
        display_instructions = False


    clock.tick(60)

    pygame.display.flip()

while not done:
    click = False

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.MOUSEBUTTONDOWN:
            click = True




    #INPUT ^ =========================================================



    #PROCESS v -------------------------------------------------------
    str(playerOne_score)
    str(playerTwo_score)
    scoreOne = text.render("Player One:" + str(playerOne_score), False, WHITE)
    scoreTwo = text.render("Player Two:" + str(playerTwo_score), False, WHITE)
    #moves paddles with keys on keyboar
    key = pygame.key.get_pressed()
    if key[pygame.K_a]: paddleOne_rect.move_ip(0, -10)
    if key[pygame.K_z]: paddleOne_rect.move_ip(0, 10)
    if key[pygame.K_k]: paddleTwo_rect.move_ip(0, -10)
    if key[pygame.K_m]: paddleTwo_rect.move_ip(0, 10)

    #makes sure paddles stay on screen
    paddleOne_rect.clamp_ip(screen_rect)
    paddleTwo_rect.clamp_ip(screen_rect)    
    ball_rect.move_ip(x_speed, y_speed)


    if ball_rect.y  + ball_rect.height> screen_rect.height or ball_rect.y < 0:
        y_speed = y_speed * -1
        bounce.play()

    if ball_rect.collidelist([paddleOne_rect, paddleTwo_rect]) > -1:
        x_speed = -x_speed
        R = random.randrange(1,255)
        B = random.randrange(1,255)
        G = random.randrange(1,255)
        bounce.play()

    if ball_rect.x >= 700:        
        x_speed * -1
        playerOne_score += 1
        pygame.display.flip

    if ball_rect.x <= 0:
        x_speed * -1
        playerTwo_score += 1







    #PROCESS ^ =======================================================
    #DRAWING GOES BELOW HERE v ------------------------------------
    draw_background(screen, background, 0,0)
    screen.blit(scoreOne, (0,0))
    screen.blit(scoreTwo, (500,0))
    pygame.draw.ellipse(screen, WHITE,ball_rect )
    pygame.draw.rect(screen,RANDOM_COLOR, paddleOne_rect)
    pygame.draw.rect(screen,RANDOM_COLOR, paddleTwo_rect)    
    pygame.draw.line(screen, WHITE, (350,0),(350,700), 1)

    #DRAWING GOES ABOVE HERE ^ ------------------------------------





    pygame.display.flip()
    clock.tick(60)


pygame.quit()
导入pygame、random、math
pygame.init()
#颜色:-------------------------------------------------------------
R=random.randrange(1255)
B=随机随机随机范围(1255)
G=随机随机随机范围(1255)
白色=(255,255,255)
绿色=(39133,20)
黄色=(25225225)
黑色=(0,0,0)
蓝色=(30100225)
红色=(255,0,0)
随机颜色=(R,B,G)
#表面:-------------------------------------------------------------
宽度=700
高度=600
大小=(宽度、高度)
screen=pygame.display.set_模式(大小)
screen\u rect=screen.get\u rect()
pygame.display.set_标题(“Pong翻拍”)
background=pygame.image.load(“background.png”).convert()
背景=pygame.transform.scale(背景,(宽度,高度))
logo=pygame.image.load(“logo.png”).convert()
logo.set_颜色键((黑色))
credits=pygame.image.load(“credits.png”)
学分。设置颜色键((黑色))
#变量:-----------------------------------------------------------
clock=pygame.time.clock()
完成=错误
text=pygame.font.font(无,25)
显示指令=真
说明页面=1
start\u font=pygame.font.font(“C:\Windows\Fonts\BAUHS93.TTF”,35)
说明\u font=pygame.font.font(无,17)
win\u lose\u font=pygame.font.font(“C:\Windows\Fonts\BAUHS93.TTF”,50)
score=pygame.font.font(无,100)
bounce=pygame.mixer.Sound(“bounce.wav”)
运动员得分=0
球员得分=0
playerOne=“”
playerTwo=“”
x=350
y=300
ball_rect=pygame.rect(x,y,10,10)
palleone_rect=pygame.rect(10250,20,60)
palletwo_rect=pygame.rect(6702502060)
x_速度=随机。随机范围(5,10)
y_速度=随机。随机范围(5,10)
def绘图背景(屏幕、图片、x、y):
屏幕光点(图(x,y))
#主回路
#输入v---------------------------------------------------------
#起始页
未完成时,显示\u说明:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
如果event.type==pygame.MOUSEBUTTONDOWN:
说明\u第+=1页
如果指令_页面==2:
显示\u指令=错误
#显示页面的开头
如果指令_页面==1:
绘制背景(屏幕,徽标,100,-150)
绘制背景(屏幕,学分,100,50)
指令\文本=指令\字体.render(“如何比赛。这场比赛的目标是在对方得分之前在对方得分。”,False,WHITE)
指令\u文本\u三=指令\u字体.render(“第一个获得10分的玩家获胜,祝你玩得开心,好运!”,False,WHITE)
指令_text_two=指令_font.render(“对于玩家一,使用a和z键上下移动,对于玩家二,使用k和m键。”,False,白色)
continue\u text=start\u font.render(“单击播放…”),True,白色)
blit(continue_text[200400])
屏幕.blit(说明文字[0500])
blit(指令文本三,[0532])
blit(指令文本二,[0516])
如果指令_页面==2:
显示\u指令=错误
时钟滴答(60)
pygame.display.flip()
虽然没有这样做:
单击=False
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
完成=正确
elif event.type==pygame.MOUSEBUTTONDOWN:
单击=真
#输入^=========================================================
#过程五-------------------------------------------------------
str(运动员评分)
str(球员双分)
scoreOne=text.render(“玩家一:”+str(玩家分数),假,白)
scoreTwo=text.render(“玩家二:”+str(玩家二分),假,白)
#用键盘上的键移动桨
key=pygame.key.get_pressed()
如果键[pygame.K_a]:直接移动ip(0,-10)
如果键[pygame.K_z]:直接移动ip(0,10)
如果键[pygame.K\u K]:两个右移动ip(0,-10)
if键[pygame.K\m]:两条直线移动ip(0,10)
#确保挡板保持在屏幕上
桨叶一个直接夹紧(屏蔽)
桨叶两个垂直夹钳(屏幕垂直)
球直线移动ip(x速度,y速度)
如果ball_-rect.y+ball_-rect.height>screen_-rect.height或ball_-rect.y<0:
y_速度=y_速度*-1
弹跳
如果ball_u u rect.CollizeList([PapperOne_rect,PapperTwo_rect])>-1:
x_速度=-x_速度
R=random.randrange(1255)
B=随机随机随机范围(1255)
G=随机随机随机范围(1255)
弹跳
如果ball_rect.x>=700:
x_速度*-1
球员得分+=1
pygame.display.flip

如果ball_rect.x这里有很多代码,因此这并不遵循您使用的特定变量,但我希望这会有所帮助

1) 找到你屏幕的宽度

2) 取你用来知道在哪里画球的x和y坐标

3) 做一个if语句,本质上说 (伪代码)

如果x>1000
分数1+=1
x=500
如果x<0
分数2+=1
x=500
``
我希望这能让你走上正轨,我建议你看看pygame文档。
干杯

这解决了我的问题,非常感谢!!
if x > 1000
  score1 += 1
  x = 500
if x < 0
  score2 += 1
  x = 500
``
I hope this can set you on the right track, and I suggest checking out the pygame docs.
Cheers!