制作游戏,如果你想称之为,那还没有真正的目标。 我的问题在于Player.checkCollision。
我的游戏运行但是我的播放器现在就运行到位了? 我不确定这个问题。 同样在menu.handle显示我的游戏菜单后,它不接受第二个ESC键退出游戏,但我可以按Enter键继续游戏。
也可能是一些整体冷凝的提示。
import pygame, pyganim, random, sys, time
from time import sleep
from pygame.locals import *
pygame.init()
clock = pygame.time.Clock()
loaded = False
"""MainRun class """
class MainRun(object):
def __init__(self):
self.Main()
self.loaded = False
def Main(self):
paused = False
loaded = False
game = GameDisplay()
player = Player()
menu = Menu()
while True:
while paused == True:
menu.draw()
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_RETURN:
paused = False
while paused == False:
for event in pygame.event.get():
player.handle(event)
if event.type == KEYDOWN:
if event.key == K_RETURN:
loaded = True
if event.key == K_ESCAPE:
menu.handle(event)
GameDisplay.update(self,GameScene)
paused = True
clock.tick(0)
if loaded == False:
game.load()
GameDisplay.update(self,GameScene)
clock.tick(20)
if loaded == True:
player.checkCollision(game.rect)
print(player.rect,game.rect)
game.draw()
player.update()
GameDisplay.update(self,GameScene)
clock.tick(20)
"""GameScene class """
class GameScene(object):
screen = pygame.display.set_mode((800,600))
"""GameDisplay class """
class GameDisplay(GameScene):
def __init__(self):
self.intscreen = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\Intro.png').convert()
self.bg = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\World.png').convert()
self.cabin = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\cabin.png').convert()
self.rect = self.cabin.get_rect()
self.rect.x = 210
self.rect.y = 280
def load(self):
#draw Load Screen
self.screen.fill((0,0,0))
self.screen.blit(self.intscreen, (0,0))
def draw(self):
#draws game world
self.screen.fill((0,0,0))
self.screen.blit(self.bg, (0,0))
self.screen.blit(self.cabin, ((self.rect.x,self.rect.y)))
def update(self,GameScene):
pygame.display.update()
"""Menu class """
class Menu(GameScene):
def __init__(self):
self.menu = self.menu = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\Menu.png')
def draw(self):
GameScene.screen.blit(self.menu,(0,0))
pygame.display.update()
def handle(self,event):
for event in pygame.event.get():
print (event)
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == K_RETURN:
paused = False
"""Player class """
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.UP = 'up'
self.DOWN = 'down'
self.LEFT = 'left'
self.RIGHT = 'right'
self.front_standing = pygame.image.load('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_front.gif').convert()
self.back_standing = pygame.image.load('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_back.gif').convert()
self.left_standing = pygame.image.load('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_left.gif').convert()
self.right_standing = pygame.transform.flip(self.left_standing, True, False)
self.running = self.moveUp = self.moveDown = self.moveLeft = self.moveRight = False
self.quitdiag = pygame.image.load('C:\\Users\\PAx\\Desktop\\stuff\\QuitDiag.png')
self.direction = self.DOWN # player starts off facing down (front)
# load the "standing" sprites (these are single images, not animations)
self.playerWidth, self.playerHeight = self.front_standing.get_size()
# creating the PygAnimation objects for walking/running in all directions
self.animTypes = 'back_run back_walk front_run front_walk left_run left_walk'.split()
self.animObjs = {}
for self.animType in self.animTypes:
self.imagesAndDurations = [('C:\\Users\PAx\\Desktop\\stuff\\gameimages\\crono_%s.%s.gif' % (self.animType, str(num).rjust(3, '0')), 1) for num in range(6)]
self.animObjs[self.animType] = pyganim.PygAnimation(self.imagesAndDurations)
# create the right-facing sprites by copying and flipping the left-facing sprites
self.animObjs['right_walk'] = self.animObjs['left_walk'].getCopy()
self.animObjs['right_walk'].flip(True, False)
self.animObjs['right_walk'].makeTransformsPermanent()
self.animObjs['right_run'] = self.animObjs['left_run'].getCopy()
self.animObjs['right_run'].flip(True, False)
self.animObjs['right_run'].makeTransformsPermanent()
# have the animation objects managed by a conductor.
# With the conductor, we can call play() and stop() on all the animtion
# objects at the same time, so that way they'll always be in sync with each
# other.
self.moveConductor = pyganim.PygConductor(self.animObjs)
self.WALKRATE = 10
self.RUNRATE = 18
self.WINDOWWIDTH = 640
self.WINDOWHEIGHT = 480
self.rect = self.front_standing.get_rect()
self.rect.x = 0
self.rect.y = 0
def checkCollision(self, sprite2):
col = self.rect.colliderect(sprite2)
if col == True:
print('you collided')
self.RUNRATE,self.WALKRATE = 0,0
def update(self):
if self.moveUp or self.moveDown or self.moveLeft or self.moveRight:
# draw the correct walking/running sprite from the animation object
self.moveConductor.play() # calling play() while the animation objects are already playing is okay; in that case play() is a no-op
if self.running:
if self.direction == self.UP:
self.animObjs['back_run'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
elif self.direction == self.DOWN:
self.animObjs['front_run'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
elif self.direction == self.LEFT:
self.animObjs['left_run'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
elif self.direction == self.RIGHT:
self.animObjs['right_run'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
else:
# walking
if self.direction == self.UP:
self.animObjs['back_walk'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
elif self.direction == self.DOWN:
self.animObjs['front_walk'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
elif self.direction == self.LEFT:
self.animObjs['left_walk'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
elif self.direction == self.RIGHT:
self.animObjs['right_walk'].blit(GameScene.screen, ((self.rect.x,self.rect.y)))
# actually move the position of the player
if self.running:
rate = self.RUNRATE
else:
rate = self.WALKRATE
if self.moveUp:
self.rect.y -= rate
if self.moveDown:
self.rect.y += rate
if self.moveLeft:
self.rect.x -= rate
if self.moveRight:
self.rect.x += rate
else:
# standing still
self.moveConductor.stop() # calling stop() while the animation objects are already stopped is okay; in that case stop() is a no-op
if self.direction == self.UP:
GameScene.screen.blit(self.back_standing, ((self.rect.x,self.rect.y)))
elif self.direction == self.DOWN:
GameScene.screen.blit(self.front_standing, ((self.rect.x,self.rect.y)))
elif self.direction == self.LEFT:
GameScene.screen.blit(self.left_standing, ((self.rect.x,self.rect.y)))
elif self.direction == self.RIGHT:
GameScene.screen.blit(self.right_standing, ((self.rect.x,self.rect.y)))
def handle(self, event):
if event.type == KEYDOWN:
if event.key in (K_LSHIFT, K_RSHIFT):
# player has started running
self.running = True
if event.key == K_UP:
self.moveUp = True
self.moveDown = False
if not self.moveLeft and not self.moveRight:
# only change the direction to up if the player wasn't moving left/right
self.direction = self.UP
elif event.key == K_DOWN:
self.moveDown = True
self.moveUp = False
if not self.moveLeft and not self.moveRight:
self.direction = self.DOWN
elif event.key == K_LEFT:
self.moveLeft = True
self.moveRight = False
if not self.moveUp and not self.moveDown:
self.direction = self.LEFT
elif event.key == K_RIGHT:
self.moveRight = True
self.moveLeft = False
if not self.moveUp and not self.moveDown:
self.direction = self.RIGHT
elif event.type == KEYUP:
if event.key in (K_LSHIFT, K_RSHIFT):
# player has stopped running
self.running = False
if event.key == K_UP:
self.moveUp = False
# if the player was moving in a sideways direction before, change the direction the player is facing.
if self.moveLeft:
self.direction = self.LEFT
if self.moveRight:
self.direction = self.RIGHT
elif event.key == K_DOWN:
self.moveDown = False
if self.moveLeft:
self.direction = self.LEFT
if self.moveRight:
self.direction = self.RIGHT
elif event.key == K_LEFT:
self.moveLeft = False
if self.moveUp:
self.direction = self.UP
if self.moveDown:
self.direction = self.DOWN
elif event.key == K_RIGHT:
self.moveRight = False
if self.moveUp:
self.direction = self.UP
if self.moveDown:
self.direction = self.DOWN
MainRun()