问题
import pygame, random, time
class Game(object):
def main(self, screen):
bg = pygame.image.load('data/bg.png')
house1 = pygame.image.load('data/house1.png')
playerIdle = pygame.image.load('data/playerIdle.png')
playerRight = pygame.image.load('data/playerRight.png')
playerLeft = pygame.image.load('data/playerLeft.png')
playerUp = pygame.image.load('data/playerUp.png')
playerX = 0
playerY = 50
clock = pygame.time.Clock()
spriteList = []
gameIsRunning = False
house1Selected = False
while 1:
clock.tick(30)
mouse = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
if event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
return
#delete all sprites on the game(only able to do this in god mode)
if event.type == pygame.KEYDOWN and event.key == pygame.K_d and gameIsRunning == False:
spriteList = []
#press 6 to select the house1 image to be placed
if event.type == pygame.KEYDOWN and event.key == pygame.K_6 and gameIsRunning == False:
house1Selected = True
#spawning the image"house1" at the position of the mouse by pressing space
if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and gameIsRunning == False and house1Selected == True:
spriteList.append((house1, mouse))
house1XY = mouse
#run mode where you cannot build and you move(where I want collision)
if event.type == pygame.KEYDOWN and event.key == pygame.K_r:
gameIsRunning = True
#god mode where you can build and place the house1 image
if event.type == pygame.KEYDOWN and event.key == pygame.K_g:
gameIsRunning = False
#this is run mode where you can move around and where I want collision
if(gameIsRunning == True):
#player Movements
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
if playerY <= 0:
playerY = 0
if playerY >= 0:
screen.blit(playerUp, (playerX, playerY))
playerY = playerY - 2
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
PLAYERDOWNLIMIT = 700 - playerIdle.get_height()
if(playerY >= PLAYERDOWNLIMIT):
playerY = PLAYERDOWNLIMIT
if(playerY <= PLAYERDOWNLIMIT):
screen.blit(playerIdle, (playerX, playerY))
playerY = playerY + 2
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
if playerX <= 0:
playerX = 0
if playerX >= 0:
screen.blit(playerLeft, (playerX, playerY))
playerX = playerX - 2
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
PLAYERRIGHTLIMIT = 1200 - playerIdle.get_width()
if(playerX >= PLAYERRIGHTLIMIT):
playerX = PLAYERRIGHTLIMIT
if(playerX <= PLAYERRIGHTLIMIT):
screen.blit(playerRight, (playerX, playerY))
playerX = playerX + 2
#collision
house1Rect = (house1XY, 64, 64)
playerRect = (playerX, playerY, 32, 32)
#print(house1Rect)
#print(playerRect)
if playerRect.colliderect(house1Rect):
print("collision")
#this is godmode where you can move around fast n preview where you place images but I don't want collision here
if(gameIsRunning == False):
if event.type == pygame.KEYDOWN and event.key == pygame.K_UP:
if playerY <= 0:
playerY = 0
if playerY >= 0:
screen.blit(playerUp, (playerX, playerY))
playerY = playerY - 8
if event.type == pygame.KEYDOWN and event.key == pygame.K_DOWN:
PLAYERDOWNLIMIT = 700 - playerIdle.get_height()
if(playerY >= PLAYERDOWNLIMIT):
playerY = PLAYERDOWNLIMIT
if(playerY <= PLAYERDOWNLIMIT):
screen.blit(playerIdle, (playerX, playerY))
playerY = playerY + 8
if event.type == pygame.KEYDOWN and event.key == pygame.K_LEFT:
if playerX <= 0:
playerX = 0
if playerX >= 0:
screen.blit(playerLeft, (playerX, playerY))
playerX = playerX - 8
if event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
PLAYERRIGHTLIMIT = 1200 - playerIdle.get_width()
if(playerX >= PLAYERRIGHTLIMIT):
playerX = PLAYERRIGHTLIMIT
if(playerX <= PLAYERRIGHTLIMIT):
screen.blit(playerRight, (playerX, playerY))
playerX = playerX + 8
screen.fill((200, 200, 200))
screen.blit(bg, (0, 0))
#what block are you placing(displays preview)
if(gameIsRunning == False and house1Selected == True):
screen.blit(house1, (mouse))
if(gameIsRunning == True):
playerXSTR = str(playerX)
playerYSTR = str(playerY)
playerXYSTR = ("(" + playerXSTR + ", " + playerYSTR + ")")
font = pygame.font.SysFont("monospace", 15)
playercord = font.render(playerXYSTR, 1, (255,255,255))
screen.blit(playercord, (0, 0))
runMode = font.render("Run Mode(Cannot build)", 1, (255,255,255))
screen.blit(runMode, (0, 20))
if(gameIsRunning == False):
playerXSTR = str(playerX)
playerYSTR = str(playerY)
playerXYSTR = ("(" + playerXSTR + ", " + playerYSTR + ")")
font = pygame.font.SysFont("monospace", 15)
playercord = font.render(playerXYSTR, 1, (255,255,255))
screen.blit(playercord, (0, 0))
godMode = font.render("God Mode(Can build)", 1, (255,255,255))
screen.blit(godMode, (0, 20))
for (img, pos) in spriteList:
screen.blit(img, pos)
screen.blit(playerIdle, (playerX, playerY))
pygame.display.flip()
if __name__ == '__main__':
pygame.init()
infoObject = pygame.display.Info()
#screen = pygame.display.set_mode((infoObject.current_w, infoObject.current_h))
screen = pygame.display.set_mode((1200, 700))
pygame.display.set_caption('Sandbox')
Game().main(screen)
I'm trying to add collision to my game between the player and the house1 image. At line 330 when the user presses space to spawn the image(house1) at the location of the mouse, it sets the variable house1XY. and at line 393 is where I have the collision. I set house1Rect = (house1XY, 64, 64) and playerRect = (playerX, playerY, 32, 32) and then do an if statement if playerRect.colliderect(house1Rect): print("collision") but I get an error:
Traceback (most recent call last):
File "C:\Users\Dusty\Desktop\Dropbox\OR Software\Projects\Sandbox Project\game.py", line 512, in <module>
Game().main(screen)
File "C:\Users\Dusty\Desktop\Dropbox\OR Software\Projects\Sandbox Project\game.py", line 397, in main
if playerRect.colliderect(house1Rect):
AttributeError: 'tuple' object has no attribute 'colliderect'
I'm really starting to get confused with this collision stuff so if someone could tell me how I could fix this it would be great!
回答1:
playerRect is a tuple, as defined here:
playerRect = (playerX, playerY, 32, 32)
You need to make it a pygame.rect, using this code:
playerRect = pygame.Rect(playerX, playerY, 32, 32)
Then you can use the colliderect method
Edit: do the same to house1Rect:
house1Rect = pygame.Rect(house1XY[0], house1XY[1], 64, 64)
来源:https://stackoverflow.com/questions/21635596/pygame-collision-detection-error