【问题标题】:Collision randomly detecting and killing program [duplicate]碰撞随机检测和杀死程序[重复]
【发布时间】:2021-12-06 18:09:24
【问题描述】:

我已经完成了我的翻拍小鸟的编码,现在我已经开始修改我的代码并尝试消除错误。但是我的碰撞似乎有问题。我已经对其进行了编码,以便当检测到玩家和障碍物之间发生碰撞时,它会终止程序。但是我现在面临的问题是程序何时运行并且玩家在两个障碍物之间甚至没有关闭程序会自行杀死。但这并不总是发生。只有50%的时间。这很奇怪。你能看看我的碰撞并向我指出我做错了什么吗?这是我第一次尝试 pygame 和项目,所以我不是最好的。

#Imports
import sys, pygame
from pygame import mixer
from pygame.locals import *
import time
import random
from random import randint
import time

#Built in pygame functions + Stuff
pygame.init()
pygame.mixer.init()
pygame.display.set_caption('Floppy Disk') 
FRAMERATE = 60
clock = pygame.time.Clock()

#Screen Stuff
Screen = pygame.display.set_mode((500,750))
background_image = pygame.image.load("circuit.jpeg").convert()

#Sound Track
##playlist = list()
##playlist.append("Midnight City.mp3")
##playlist.append("Midnight City.mp3")
##playlist.append("Midnight City.mp3")
##
##pygame.mixer.music.load(playlist.pop())
##pygame.mixer.music.queue(playlist.pop())
##pygame.mixer.music.set_endevent(pygame.USEREVENT)
##pygame.mixer.music.play()



#Floppy Disk
disk = pygame.image.load("floppy1.jpg")
disk_x = 50
disk_y = 300
disk_y_change = 0
score = 0
oldscore = [0]
Score_font = pygame.font.Font('freesansbold.ttf', 32)

def display_disk(x, y):
    Screen.blit(disk, (x,y))

Obstacle_Width = 70
Obstacle_Height = random.randint(150, 450)
Obstacle_colour = (0,200,10)
Obstacle_x_change = -4
Obstacle_x = 500


def display_obstacle(height):
    pygame.draw.rect(Screen, Obstacle_colour, (Obstacle_x, 0, Obstacle_Width, height))
    bottom_y = height+200
    bottom_height = 635-bottom_y
    pygame.draw.rect(Screen, Obstacle_colour, pygame.Rect(Obstacle_x, bottom_y, Obstacle_Width, bottom_height))


def Collision (Obstacle_x, Obstacle_Height, disk_y, bottom_height):
    if Obstacle_x >= 70 and Obstacle_x <= (40 + 64):
        if disk_y <= (Obstacle_Height -30) or disk_y >= (bottom_height - 34):
            return True
    return False

def Score(score):
    display = Score_font.render(f"Score: {score}", True, (181, 26, 18, 0.86))
    display1 = Score_font.render(f"OldScore: {oldscore[0]}", True, (181, 26, 18, 0.86))
    Screen.blit(display, (10,10))
    Screen.blit(display1, (10,35))

#Shutdown of code
shuton = True

while shuton:
    Screen.fill((0,0,0))
    Screen.blit(background_image, (0,0))
    clock.tick(60)
    print(disk_y)

    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            shuton = False
            sys.exit()
            pygame.quit()
##
##        if event.type == pygame.USEREVENT:
##            if len(playlist) > 0:
##                pygame.mixer.music.queue(playlist.pop())
        
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                disk_y_change = -8

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_SPACE:
                disk_y_change = 3

    disk_y += disk_y_change

    if disk_y <= 0:
        disk_y = 0
    if disk_y >= 571:
        bird_y = 571
    
    Obstacle_x += Obstacle_x_change
    if Obstacle_x <= -10:
        Obstacle_x = 500
        Obstacle_Height = random.randint(200, 400)
        score += 1
    display_obstacle(Obstacle_Height)

    # if disk.rect.colliderect(obstacle)

    collision = Collision(Obstacle_x, Obstacle_Height, disk_y, Obstacle_Height + 150)

    if collision:

        oldscore.append(score)
        print(f"Score was: {score}")
        pygame.quit()
        sys.exit()

    display_disk(disk_x, disk_y)
    Score(score)

    pygame.display.update()

pygame.quit()

【问题讨论】:

  • 只使用pygame.Rect 和它的方法.colliderect,现在很难指出这个问题(部分是因为你没有提供minimal reproducible example)而且你有太多的任意整数浮动
  • 我强烈建议学习如何使用 GUI 调试器(如 PyCharm 中的免费调试器)来调试您自己的代码。您可以在任何行上放置断点,例如发生冲突的代码行,然后检查该点处所有变量的值,并跟踪代码到达该点的路径。如果您不知道如何调试自己的代码,那么您将处于极大的劣势。
  • @Matiiss 是否有文档可以链接我获取 pygame.rect 和 .colliderect 方法
  • @KGGamingFluff 你不只是谷歌“pygame rect”吗?两者的文档都是最佳结果。请至少尝试付出一些努力,否则人们将不太可能帮助您:pygame.org/docs/ref/rect.html
  • @RandomDavis 我查看了我只是不理解的 pygame 文档。我想看看他有没有他推荐的。

标签: python pygame collision


【解决方案1】:

这不是您问题的答案,但它应该可以帮助您了解 pygame.Rect 对象以及如何使用 pygame.colliderect 函数。

import pygame
from pygame import Rect
from pygame import Vector2

pygame.init()
PDR = Rect(0, 0, 1024, 640)
PDS = pygame.display.set_mode(PDR.size)

# define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (128, 0, 0)

# create a pygame rect (size: 100x100) object in the center of the primary display surface
R1 = Rect(Vector2(PDR.center) - (50, 50), (100, 100))

exitDemo = False
while not exitDemo:
    events = pygame.event.get()
    for event in events:
        if event.type == pygame.QUIT:
            exitDemo = True

    mp = Vector2(pygame.mouse.get_pos()) # get the mouse location 
    
    # create pygame rect object (size: 100x100) at the mouse location
    R2 = Rect(mp - (50, 50), (100, 100))
    
    # check for collision. If true both squares will turn red
    if R1.colliderect(R2):
        color = RED
    else:
        color = WHITE

    # clear the display
    PDS.fill(BLACK)
    pygame.draw.rect(PDS, color, R1, 1) # draw a square using the R1 rect (center po)
    pygame.draw.rect(PDS, color, R2, 1) # draw a square using the R2 rect (mouse pos)

    # update the primary display surface
    pygame.display.update()

pygame.quit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多