import pygame, sys
from pygame.locals import *
#Game mechanics that dosent fit into a class
global collide
collide = False
def checkCollision(players, blocks):
for player in players:
ox = player.x
oy = player.y
for block in blocks:
if player.rect.colliderect(block.rect):
print "collide"
player.movePlayer(ox, oy)
collide = True
# The class that controls the game
class Game:
def __init__(self):
self.earth = World("Earth", 0.3)
self.PlayerEntities = [Player(10,10)]
#Method that starts the game
def init_game(self):
jumpc = 0
global screen
pygame.init()
screen = pygame.display.set_mode((800,500))
pygame.display.set_caption(self.earth.returnName())
moveDown = False
moveUp = False
moveLeft = False
moveRight = False
tickCounter = 0
while True:
screen.fill((200,200,255))
self.earth.drawWorld()
#print tickCounter
#print self.PlayerEntities[0].x, self.PlayerEntities[0].y
checkCollision(self.PlayerEntities, self.earth.returnBlocks())
for player in self.PlayerEntities:
player.drawPlayer()
#Game mechanic checkers go here:
for players in self.PlayerEntities:
self.earth.gravityMove(players, moveUp)
#Movement has to go here for it to be smooth, sadly.
#Keypress
for event in pygame.event.get():
#Pushing the key
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == K_DOWN:
moveDown = True
if event.key == K_UP:
moveUp = True
jumpc = tickCounter
if event.key == K_LEFT:
moveLeft = True
if event.key == K_RIGHT:
moveRight = True
#Releasing the key
if event.type == pygame.KEYUP:
if event.key == K_DOWN:
moveDown = False
if event.key == K_UP:
moveUp = False
if event.key == K_LEFT:
moveLeft = False
if event.key == K_RIGHT:
moveRight = False
#Moving
if moveDown == True:
self.PlayerEntities[0].movePlayer(0,0.5)
if moveUp == True:
self.PlayerEntities[0].movePlayer(0,-0.5)
if tickCounter == jumpc + 100:
moveUp = False
if moveLeft == True:
self.PlayerEntities[0].movePlayer(-0.3, 0)
if moveRight == True:
self.PlayerEntities[0].movePlayer(0.3, 0)
pygame.display.flip()
tickCounter += 1
def returnScreen(self):
return screen
# The player class
class Player:
# Initializing the position and the looks of the player class.
def __init__(self, x, y):
self.x = x
self.y = y
self.rect = pygame.Rect((self.x, self.y), (self.x+10, self.y+20))
def drawPlayer(self):
pygame.draw.rect(screen, (255,0,0), (self.x,self.y,10,20),1)
def movePlayer(self, nx, ny):
self.x = self.x + nx
self.y = self.y + ny
self.rect.move_ip(self.x, self.y)
# For diffrent textures
class Textures:
def __init__(self):
self.BlockTextures = {"DIRT":(0,255,0)}
def returnTexture(self, type):
return self.BlockTextures["DIRT"]
#World class
class World(Game):
def __init__(self, name, gravity):
self.blocks = [Block("DIRT",10, 50),Block("DIRT",20, 50),Block("DIRT",50, 50)]
self.name = name
self.maxGravity = gravity
self.gravmodify = gravity
def returnName(self):
return self.name
def drawWorld(self):
for block in self.blocks:
block.drawBlock()
def returnBlocks(self):
return self.blocks
def gravityMove(self, player, moveUp):
if collide == False:
if moveUp == False:
if player.y < 500:
player.movePlayer(0, self.maxGravity-self.gravmodify)
self.gravmodify = self.gravmodify - 0.004
if moveUp == True:
self.gravmodify = self.maxGravity
# block class
class Block(World):
def __init__(self, type, x, y):
self.x = x
self.type = type
self.y = y
self.rect = pygame.Rect((self.x, self.y), (self.x+10, self.y+10))
def drawBlock(self):
pygame.draw.rect(screen, Texture.returnTexture(self.type),(self.x,self.y,10,10), 1)
# Debugging and testing for the API
Texture = Textures()
mainGame = Game()
mainGame.init_game()
我已经尝试了3种不同的方法来制作这个游戏的碰撞。他们两个都不行。这一次,它只是不断地从积木中跌落,即使打印出“碰撞”,它也没有像应该的那样停止
我很乐意帮忙,谢谢
目前没有回答
相关问题 更多 >
编程相关推荐