【发布时间】:2019-09-27 18:57:56
【问题描述】:
我正在尝试制作 pin pon 游戏,当然,一个矩形在屏幕右侧,另一个在屏幕左侧。当球击中第二个矩形时,它需要碰撞,但在更新方法中有一个 hits1 变量,它应该与东西碰撞,但在同一行
hits1 = pg.sprite.spritecollide(self.player,self.balls,False)
pygame 给我这个错误:
AttributeError: 'pygame.math.Vector2' 对象没有属性 '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()
【问题讨论】: