Python Pygame冲突,未使用Pygame.sprite
所以我做了一个小游戏,你可以在一个小镇上漫步,我已经完成了我大部分的行走动画和一些音乐等等。现在我需要添加碰撞。我在想要发生碰撞的区域上画了一个矩形,但我不知道从这里该怎么办。(右心房朝向代码底部)我希望我的玩家在击中我画的右心房时停止行走Python Pygame冲突,未使用Pygame.sprite,python,collision,Python,Collision,所以我做了一个小游戏,你可以在一个小镇上漫步,我已经完成了我大部分的行走动画和一些音乐等等。现在我需要添加碰撞。我在想要发生碰撞的区域上画了一个矩形,但我不知道从这里该怎么办。(右心房朝向代码底部)我希望我的玩家在击中我画的右心房时停止行走 import pygame import pyganim import time import pygame.gfxdraw pygame.init() white = (255,255,255) black = (0,0,0) red = (255,0,0
import pygame
import pyganim
import time
import pygame.gfxdraw
pygame.init()
white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
y = 200
x = 300
x_change = 0
y_change = 0
pygame.mixer.music.load("gamesong.ogg")
pygame.mixer.music.play(-1)
global playerAnim
#imports pygame and initializes the module
pygameDisplay = pygame.display.set_mode((958,910))
bg = pygame.image.load("map.png")
#creates a screen
pygame.display.set_caption("pokemon-ish")
gameExit = False
clock = pygame.time.Clock()
playerAnimLeft = pyganim.PygAnimation([("left walk 0.png",.3)])
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_change = -10
playerAnimLeft = pyganim.PygAnimation([("left walk 1.png",0.3),("left walk 2.png", 0.3),("left walk 3.png",0.3),("left walk 4.png",0.3)])
if event.key == pygame.K_RIGHT:
x_change = +10
playerAnimLeft = pyganim.PygAnimation([("walk right 1.png",0.3),("walk right 2.png", 0.3),("walk right 3.png",0.3),("walk right 4.png",0.3)])
if event.key == pygame.K_UP:
y_change = -10
playerAnimLeft = pyganim.PygAnimation([("walk right 1.png",0.3),("walk right 2.png", 0.3),("walk right 3.png",0.3),("walk right 4.png",0.3)])
playerAnimLeft = pyganim.PygAnimation([("walk forward 1.png", 0.3)])
if event.key == pygame.K_DOWN:
y_change = +10
playerAnimLeft = pyganim.PygAnimation([("walk down 1.bmp", 0.3),("walk down 2.bmp", 0.3)])
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
x_change = 0
playerAnimLeft = pyganim.PygAnimation([("left walk 2.png",.3)])
if event.key == pygame.K_RIGHT:
x_change = 0
playerAnimLeft = pyganim.PygAnimation([("walk right 2.png",.3)])
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_change = 0
pygame.draw.rect(pygameDisplay,(0),[192,145,187,182])
pygameDisplay.blit(bg,(0,0))
playerAnimLeft.blit(pygameDisplay, (x,y))
playerAnimLeft.play()
pygame.display.update()
clock.tick(30)
x += x_change
y += y_change
pygame.display.update()
clock.tick(30)
pygame.quit()
quit()
您应该将对象转换为
pygame.Rect
对象,然后使用一些方法,如pygame.Rect.colliderect
捕获您的对象是否与另一个对象发生碰撞。由于您刚刚编写了全部代码,所以不可能编写答案,您应该观看一些关于碰撞的pygame教程。检查
这只是一个如何使用colliderect
的简单示例
用箭头键移动大矩形,如果你触摸小矩形,它将移动到屏幕左侧你应该显示一些你尝试过的代码,我们不是代码工作者。这里有很多代码,你希望我们能帮助你,请尽量减少它。我将为你添加一个例子,等一下please@B.Morris检查我的答案
import pygame
pygame.init()
gameDisplay=pygame.display.set_mode((800,600))
x=0
y=0
rect2= pygame.Rect (500,30,50,20) #we should make a rect object first
clock=pygame.time.Clock()
running=True
while running:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key==pygame.K_LEFT:
x -= 10
if event.key==pygame.K_RIGHT:
x += 10
gameDisplay.fill((0,0,0))
rect1= pygame.Rect (x,y,100,200) #big rect object
pygame.draw.rect(gameDisplay, (255,255,255) , rect1) #drawing them
pygame.draw.rect(gameDisplay, (255,255,255) , rect2)
if rect1.colliderect(rect2): #if first one which is
rect2= pygame.Rect (50,500,50,20) #we are controlling it with arrow keys
#collide with other one
pygame.display.update() #change the coordinates of that rectangle
clock.tick()