gpt4 book ai didi

python - 我无法让碰撞在 pygame 中工作

转载 作者:行者123 更新时间:2023-12-01 02:31:50 24 4
gpt4 key购买 nike

import pygame, time, random

pygame.init()

display_width = 800
display_height = 600

white = (255,255,255)
green = (0,255,0)
black = (0,0,0)

gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

boy = pygame.image.load("pig.png")
fence = pygame.image.load("fence.png")

def message_display(text):
largeText = pygame.font.Font("freesansbold.ttf",115)
TextSurf, TextRect = text_objects(text, largeText)
TextRect.center = (display_width/2),(display_height/2)
gameDisplay.blit(TextSurf, TextRect)
pygame.display.update()
time.sleep(2)
game_loop()
def text_objects(text, font):
textSurface = font.render(text, True, black)
return textSurface, textSurface.get_rect()
def crash():
message_display("Game Over")
def fence1(fencex, fencey, fencew, fenceh, color):
gameDisplay.blit(fence, (fencex, fencey, fencew, fenceh))
def collision():
pig_image = pygame.image.load("pig.png")
pig_rect = pig_image.get_rect()
fence_image = pygame.image.load("fence.png")
fence_rect = fence_image.get_rect()
if pig_rect.colliderect(fence_rect):
crash()
def game_loop():
fence_startx = 900
fence_starty = gameDisplay.get_height() * 0.8
fence_speed = 15
fence_width = 50
fence_height = 50

x = gameDisplay.get_width() * 0.1
y = gameDisplay.get_height() * 0.8
y_change = 0
on_ground = True
gravity = .9

gameExit = False

while not gameExit:

for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if on_ground:
y_change = -20
on_ground = False

y_change += gravity
y += y_change
if y >= 500:
y = 500
y_change = 0
on_ground = True

gameDisplay.fill(green)
gameDisplay.blit(boy, (x, y))
fence1(fence_startx, fence_starty, fence_width, fence_height, white)
fence_startx -= fence_speed
randomx = random.randint(1,3)

if fence_startx <= -50:
if randomx == 1:
fence_startx = 1000
if randomx == 2:
fence_startx = 800
if randomx == 3:
fence_startx = 1500

collision()

pygame.display.flip()

clock.tick(60)

game_loop()
pygame.quit()
quit()

出于某种原因,当我运行此代码时,它向我发送垃圾邮件“游戏结束”。有人可以帮我解决这个问题吗?我在这段代码中使用了碰撞函数。使用 pygame.rect 代码是在 pygame 中进行碰撞的最佳方法吗?除了我在游戏中的碰撞之外,一切正常。我将非常感谢一些帮助。谢谢。

最佳答案

collision 函数中,您创建两个矩形,但将它们的坐标保留在默认位置 (0, 0),以便矩形重叠。您应该在主 game_loop 函数中为玩家和栅栏添加矩形,设置它们的 xy (或 topleft) 属性到所需的坐标,然后在 while 循环的每次迭代中更新它们。要检查矩形是否发生碰撞,请将它们传递给碰撞函数并执行以下操作:

def collision(boy_rect, fence_rect):
if boy_rect.colliderect(fence_rect):
# I'm just printing it to shorten the code example.
print('crash')

这是一个最小的完整示例:

import random
import pygame


pygame.init()

green = (0,255,0)

gameDisplay = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

boy = pygame.Surface((40, 60))
boy.fill((0, 40, 100))
fence = pygame.Surface((50, 50))
fence.fill((100, 60, 30))


def collision(boy_rect, fence_rect):
if boy_rect.colliderect(fence_rect):
# I'm just printing it to shorten the code example.
print('crash')


def game_loop():
fence_speed = 15
# Create a fence and a boy rect and set their x,y or topleft coordinates.
fence_rect = fence.get_rect()
fence_rect.x = 900
fence_rect.y = gameDisplay.get_height() * 0.8

x = gameDisplay.get_width() * 0.1
y = gameDisplay.get_height() * 0.8
boy_rect = boy.get_rect(topleft=(x, y))

y_change = 0
on_ground = True
gravity = .9

gameExit = False

while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
if on_ground:
y_change = -20
on_ground = False

y_change += gravity
y += y_change
if y >= 500:
y = 500
y_change = 0
on_ground = True
boy_rect.y = y # Update the rect position.

gameDisplay.fill(green)
gameDisplay.blit(boy, boy_rect)
gameDisplay.blit(fence, fence_rect)
# Update the position of the fence rect.
fence_rect.x -= fence_speed

if fence_rect.x <= -50:
# You can use random.choice to pick one of these values.
fence_rect.x = random.choice((800, 1000, 1500))
# Pass the two rects and check if they collide.
collision(boy_rect, fence_rect)

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

game_loop()
pygame.quit()

关于python - 我无法让碰撞在 pygame 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46739457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com