1
是否有某些原因導致我的作品在播放功能中檢測到碰撞?Collide_Rect未檢測到
當我打印出碎片的位置時,他們說它們位於正確的位置,但由於某些原因,碰撞不能繼續進行。
import pygame
import os
import random
pygame.mixer.pre_init(44100, -16, 1, 512)
pygame.init()
pygame.display.init()
font = pygame.font.SysFont("times", 20)
black = (0,0,0)
class Wall(pygame.sprite.Sprite):
def __init__(self, color, x, y, width, height):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((width, height))
self.image.fill(pygame.color.Color(color))
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
class Player(pygame.sprite.Sprite):
def __init__(self, image):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("coin.jpg")
self.image = pygame.transform.scale(self.image, (20,20))
self.rect = self.image.get_rect()
###############################################################################
#intializeing class
class PygameGame(object):
def init(self):
self.mode = 'home'
self.prevMode = 'home'
self.homeFunctions = {'keyPressed':self.homeKeyPressed,
'keyReleased':self.homeKeyReleased,
'mousePressed':self.homeMousePressed,
'mousePosition':self.homeMousePosition,
'redrawAll':self.homeRedrawAll,
'timerFired':self.homeTimerFired
}
self.pausedFunctions = {'keyPressed':self.pausedKeyPressed,
'keyReleased':self.pausedKeyReleased,
'mousePressed':self.pausedMousePressed,
'mousePosition':self.pausedMousePosition,
'redrawAll':self.pausedRedrawAll,
'timerFired':self.pausedTimerFired
}
self.playFunctions = {'keyPressed':self.playKeyPressed,
'keyReleased':self.playKeyReleased,
'mousePressed':self.playMousePressed,
'mousePosition':self.playMousePosition,
'mouseMotion': self.playMouseMotion,
'redrawAll':self.playRedrawAll,
'timerFired':self.playTimerFired
}
self.instructionFunctions = {'keyPressed':self.instructKeyPressed,
'keyReleased':self.instructKeyReleased,
'mousePressed':self.instructMousePressed,
'mousePosition':self.instructMousePosition,
'redrawAll':self.instructRedrawAll,
'timerFired':self.instructTimerFired
}
self.textSize = 20
self.players = pygame.sprite.Group()
self.walls = pygame.sprite.Group()
self.allSprites = pygame.sprite.Group()
self.player = Player("coin.jpg")
self.wallOne = Wall('black', self.width//4, self.height//4, 10, 20)
self.walls.add(self.wallOne)
self.players.add(self.player)
self.allSprites.add(self.wallOne, self.player)
self.collide = pygame.sprite.collide_rect(self.wallOne, self.player)
##############################################################################
## home
def homeMousePressed(self, x, y):
self.mode = 'play'
def homeMousePosition(self, x, y):
pass
def homeKeyPressed(self, keyCode):
pass
def homeKeyReleased(self, keyCode):
pass
def homeTimerFired(self, dt):
pass
def homeRedrawAll(self, screen):
#title
text = "Omega"
title = font.render(text, False, (0,0,0))
centered = title.get_rect(center=(self.width//2, self.height//2))
screen.blit(title, (centered))
#how to play
text = "click to play"
title = font.render(text, False, (255,0,0))
centered = title.get_rect(center=(self.width//2, self.height//2+2*self.textSize))
screen.blit(title, (centered))
###############################################################################
#play
def playMousePressed(self, x, y):
pass
def playMouseMotion(self, x, y):
pass
def playMousePosition(self, x, y):
pass
def playKeyPressed(self, keyCode):
print(self.collide)
print("playerY= ", self.player.rect.y)
print("playerX= ", self.player.rect.x)
print("WallX,Y= ", self.width//4, self.height//4)
print("********************************")
if self.collide:
print("made it")
else:
if keyCode == pygame.K_s:
self.player.rect.y += 5
if keyCode == pygame.K_w:
self.player.rect.y -= 5
if keyCode == pygame.K_a:
self.player.rect.x -= 5
if keyCode == pygame.K_d:
self.player.rect.x += 5
def playKeyReleased(self, keyCode):
pass
def playTimerFired(self, dt):
pass
def playRedrawAll(self, screen):
self.allSprites.draw(screen)
self.allSprites.update()
##############################################################################
#paused
def pausedMousePressed(self, x, y):
pass
def pausedMousePosition(self, x, y):
pass
def pausedKeyPressed(self, keyCode):
pass
def pausedKeyReleased(self, keyCode):
pass
def pausedTimerFired(self, dt):
pass
def pausedRedrawAll(self, screen):
pass
###############################################################################
#Instructions
def instructMousePressed(self, x, y):
pass
def instructMousePosition(self, x, y):
pass
def instructKeyPressed(self, keyCode):
pass
def instructKeyReleased(self, keyCode):
pass
def instructTimerFired(self, dt):
pass
def instructRedrawAll(self, screen):
pass
################################################################################
#big stuff below
def isKeyPressed(self, key):
''' return whether a specific key is being held '''
return self._keys.get(key, False)
def __init__(self, width=900, height=500, fps=100, title="112 Pygame Game"):
self.width = 400
self.height = 400
self.fps = fps
self.title = title
self.bgColor = (255, 255, 255)
pygame.init()
def doLoop(self,func,time,screen):
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
func['mousePressed'](*(event.pos))
elif (event.type == pygame.MOUSEMOTION and
event.buttons == (0, 0, 0)):
func['mousePosition'](*(event.pos))
elif event.type == pygame.KEYDOWN:
self._keys[event.key] = True
func['keyPressed'](event.key)
elif event.type == pygame.KEYUP:
self._keys[event.key] = False
func['keyReleased'](event.key)
elif event.type == pygame.QUIT:
self.playing = False
func['timerFired'](time)
func['redrawAll'](screen)
##################################################################
def run(self):
clock = pygame.time.Clock()
screen = pygame.display.set_mode((self.width, self.height))
# set the title of the window
pygame.display.set_caption(self.title)
# stores all the keys currently being held down
self._keys = dict()
# call game-specific initialization
self.init()
self.playing = True
while self.playing:
screen.fill(self.bgColor)
time = clock.tick(self.fps)
# the 'name' functions are dictionaries that hold the key, mouse and draw func for a mode
if self.mode == 'home':
self.doLoop(self.homeFunctions,time,screen)
elif self.mode == 'paused':
self.doLoop(self.pausedFunctions,time,screen)
elif self.mode == 'play':
self.doLoop(self.playFunctions,time,screen)
elif self.mode == 'instructions':
self.doLoop(self.instructionFunctions,time,screen)
pygame.display.update()
pygame.quit()
def main():
game = PygameGame()
game.run()
if __name__ == '__main__':
main()