English 中文(简体)
p-python[复制]碰撞探测
原标题:collision detection in pygame-python [duplicate]
import pygame
import sys
from time import sleep
class Doodlejump:
    def __init__(self):
        self.go = True
        self.x = 275
        self.y = 800
        self.doodle = Doodle(self.x, self.y, "doodle_resized.png")
        self.gravity = 7.5
        self.jump_counter = 0
        self.speed = 2
        self.jump_timer = 0

    def run(self):
        pygame.init()
        pygame.display.set_caption("Doodlejump")
        clock = pygame.time.Clock()
        screen = pygame.display.set_mode((600, 800))

        while self.go:
            if self.x < 0:
                self.x = 0
            elif self.x >= 600 - self.doodle.width:
                self.x = 600 - self.doodle.width

            if self.y < 0:
                self.y = 0
            elif self.y >= 800 - self.doodle.height:
                self.y = 800 - self.doodle.height

            if self.y >= 800 - self.doodle.height:
                self.jump_counter = 0

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            pressed = pygame.key.get_pressed()
            if pressed[pygame.K_LEFT]:
                self.x -= self.speed
            if pressed[pygame.K_RIGHT]:
                self.x += self.speed

            screen.fill((255, 255, 255))

            is_jumping = False
            if pressed[pygame.K_SPACE] and not is_jumping and self.jump_counter < 1 and self.jump_timer <= 0:
                is_jumping = True
                self.y -= self.speed
                self.jump_counter += 1
                self.jump_timer = 100


            if self.jump_timer > 0:
                self.jump_timer -= 1

            jump_speed_factor = 250
            if is_jumping:
                self.y -= jump_speed_factor
                jump_speed_factor *= 0.3
            else:
                self.y += self.gravity
                self.jump_timer -= 15

            platform = pygame.draw.rect(screen, (0, 0, 0), (100, 700, 150, 30))
            if platform.colliderect(self.doodle):
                print("Kollision!!!")


            screen.blit(self.doodle.image, (self.x, self.y))

            pygame.display.update()
            clock.tick(60)

if __name__ == "__main__":
    game = Doodlejump()
    game.run()

我策划了一场与佩伊加梅交的Doodlejump游戏,但不幸的是,我的碰撞探测确实奏效。 当我试图把我的杜戈放在平台上时,它只是跳跃走。 原因何在? 我已经把发言从法典的顶点移到底部,但这种说法并没有改变。 采用col(col)法的“一”方法是错误的? 如果发生碰撞,就根本不进入路障。

问题回答

这里的工作守则。 这印刷了发生碰撞时的“Kollide”。 我相信你会重述你的法典。 贵组织可维持。

import pygame
import sys
from time import sleep

class Doodle():
    def __init__(self, x, y, image_path):
        self.image = pygame.image.load(image_path)
        self.rect = pygame.Rect((x, y), self.image.get_size())
        self.width = self.rect.width
        self.height = self.rect.height
    
    def update(self, x, y):
        self.rect.x = x
        self.rect.y = y


class Doodlejump:
    def __init__(self):
        self.go = True
        self.x = 175
        self.y = 100
        self.doodle = Doodle(self.x, self.y, "doodle_resized.png")
        self.gravity = 7.5
        self.jump_counter = 0
        self.speed = 2
        self.jump_timer = 0

    def run(self):
        pygame.init()
        pygame.display.set_caption("Doodlejump")
        clock = pygame.time.Clock()
        screen = pygame.display.set_mode((600, 700))

        while self.go:
            if self.x < 0:
                self.x = 0
            elif self.x >= 600 - self.doodle.width:
                self.x = 600 - self.doodle.width

            if self.y < 0:
                self.y = 0
            elif self.y >= 800 - self.doodle.height:
                self.y = 800 - self.doodle.height

            if self.y >= 800 - self.doodle.height:
                self.jump_counter = 0

            self.doodle.update(self.x, self.y)

            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()

            pressed = pygame.key.get_pressed()
            if pressed[pygame.K_LEFT]:
                self.x -= self.speed
            if pressed[pygame.K_RIGHT]:
                self.x += self.speed

            screen.fill((255, 255, 255))

            is_jumping = False
            if pressed[pygame.K_SPACE] and not is_jumping and self.jump_counter < 1 and self.jump_timer <= 0:
                is_jumping = True
                self.y -= self.speed
                self.jump_counter += 1
                self.jump_timer = 100

            if self.jump_timer > 0:
                self.jump_timer -= 1

            jump_speed_factor = 250
            if is_jumping:
                self.y -= jump_speed_factor
                jump_speed_factor *= 0.3
            else:
                self.y += self.gravity
                self.jump_timer -= 15

            platform = pygame.draw.rect(screen, (0, 0, 0), (100, 500, 150, 30))
            if platform.colliderect(self.doodle.rect):
                print("Kollision!!!")
                self.doodle.update

            screen.blit(self.doodle.image, (self.x, self.y))

            pygame.display.update()
            clock.tick(60)


if __name__ == "__main__":
    game = Doodlejump()
    game.run()





相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签