矩形不会碰撞彼此,即使碰撞也是如此

时间:2017-06-13 21:11:43

标签: python pygame

#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,两个玩家都会与它发生碰撞,但它似乎根本不会改变伤害输出。

我花了好几个小时试图找出问题所在。皮卡吸引但不会碰撞。

请帮助,我的计算机科学老师试图帮助我,但告诉我他不知道并告诉我自己弄清楚。我还不知道如何使用课程或精灵,因为我很快就没有时间学习这个项目了。

1 个答案:

答案 0 :(得分: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)