2014-07-27 55 views
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 

回答

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。在中間參數和中間比較之間添加空格。縮進更好。導入這個。 etc ...