can someone find the bugg in CheckCollision, for me?
import pygame, sys
#globaler:
BlockSize = 16
PlayerSize = 12
ScreenSize = (1024, 768)
FPS = 60
Gravity = 0.20
BackgroundColour = (255, 255, 255)#(red, green, blue) range: 0-255
BlockColor = (0, 0, 0)
print "Defining helperfunctions...."
def CheckCollision(x, y):
global BlockSize, PlayerSize, Stage
for x2, y2 in ((1,0), (0,1), (0,1), (1,0)):#Each corner of player:
#if corner of player is within a solid in Stage[y][x]:
if Stage[int(y - 1 + PlayerSize*y2) // BlockSize][int(x - 1 + PlayerSize*x2) // BlockSize] == "#":
return True
if Stage[int(y + 2 + PlayerSize*y2) // BlockSize][int(x + 2 + PlayerSize*x2) // BlockSize] == "#":
return True
if Stage[int(y + PlayerSize*y2) // BlockSize][int(x + 2 + PlayerSize*x2) // BlockSize] == "#":
return True
return False
print "Setting up playerobject..."
class Player:
def __init__(self, x, y):
self.x, self.y = float(x), float(y)
self.Color = (255, 0, 127)#(red, green, blue) range: 0-255
self.vspeed = 0.0
def Step(self):
global Gravity
#Apply gravitation pull:
if not CheckCollision(self.x, self.y+1):#if there is no ground beneath the player's feet:
self.vspeed += Gravity
else:
self.vspeed = 0.0
#Walk:
Keys = pygame.key.get_pressed()
if Keys[pygame.K_d] in (1,2):#Right D
if not CheckCollision(self.x+1, self.y):
self.x += 5
if Keys[pygame.K_a] in (1,2):#Left A
if not CheckCollision(self.x-1, self.y):
self.x -= 5
#jump:
if Keys[pygame.K_w] in (1,2):#Up W
#if ground beneath feet:
if CheckCollision(self.x, self.y+1):
self.vspeed = -6
#add gravity and jump to self.y:
if self.vspeed <> 0.0:
#check if the fall will collide
if CheckCollision(self.x, self.y+self.vspeed):
#if so, step pixel by pixel until hitting the wall:
for i in xrange(int(self.y), int(self.y+self.vspeed), 1 if self.vspeed > 0 else -1):
if CheckCollision(self.x, i):
self.y = float(i-1) if self.vspeed > 0 else float(i+1)
break
else:
self.y += self.vspeed
print self.vspeed
def Draw(self, Parent):
global PlayerSize
pygame.draw.rect(Parent, self.Color, (int(self.x), int(self.y), PlayerSize, PlayerSize))#(Surface, color, (x, y, width, height))
you = Player(1*BlockSize, 46*BlockSize)
print "Initalizing pygame..."
pygame.init()
Screen = pygame.display.set_mode(ScreenSize)
Timer = pygame.time.Clock()
#main loop:
print "START GAME!"
while 1:
#maintain FPS:
Timer.tick(FPS)
#read input:
for i in pygame.event.get():
if i.type == pygame.QUIT:
sys.exit()
#step:
you.Step()
#draw:
Screen.fill(BackgroundColour)#clear screen
for x in xrange(len(Stage[0])):
for y in xrange(len(Stage)):
if Stage[y][x] == "#":
pygame.draw.rect(Screen, BlockColor, (x*BlockSize, y*BlockSize, BlockSize, BlockSize))#(Surface, color, (x, y, width, height))
you.Draw(Screen)
#update changes done to Screen:
pygame.display.flip()
import pygame, sys
#globaler:
BlockSize = 16
PlayerSize = 12
ScreenSize = (1024, 768)
FPS = 60
Gravity = 0.20
BackgroundColour = (255, 255, 255)#(red, green, blue) range: 0-255
BlockColor = (0, 0, 0)
print "Defining helperfunctions...."
def CheckCollision(x, y):
global BlockSize, PlayerSize, Stage
for x2, y2 in ((1,0), (0,1), (0,1), (1,0)):#Each corner of player:
#if corner of player is within a solid in Stage[y][x]:
if Stage[int(y - 1 + PlayerSize*y2) // BlockSize][int(x - 1 + PlayerSize*x2) // BlockSize] == "#":
return True
if Stage[int(y + 2 + PlayerSize*y2) // BlockSize][int(x + 2 + PlayerSize*x2) // BlockSize] == "#":
return True
if Stage[int(y + PlayerSize*y2) // BlockSize][int(x + 2 + PlayerSize*x2) // BlockSize] == "#":
return True
return False
print "Setting up playerobject..."
class Player:
def __init__(self, x, y):
self.x, self.y = float(x), float(y)
self.Color = (255, 0, 127)#(red, green, blue) range: 0-255
self.vspeed = 0.0
def Step(self):
global Gravity
#Apply gravitation pull:
if not CheckCollision(self.x, self.y+1):#if there is no ground beneath the player's feet:
self.vspeed += Gravity
else:
self.vspeed = 0.0
#Walk:
Keys = pygame.key.get_pressed()
if Keys[pygame.K_d] in (1,2):#Right D
if not CheckCollision(self.x+1, self.y):
self.x += 5
if Keys[pygame.K_a] in (1,2):#Left A
if not CheckCollision(self.x-1, self.y):
self.x -= 5
#jump:
if Keys[pygame.K_w] in (1,2):#Up W
#if ground beneath feet:
if CheckCollision(self.x, self.y+1):
self.vspeed = -6
#add gravity and jump to self.y:
if self.vspeed <> 0.0:
#check if the fall will collide
if CheckCollision(self.x, self.y+self.vspeed):
#if so, step pixel by pixel until hitting the wall:
for i in xrange(int(self.y), int(self.y+self.vspeed), 1 if self.vspeed > 0 else -1):
if CheckCollision(self.x, i):
self.y = float(i-1) if self.vspeed > 0 else float(i+1)
break
else:
self.y += self.vspeed
print self.vspeed
def Draw(self, Parent):
global PlayerSize
pygame.draw.rect(Parent, self.Color, (int(self.x), int(self.y), PlayerSize, PlayerSize))#(Surface, color, (x, y, width, height))
you = Player(1*BlockSize, 46*BlockSize)
print "Initalizing pygame..."
pygame.init()
Screen = pygame.display.set_mode(ScreenSize)
Timer = pygame.time.Clock()
#main loop:
print "START GAME!"
while 1:
#maintain FPS:
Timer.tick(FPS)
#read input:
for i in pygame.event.get():
if i.type == pygame.QUIT:
sys.exit()
#step:
you.Step()
#draw:
Screen.fill(BackgroundColour)#clear screen
for x in xrange(len(Stage[0])):
for y in xrange(len(Stage)):
if Stage[y][x] == "#":
pygame.draw.rect(Screen, BlockColor, (x*BlockSize, y*BlockSize, BlockSize, BlockSize))#(Surface, color, (x, y, width, height))
you.Draw(Screen)
#update changes done to Screen:
pygame.display.flip()
Last edited by a moderator: