Im trying to make pin pon game, one rectangle is right side of the screen and other one is left side of the screen of course. When the ball hits the second rectangle it needs to be collide but in the update method there is a hits1 variable which supposed to be collide the stuffs but in same line
hits1 = pg.sprite.spritecollide(self.player,self.balls,False)
pygame gives me this error:
AttributeError: 'pygame.math.Vector2' object has no attribute 'colliderect'
import pygame as pg
import random
from settings import *
from sprites import *
from os import path
class Game:
def __init__(self):
# initialize game window, etc
pg.init()
pg.mixer.init()
self.screen = pg.display.set_mode((WIDTH, HEIGHT))
pg.display.set_caption(TITLE)
self.clock = pg.time.Clock()
self.running = True
def new(self):
# start a new game
self.all_sprites = pg.sprite.Group()
self.balls = pg.sprite.Group()
self.player = Player(self)
self.player2 = Player2(self)
self.ball = Ball(self.player.pos.x + 10, self.player.pos.y + 20,self)
self.all_sprites.add(self.player,self.player2)
self.all_sprites.add(self.ball)
self.balls.add(self.ball)
self.run()
def run(self):
# Game Loop
self.playing = True
while self.playing:
self.clock.tick(FPS)
self.events()
self.update()
self.draw()
def update(self):
# Game Loop - Update
self.all_sprites.update()
hits1 = pg.sprite.spritecollide(self.player,self.balls,False)
if hits1:
self.player2.throw_back()
def events(self):
# Game Loop - events
for event in pg.event.get():
# check for closing window
if event.type == pg.QUIT:
if self.playing:
self.playing = False
self.running = False
def draw(self):
# Game Loop - draw
self.screen.fill(BLACK)
self.all_sprites.draw(self.screen)
# *after* drawing everything, flip the display
pg.display.flip()
def show_start_screen(self):
# game splash/start screen
pass
def show_go_screen(self):
# game over/continue
pass
g = Game()
g.show_start_screen()
while g.running:
g.new()
g.show_go_screen()
pg.quit()
You didn't show all relevant code but my educated guess is somewhere you have a Sprite class (either Player and/or Ball) where you assing a Vector2 instance to the rect attribute instead of a Rect instance.
I don't know how the code actually looks like but instead of something like this:
self.rect = some_vector
alter the existing Rect like this instead:
self.rect.topleft = some_vector
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With