import random
import pygame
def main():
pygame.init()
pygame.display.set_mode((500, 400))
pygame.display.set_caption('Pong')
w_surface = pygame.display.get_surface()
game = Game(w_surface)
game.play()
pygame.quit()
class Ball:
def __init__(self,color,center,radius,velocity,surface):
self.color = color
self.center = center
self.radius = radius
self.velocity = velocity
self.surface = surface
def draw(self):
pygame.draw.circle(self.surface,self.color,self.center,self.radius)
def move(self):
self.center[0] = self.center[0] + (self.velocity[0])*2
self.center[1] = self.center[1] + (self.velocity[1])
size = self.surface.get_size()
for index in range(0,2):
self.center[index] = self.center[index] + self.velocity[index]
if self.center[index] < self.radius:
self.velocity[index] = -self.velocity[index]
if self.center[index]+ self.radius > size[index]:
self.velocity[index] = -self.velocity[index]
class Game:
def __init__(self,surface):
self.close_clicked = False
self.continue_game = True
self.game_clock = pygame.time.Clock()
self.frames_per_second = 60
self.surface = surface
self.bg_color = pygame.Color('black')
self.score1 = 0
self.dot1 = Ball(pygame.Color('white'),[250,200],5,[2,1],self.surface)
self.right_paddle = [380,180],[10,50]
self.left_paddle = [100,180],[10,50]
def play(self):
while self.close_clicked == False:
self.handle_events()
self.draw()
if self.continue_game == True:
self.update()
self.decide_continue()
self.game_clock.tick(self.frames_per_second)
def handle_events(self):
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
self.close_clicked = True
def draw(self):
self.surface.fill(pygame.Color('black'))
self.dot1.draw()
pygame.draw.rect(self.surface,pygame.Color('white'),self.right_paddle)
pygame.draw.rect(self.surface,pygame.Color('white'),self.left_paddle)
self.draw_score1()
#I keep getting an error saying that tuple object has no attribute to collide point?
if self.left_paddle.collidepoint(self.center) and self.velocity[0] < 0:
self.velocity[0] = - self.velocity[0]
if self.right_paddle.collidepoint(self.center) and self.velocity[0] > 0:
self.velocity[0] = - self.velocity[0]
pygame.display.update()
def draw_score1(self):
fg_color = pygame.Color('white')
font = pygame.font.SysFont('',70)
text_string = '' + str(self.score1)
text_string2 = '' + str(self.score1)
text_box = font.render(text_string,True,fg_color,self.bg_color)
text_box2 = font.render(text_string2,True,fg_color,self.bg_color)
location = (5,0)
location2 = (470,0)
self.surface.blit(text_box,location)
self.surface.blit(text_box2,location2)
def update(self):
self.dot1.move()
main()
我用Google搜索了很多东西,但是collidepoint
函数上没有很多东西。在pygame中,它说它需要一个我完全理解的点(x,y),但是,我需要使用球的中心作为它的x,y,因此一旦游戏检测到球拍已经达到该点,然后它将使球偏斜。我在碰撞点出现一些错误的地方写了一条评论!
.collidepoint()
是.collidepoint()
的方法。当然,pygame.Rect
和pygame.Rect
必须分别是self.right_paddle
对象。例如:
self.left_paddle
现在您可以在对象上使用pygame.Rect
。例如:
class Game:
def __init__(self,surface):
# [...]
self.right_paddle = pygame.Rect(380,180,10,50)
self.left_paddle = pygame.Rect(100,180,10,50)