【发布时间】:2025-12-22 21:35:11
【问题描述】:
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
houseInWorld = 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
houseInWorld = True
#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
if(house1InWorld == True):
house1Rect = pygame.Rect(house1XY[0], house1XY[1], 64, 64)
if playerRect.colliderect(house1Rect) and playerX <= house1XY[0]:
playerX = playerX - 2
if playerRect.colliderect(house1Rect) and playerX >= house1XY[0]:
playerX = playerX + 2
if playerRect.colliderect(house1Rect) and playerY <= house1XY[1]:
playerY = playerY - 2
if playerRect.colliderect(house1Rect) and playerY >= house1XY[1]:
playerY = playerY + 2
#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()
screen = pygame.display.set_mode((1200, 700))
pygame.display.set_caption('Sandbox')
Game().main(screen)
在我的游戏中,您从“上帝模式”开始,您可以在其中放置对象。但是,当您完成放置对象并进入“运行模式”时,游戏是实时的,并且会出现碰撞和其他物理效果。
所以我现在在游戏中遇到的碰撞仅在部分时间有效。
当您按下空格键时,您会在鼠标位置生成“house1”图像,它会设置 house1 的 XY 坐标,并设置 house1InWorld = True(这会影响碰撞)。
由于 house1inWorld 属性,碰撞在生成 house1 图像时生效。因此,如果您按 r(进入运行模式),则碰撞适用于一个 house1 实例。但是当您生成 2 个 house1 实例(在不同位置)时,碰撞仅适用于最近放置的 house1。
我应该如何更改我的代码,以便无论你生成多少 house1 都可以进行碰撞?
【问题讨论】:
-
你的缩进错了吗?你只有在 While True 循环中的事件循环。