0
因此,我正在製作一個遊戲,玩家必須點擊移動矩形(網球)才能獲得積分。到目前爲止,我已經創建了網球和其他一些球,我已經讓它們隨機圍繞着屏幕移動並從邊緣反彈。我也製作了一個叫鼠標的目標。每當玩家點擊網球(與目標)時,我希望遊戲增加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