简体   繁体   English

为什么我在尝试检查碰撞时会出现类型错误,我该如何解决?

[英]Why am i getting Type errors when i am trying to check for collision and how can i fix it?

I am a Beginner in Python and i was making a simple 2D game in Pygame.我是 Python 的初学者,我正在 Pygame 中制作一个简单的 2D 游戏。 I am almost done except for the collision checking with the pygame.Rect function.除了与 pygame.Rect function 进行碰撞检查外,我几乎完成了。 I am trying to check for collision with car2 and car under the comment "collision detecting" but i am getting -- TypeError: Invalid rectstyle argument.我正在尝试在“碰撞检测”评论下检查与 car2 和 car 的碰撞,但我得到了 -- TypeError: Invalid rectstyle argument。 I researched for quite a while for this but i just am not able to utilize it in my code.我为此研究了很长时间,但我无法在我的代码中使用它。 I would appreciate if someone helped fix this problem.如果有人帮助解决此问题,我将不胜感激。

import pygame
import random
import math


pygame.init()


black = (0, 0, 0)

screen_width = 600
screen_length = 800
screen = pygame.display.set_mode((screen_length, screen_width))
clock = pygame.time.Clock()
FPS = 60

icon = pygame.image.load('car.png')
pygame.display.set_icon(icon)

#game caption
pygame.display.set_caption("Car game")




#Main car
car = pygame.image.load('car.png')
carX = 370
carY = 470 
carX_change = 0

#second car
car2 = pygame.image.load('car2.png')
car2_rect = car.get_rect()
car2_X = random.randint(200, 600)
car2_Y = random.randint(-50, 0)
car2Y_speed = 13


#road side trees
tree1Img = pygame.image.load('tree1.png')
tree1_X = 70
tree1_Y = 30

tree2Img = pygame.image.load("tree2.png")
tree2_X = 700
tree2_Y = 300


tree3Img = pygame.image.load("tree3.png")
tree3_X = 70
tree3_Y = 400

tree4Img = pygame.image.load("tree4.png")
tree4_X = 700
tree4_Y = 50



#game functions
def car1(x, y):
    screen.blit(car, car_rect, (x, y))

def car_2(x, y):
    global car2_Y
    global car2_X
    screen.blit(car2, car2_rect, (x, y))
    car2_Y += car2Y_speed
    if car2_Y > screen_length:
        car2_X = random.randint(200, 545)
        car2_Y = random.randint(-50, 0)   


def tree1(x, y):
    global tree1_Y
    screen.blit(tree1Img, (x, y))
    tree1_Y += 10
    if tree1_Y >= screen_width:
        tree1_Y = -50


def tree2(x, y):
    global tree2_Y
    screen.blit(tree2Img, (x, y))
    tree2_Y += 10
    if tree2_Y >= screen_width:
        tree2_Y = -50


def tree3(x, y):
    global tree3_Y
    screen.blit(tree2Img, (x, y))
    tree3_Y += 10
    if tree3_Y >= screen_width:
        tree3_Y = -50


def tree4(x, y):
    global tree4_Y
    screen.blit(tree4Img, (x, y))
    tree4_Y += 10
    if tree4_Y >= screen_width:
        tree4_Y = -50

# 1st Road marker's movement
rectangleX = 426
rectangleY = 100
def roadmarker_move():
    global rectangleY
    if running == True:
        rectangleY += 10
    if rectangleY == 610:
        rectangleY = -50


#2nd road marker's movement
rectangle2_X = 426
rectangle2_Y = 410
def roadmarker2_move():
    global rectangle2_Y
    if running == True:
        rectangle2_Y += 10
    if rectangle2_Y == 610:
        rectangle2_Y = -50





