我一直在尝试使用python 3.4.3为类创建Pong。我要做的最后一件事是在球和桨之间产生碰撞。另外,我现在才刚刚了解Sprites,但是距离重启还差得远。
这是我的代码:
#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()
也请告知我是否还要进行其他修改。 仅供参考,这需要在明天之前完成,因此我真的不能在此上花费更多时间。
答案 0 :(得分:1)
嘿,欢迎来到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()