当一个rect与另一个rect碰撞并点击鼠标左键时,如何增加点数?

时间:2014-07-27 10:55:31

标签: python pygame

所以我正在制作一个游戏,玩家必须点击移动的矩形(网球)来获得积分。到目前为止,我已经创造了网球和其他一些球,我让它们在屏幕上随机移动并从边缘反弹。我还制作了一个叫做目标的矩形,用鼠标移动。我希望每次玩家点击网球(带目标)时,游戏都会增加10点,如果他们点击屏幕上的其他地方,就会扣除生命。任何帮助将不胜感激! 谢谢:))

import pygame
import sys

from random import *
from pygame.locals import *


#game        
pygame.init()
running=True
screen_width=800
screen_height=600
screen=pygame.display.set_mode([screen_width,screen_height])
background_colour = (0,0,150)
screen.fill(background_colour)

clock=pygame.time.Clock()
game_sound='pumpup.wav'
pygame.mixer.init()
points=0
lives=3

#balls
x= randint(20,40)
y= randint(20,40)
ball=pygame.image.load ('ball_t.png')
ball_movement=[x,y]#amount ball moves each time (x,y)
ball_location=[100,100]#x,y co-ordinates for start position
ball_rect=ball.get_rect()#gets rectangle for ball

x2= randint(5,15)
y2= randint(5,15)

ball2_movement=[x2,y2]#amount ball moves each time (x,y)
ball2_location=[10,10]#x,y co-ordinates for start position
ball2=pygame.image.load ('ball_tt.png')
ball2_rect=ball2.get_rect()#gets rectangle for ball

x3= randint(10,40)
y3= randint(10,30)

ball3_movement=[x3,y3]#amount ball moves each time (x,y)
ball3_location=[10,100]#x,y co-ordinates for start position
ball3=pygame.image.load ('ball_b.png')
ball3_rect=ball3.get_rect()#gets rectangle for ball

x4= randint(10,30)
y4= randint(10,30)

ball4_movement=[x4,y4]#amount ball moves each time (x,y)
ball4_location=[200,100]#x,y co-ordinates for start position
ball4=pygame.image.load ('ball_bs.png')
ball4_rect=ball4.get_rect()#gets rectangle for ball

#target
target_location=[200,100]#x,y co-ordinates for start position
target=pygame.image.load ('Target.png')
target_rect=target.get_rect()#gets rectangle for ball
target_rect.center=target_location

#score
font=pygame.font.Font(None, 50)#default font
score="Score: {}".format(points)
score_text=font.render(score,1,(0,200,250))
text_pos=[10,10] #position on screen

#lives
font=pygame.font.Font(None, 50)#default font
livest="Lives: {}".format(lives)
lives_text=font.render(livest,1,(0,200,250))
textl_pos=[650,10]#position on screen




#show initial screen
screen.blit(ball, ball_rect)#draws ball on its rectangle
screen.blit(ball2, ball2_rect)
screen.blit(ball3, ball3_rect)
screen.blit(ball4, ball4_rect)
screen.blit(target, target_rect)
screen.blit(score_text, text_pos)
screen.blit(lives_text, textl_pos)

pygame.display.flip() #displays screen