#Main game loop
running = True
while running:
    screen.fill((119, 118, 110))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                carX_change = 48
            if event.key == pygame.K_LEFT:
                carX_change = -48

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                carX_change = 0
        carX += carX_change



    #drawing Road lines
    pygame.draw.line(screen, black, (639, 0), (639, 600), 10)
    pygame.draw.line(screen, black, (200, 0), (200, 600), 10)

    
    #drawing the 1st road markers
    rectangle = pygame.draw.rect(screen, black, (rectangleX, rectangleY, 15, 80))
    roadmarker_move()

    #drawing the 2nd road markers
    rectangle = pygame.draw.rect(screen, black, (rectangle2_X, rectangle2_Y, 15, 80))
    roadmarker2_move()

    #setting boundries
    if carX <= 170:
        carX = 170
    elif carX >= 547:
        carX = 547

    #collision detecting
    car_rect = car.get_rect()
    car_rect.topleft = (carX, carY)
    car_rect.move_ip((carX_change, 0))

    car2_rect = car2.get_rect()
    car2_rect.topleft = (car2_X, car2_Y)
    car2_rect.move_ip((car2Y_speed, 0))

    if ( car_rect.colliderect(car2_rect) ):
        print( "car hit tree" )


    car1(carX, carY)
    car_2(car2_X, car2_Y)
    tree1(tree1_X, tree1_Y)
    tree2(tree2_X, tree2_Y)
    tree3(tree3_X, tree3_Y)
    tree4(tree4_X, tree4_Y)
    clock.tick(FPS)
    pygame.display.update()

pygame.quit()

The name of the attribute is topleft rather than top_left .该属性的名称是topleft而不是top_left See pygame.Rect .参见pygame.Rect

Furthermore, move_ip has to be applied to the pygame.Rect object rather than to the Surface:此外, move_ip必须应用于pygame.Rect object 而不是 Surface:

car.move_ip((carX_change, 0))

car_rect.move_ip((carX_change, 0))

Finally car_rect(x, y) makes not any sens at all.最后car_rect(x, y)根本没有任何意义。 It is sufficient to pass a tuple with the position of the car to the method blit in the function car1 :将带有汽车 position 的元组传递给 function car1中的方法blit就足够了:

screen.blit(car, car_rect(x, y))

screen.blit(car, (x, y))

respectively in the function car_2分别在 function car_2

screen.blit(car2, car2_rect, (x, y))

screen.blit(car2, (x, y))

To improve the collision you have to ensure that the collision rectangle reflects the actual position of the cars.为了改善碰撞,您必须确保碰撞矩形反映汽车的实际 position。 You wrongly move the collision rectangles.您错误地移动了碰撞矩形。 Remove this lines of code and do the collision test after the cars have been moved:删除这行代码并在汽车移动后进行碰撞测试:

car1(carX, carY)      # <--- INSERT
car_2(car2_X, car2_Y) # <--- INSERT

car_rect = car.get_rect()
car_rect.topleft = (carX, carY)
#car_rect.move_ip((carX_change, 0)) <--- DELETE

car2_rect = car2.get_rect()
car2_rect.topleft = (car2_X, car2_Y)
#car2_rect.move_ip((car2Y_speed, 0)) <--- DELETE

#car1(carX, carY)       <--- DELETE
#car_2(car2_X, car2_Y)  <--- DELETE

Complete example:完整示例:

import pygame
import random
import math


pygame.init()


black = (0, 0, 0)

screen_width = 600
screen_length = 800
screen = pygame.display.set_mode((screen_length, screen_width))
clock = pygame.time.Clock()
FPS = 60

icon = pygame.image.load('car.png')
pygame.display.set_icon(icon)

#game caption
pygame.display.set_caption("Car game")


#Main car
car = pygame.image.load('car.png')
carX = 370
carY = 470 
carX_change = 0

#second car
car2 = pygame.image.load('car2.png')
car2_rect = car.get_rect()
car2_X = random.randint(200, 600)
car2_Y = random.randint(-50, 0)
car2Y_speed = 13


#road side trees
tree1Img = pygame.image.load('tree1.png')
tree1_X = 70
tree1_Y = 30

tree2Img = pygame.image.load("tree2.png")
tree2_X = 700
tree2_Y = 300


tree3Img = pygame.image.load("tree3.png")
tree3_X = 70
tree3_Y = 400

tree4Img = pygame.image.load("tree4.png")
tree4_X = 700
tree4_Y = 50

#game functions
def car1(x, y):
    screen.blit(car, (x, y))

