2017-06-13 52 views
0
#Main problem should be around this function 
def pick_ups(health_regenerate,damage_buff,speed_increase): 
    print("pickups") 
    #Checks if player collides with health_regenerate, then makes health = 
    100 
    if player.colliderect(health_regenerate): 
     print('collidehealth') 
     player_health = 100 
    if player_2.colliderect(health_regenerate): 
     print('collidehealth2') 
     player_2_health = 100 



    print(damage_buff) 
    print(player) 
    #if player collides with the damage pickup, bullets should domoredamage 
    if player.colliderect(damage_buff): 
     print('collide1') 
     for i in range(len(bullets_2)-1,-1,-1): 
     if bullets[i].colliderect(player_2): 
      damage = 50 
      player_2_health -= damage  

    if player_2.colliderect(damage_buff): 
     print('collide2') 
     for i in range(len(bullets_2)-1,-1,-1): 
      if bullets_2[i].colliderect(player): 
       damage = 50 
       player_health -= damage 
    #If player collides with speed_increase pickup, player speed increases 
    if player.colliderect(speed_increase): 
     print("speed") 
     player_speed += 5 
    if player_2.colliderect(speed_increase): 
     print("speed") 
     player_speed += 5 


firing = False 
firing_2 = False 
while True: 
    screen.fill(background) 
    health_bars(player_health,player_2_health) 
    pygame.draw.rect(screen,player_colour,player) 
    pygame.draw.rect(screen,player_2_colour,player_2) 
    pick_ups(health_regenerate,damage_buff,speed_increase) 
    pygame.time.delay(40) 
    events = pygame.event.get() 





for event in events: 
    player_direction = [0,0] 
    player_direction_2 = [0,0] 
    player_speed = 2 
    player_speed_2 = 2 
    if event.type == pygame.QUIT: 
     exit() 


for obstacle in obstacles: 
    pygame.draw.rect(screen, (255, 255, 255), obstacle) 

for wall in walls: 
    pygame.draw.rect(screen, (255, 255, 255), wall, 1) 

#PLAYER ONE: 
#Movement of player 1 
keys_pressed = pygame.key.get_pressed() 
#Upward 
if keys_pressed[pygame.K_UP] == 1: 
    player_direction[1] = -player_speed 
    last_direction_y = -1 

#Downward 
if keys_pressed[pygame.K_DOWN] == 1: 
    player_direction[1] = player_speed 
    last_direction_y = 1 
#Left 
if keys_pressed[pygame.K_LEFT] == 1: 
    player_direction[0] = -player_speed 
    last_direction_x = -1 
#Right 
if keys_pressed[pygame.K_RIGHT] == 1: 
    player_direction[0] = player_speed 
    last_direction_x = 1 
#shoot player 1 
if keys_pressed[pygame.K_PERIOD]: 
    if not firing: 
     shoot_timer = shoot_timer_max 
     firing = True 
     bullet_direction_x += [last_direction_x] 
     bullet_direction_y += [last_direction_y] 
     bullets += [pygame.Rect(player.x, player.y, 3, 5)] 

if shoot_timer > 0: 
    shoot_timer -= 1 

    if shoot_timer == 0: 
     firing = False 

#Shooting while not moving player 1 
if player_direction[0] != 0: 
    last_direction_x = player_direction[0] 

else: 
    if last_direction_y != 0: 
     last_direction_x = 0 
if player_direction[1] != 0: 
    last_direction_y = player_direction[1] 
else: 
    if last_direction_x != 0: 
     last_direction_y = 0 
for i in range(len(bullets)): 
    pygame.draw.rect(screen, (0, 0, 222), bullets[i]) 
    bullets[i] = bullets[i].move(bullet_direction_x[i]*bullet_speed, 
    bullet_direction_y[i]*bullet_speed) 

#Bullet Collision 1 
for bullet_hit in map_walls: 
    for i in range(len(bullets)): 
     if bullets[i].collidelist(bullet_hit) >= 0: 
      bullets.pop(i) 
      bullet_direction_x.pop(i) 
      bullet_direction_y.pop(i) 
      break 
#collision player 1 
player = player.move(player_direction[0], 0) 
for wall_list in map_walls: 
    if player.collidelist(wall_list) >= 0: 
      player = player.move(-player_direction[0], 0) 
      break 
player = player.move(0, player_direction[1]) 
for wall_list in map_walls: 
    if player.collidelist(wall_list) >= 0: 
      player = player.move(0, -player_direction[1]) 
      break 

pygame.draw.rect(screen, red,(640,360,10,10)) 
pygame.draw.rect(screen, green,(600,300,10,10)) 
pygame.draw.rect(screen, yellow,(600,250,10,10)) 
pygame.display.update() 

問題

我似乎無法獲得遊戲來檢測玩家碰撞health_regenerate的RECT對象的速度增加。而對於damage_buff,兩名玩家都會與它發生碰撞,但似乎並沒有改變傷害輸出。矩形不海誓山盟碰撞即使Colliderect

我花了數小時試圖找出問題。拾音器畫但不會碰撞。

請幫助,我的計算機科學老師試圖幫助我,但告訴我他不知道並告訴我自己弄清楚。我不知道如何使用類或精靈,因爲我很快就沒有時間學習這個項目。

+0

請編輯您的問題並提供[最小,完整且可驗證的示例](https://stackoverflow.com/help/mcve),否則我們無法看到錯誤來自哪裏。 – skrx

+0

我在評論的頂部說了一個名爲pick_ups的函數可能是最可能的罪犯。這是我能做的最少的事情。我的完整代碼有大約400行。 –

+0

好吧,如果你發佈了這樣一個不完整的代碼示例,你實在無法等待幫助。如果缺少重要信息,調試此類代碼片段可能非常困難,甚至不可能。如果你無法提供一個最小和完整的例子,你可以嘗試發佈完整的程序,但如果他們必須閱讀和分析這麼多的代碼,並且你可能會被低估,這裏的人不太可能幫助你。 – skrx

回答

0

這就是大量的代碼......林不知道你的問題是什麼......但在這裏,這裏是一個例子

import pygame 


player = pygame.Rect(10,20,50,50) 
pickup = pygame.Rect(100,20,50,50) # not colliding 
print player.colliderect(pickup) # 0 

#move player 
player.left = 85 
player.top = 15 
# now playwe collides 
print player.colliderect(pickup) # 1 

是一個更完整的例子

import random 

import pygame 
import sys 


player = pygame.Rect(10,20,50,50) 
pickup = pygame.Rect(100,20,50,50) # not colliding 
print player.colliderect(pickup) # 0 

clock = pygame.time.Clock() 
pygame.init() 
screen = pygame.display.set_mode((424, 320)) 

collide = False 
direction=1 
while 1: 
    screen.fill((0,0,0)) 
    player.left += 1*direction 
    player.top = random.randint(10,30) 
    collide = player.colliderect(pickup) 
    pygame.draw.rect(screen, (255, 0, 0) if not collide else (0, 0, 255), player) 
    pygame.draw.rect(screen, (0, 250, 0) if not collide else (200, 200, 255), pickup) 
    if player.left > 300 and direction > 0: 
     direction = -1 
     player.left = 299 
    elif player.left < 1 and direction < 0: 
     direction = 2 
     player.left = 1 

    pygame.display.update() 
    clock.tick(30) 
+0

從我所知道的情況來看,我的碰撞代碼與上面列出的完全相同。所以也許這不是功能上的問題。 –