while running: #event loop
    clock.tick(50) #won't run at more than 60 frames per second
    screen.fill([0,0,150])#clears screen

    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            running=False
        elif event.type==pygame.MOUSEMOTION:
        target_rect.centerx=event.pos[0] #moves target with mouse
        target_rect.centery=event.pos[1]
        if event.type == pygame.MOUSEBUTTONDOWN and target_rect.colliderect(ball2_rect):
       points=100 




    if ball_rect.left<0 or ball_rect.right>screen_width: #check whether ball is off screen (x)
        ball_movement[0]=-ball_movement[0]#change direction on x axis
    if ball_rect.top<0 or ball_rect.bottom>screen_height: #check whether ball is off screen (y)
        ball_movement[1]=-ball_movement[1] #change direction on y axis

    if ball2_rect.left<0 or ball2_rect.right>screen_width: #check whether ball is off screen (x)
        ball2_movement[0]=-ball2_movement[0]#change direction on x axis
    if ball2_rect.top<0 or ball2_rect.bottom>screen_height: #check whether ball is off screen (y)
        ball2_movement[1]=-ball2_movement[1] #change direction on y axis

    if ball3_rect.left<0 or ball3_rect.right>screen_width: #check whether ball is off screen (x)
        ball3_movement[0]=-ball3_movement[0]
    if ball3_rect.top<0 or ball3_rect.bottom>screen_height: #check whether ball is off screen (y)
        ball3_movement[1]=-ball3_movement[1] #change direction on y axis

    if ball4_rect.left<0 or ball4_rect.right>screen_width: #check whether ball is off screen (x)
        ball4_movement[0]=-ball4_movement[0]
    if ball4_rect.top<0 or ball4_rect.bottom>screen_height: #check whether ball is off screen (y)
        ball4_movement[1]=-ball4_movement[1] #change direction on y axis



    if ball_rect.top > screen_height and lives >0:
        lives=lives-1
        livest="Lives: {}".format(lives)
        lives_text=font.render(livest,1,(0,0,0))
        pygame.time.delay(500)#pause
        ball_rect.topleft=[10,0] #reinstate ball
    if lives ==0:
        pygame.display.flip()
        pygame.time.delay(2500)#pause
        running=False





    ball_rect=ball_rect.move(ball_movement)#move ball
    ball2_rect=ball2_rect.move(ball2_movement)
    ball3_rect=ball3_rect.move(ball3_movement)
    ball4_rect=ball4_rect.move(ball4_movement)
    screen.blit(ball, ball_rect)#redraws ball
    screen.blit(ball2, ball2_rect)
    screen.blit(ball3, ball3_rect)
    screen.blit(ball4, ball4_rect)
    screen.blit(target, target_rect)
    screen.blit(score_text, text_pos)
    screen.blit(lives_text, textl_pos)


    pygame.display.flip() #displays screen

pygame.quit() #quits game

1 个答案:

答案 0 :(得分:0)

我建议不要创造一个球4次。相反,创建一个球类并制作4个球对象。试试这个

class ball:
    def __init__(self, x, y, image, location):
        self.x = x
        self.y = y
        self.img = image
        self.movement = [x, y]
        self.rect = self.img.get_rect()
        self.rect.topleft = location

然后,你可以摆脱所有这些:

x= randint(20,40)
y= randint(20,40)
ball=pygame.image.load ('ball_t.png')
ball_movement=[x,y]#amount ball moves each time (x,y)
ball_location=[100,100]#x,y co-ordinates for start position
ball_rect=ball.get_rect()#gets rectangle for ball

x2= randint(5,15)... etc.

并将其替换为:

balls = []
balls.append(ball(randint(20, 40), randint(20, 40), 'ball_t.png', [100, 100])
balls.append(ball(randint(5, 15), randint(5, 15), 'ball_tt.png', [10, 10])
balls.append(ball(randint(10, 40), randint(10, 30), 'ball_b.png', [10, 100])
balls.append(ball(randint(10, 30), randint(10, 30), 'ball_bs.png', [200, 100])

然后,取这一部分:

    if ball_rect.left<0 or ball_rect.right>screen_width: #check whether ball is off screen (x)
        ball_movement[0]=-ball_movement[0]#change direction on x axis
    if ball_rect.top<0 or ball_rect.bottom>screen_height: #check whether ball is off screen (y)
        ball_movement[1]=-ball_movement[1] #change direction on y axis

    if ball2... etc.

并用球的方法替换它。

class ball:
    def __init__():
        blah blah blah init

    def is_touching_wall(self):
        if self.rect.left < 0 or self.rect.right > screen_width:
            self.movement[0] -= self.movement[0]
        if self.rect.top < 0 or self.rect.bottom > screen_height: 
            self.movement[1] -= self.movement[1] 

然后在主函数中:

for ball in balls:
    ball.is_touching_wall()

然后,再向球类添加一个方法:

def update(self):
    self.rect = self.rect.move(self.movement)
    screen.blit(self.img, self.rect)

并替换此部分:

ball_rect=ball_rect.move(ball_movement)#move ball
ball2_rect=ball2_rect.move(ball2_movement)
ball3_rect=ball3_rect.move(ball3_movement)
ball4_rect=ball4_rect.move(ball4_movement)
screen.blit(ball, ball_rect)#redraws ball
screen.blit(ball2, ball2_rect)
screen.blit(ball3, ball3_rect)
screen.blit(ball4, ball4_rect)

用这个:

for ball in balls:
    ball.update()

最后,还有两项建议。

  1. 不要在SE上发帖说“这是我的代码。修复它。”您从未指定尝试解决问题的方法,代码中的问题所在,或者甚至是您的问题。此外,尝试仅发布将复制问题的代码的最小部分。修复您的代码,并在您的问题中添加更多信息,然后人们将更愿意帮助您实现目标。

  2. 注意格式化。阅读PEP-8。在参数之间和比较之间添加空格。缩进更好。导入这个。等...