def car_2(x, y):
    global car2_Y
    global car2_X
    screen.blit(car2, (x, y))
    car2_Y += car2Y_speed
    if car2_Y > screen_length:
        car2_X = random.randint(200, 545)
        car2_Y = random.randint(-50, 0)   


def tree1(x, y):
    global tree1_Y
    screen.blit(tree1Img, (x, y))
    tree1_Y += 10
    if tree1_Y >= screen_width:
        tree1_Y = -50


def tree2(x, y):
    global tree2_Y
    screen.blit(tree2Img, (x, y))
    tree2_Y += 10
    if tree2_Y >= screen_width:
        tree2_Y = -50


def tree3(x, y):
    global tree3_Y
    screen.blit(tree2Img, (x, y))
    tree3_Y += 10
    if tree3_Y >= screen_width:
        tree3_Y = -50


def tree4(x, y):
    global tree4_Y
    screen.blit(tree4Img, (x, y))
    tree4_Y += 10
    if tree4_Y >= screen_width:
        tree4_Y = -50

# 1st Road marker's movement
rectangleX = 426
rectangleY = 100
def roadmarker_move():
    global rectangleY
    if running == True:
        rectangleY += 10
    if rectangleY == 610:
        rectangleY = -50


#2nd road marker's movement
rectangle2_X = 426
rectangle2_Y = 410
def roadmarker2_move():
    global rectangle2_Y
    if running == True:
        rectangle2_Y += 10
    if rectangle2_Y == 610:
        rectangle2_Y = -50

#Main game loop
running = True
while running:
    screen.fill((119, 118, 110))
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RIGHT:
                carX_change = 48
            if event.key == pygame.K_LEFT:
                carX_change = -48

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                carX_change = 0
        carX += carX_change



    #drawing Road lines
    pygame.draw.line(screen, black, (639, 0), (639, 600), 10)
    pygame.draw.line(screen, black, (200, 0), (200, 600), 10)

    
    #drawing the 1st road markers
    rectangle = pygame.draw.rect(screen, black, (rectangleX, rectangleY, 15, 80))
    roadmarker_move()

    #drawing the 2nd road markers
    rectangle = pygame.draw.rect(screen, black, (rectangle2_X, rectangle2_Y, 15, 80))
    roadmarker2_move()

    #setting boundries
    if carX <= 170:
        carX = 170
    elif carX >= 547:
        carX = 547

    car1(carX, carY)
    car_2(car2_X, car2_Y)

    #collision detecting
    car_rect = car.get_rect()
    car_rect.topleft = (carX, carY)
    
    car2_rect = car2.get_rect()
    car2_rect.topleft = (car2_X, car2_Y)
    
    if ( car_rect.colliderect(car2_rect) ):
        print( "car hit tree" )

    tree1(tree1_X, tree1_Y)
    tree2(tree2_X, tree2_Y)
    tree3(tree3_X, tree3_Y)
    tree4(tree4_X, tree4_Y)
    clock.tick(FPS)
    pygame.display.update()

pygame.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在尝试安装FiPy时出现错误? - Why am I getting errors when trying to install FiPy? 为什么会出现这些错误以及如何解决它们? - Why am I getting these errors and how to fix them? instabot 错误,为什么我会收到这些错误以及如何解决? - instabot ERROR, Why am I getting these errors and how to fix please? 为什么我会收到属性错误? 我该如何解决? - Why am I getting an attribute error? How can I fix it? 为什么我在 Python 中收到 ValueError 以及如何修复它? - Why am I getting ValueError in Python and how can I fix it? 使用PYPJLINK时出现断言错误,该如何解决? - I am getting assertion errors when using PYPJLINK , how could I fix them? 为什么我收到 JSON 错误,我该如何解决 - Why am I getting a JSON error and how do I fix it 为什么在尝试读取不存在的s3键时出现不同的错误 - Why am I getting different errors when trying to read s3 key that does not exist 为什么在 Airflow/Google Composer 中尝试使用 DAG.get_dagrun() 时会出现暂时性错误? - Why am I getting transient errors when trying to use DAG.get_dagrun() in Airflow/Google Composer? 为什么我会收到这些错误? AttributeError:无法设置属性 - Why I am getting these errors? AttributeError: can't set attribute
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM