Pygame Colliderect Not Functioning - python

Problem with Pygame! Colliderect function doesn't detect when two rects collide! The rects just fall through each other, though no error message is given. Why is this and how can it be fixed? Been struggling on this same problem for days on end! Supposed problem spot is labeled with a comment. Thanks in advance!
#Start it up
import pygame
pygame.init()
fpsClock = pygame.time.Clock()
#surface = pygame.display.set_mode((640,480),pygame.FULLSCREEN)
surface = pygame.display.set_mode((640,480))
pygame.display.set_caption('Game Skeleton')
#Globals and Misc.
x = 10
y = 350
l = 15
w = 35
moveX=0
moveY=0
characterRect= pygame.Rect(x,y,l,w)
ground = pygame.Rect(0,385,700,385)
ledge1= pygame.Rect(310,330,20,20)
jump=0
white = (255,255,255)
black = (0,0,0)
firebrick = (178,34,34)
blockRects = [ground,ledge1]
contact = False
playOn = True
var=0
#standingLeft = pygame.image.load("images/
#standingRight = pygame.image.load("images/
#walkingRight = pygame.image.load("images/
#walkingLeft = pygame.image.load("images/
#straightJumping = pygame.image.load("images/
#rightJumping = pygame.image.load("images/
#leftJumping = pygame.image.load("images/
#inquire = pygame.image.load("images/
#climbing = pygame.image.load("images/
#Game Loop
while playOn:
#Take user input
for event in pygame.event.get():
if(event.type==pygame.KEYDOWN):
if(event.key==pygame.K_RIGHT):
moveX=1
if(event.key==pygame.K_LEFT):
moveX=-1
if(event.key==pygame.K_UP):
moveY=-1
if(event.key==pygame.K_DOWN):
moveY=1
if(event.key==pygame.K_SPACE):
jump=1
if(event.key==pygame.K_ESCAPE):
playOn = False
if(event.type==pygame.KEYUP):
if(event.key==pygame.K_RIGHT):
moveX=0
if(event.key==pygame.K_LEFT):
moveX=0
if(event.key==pygame.K_UP):
moveY=0
if(event.key==pygame.K_DOWN):
moveY=0
#If user input said to move
x = x + moveX
y = y + moveY
#Jump Code
if(jump>0 and contact==False):
y=y-jump - 1
var=var+1
if(var>45):
jump=0
var=0
if(contact==False):
y=y+1
if(contact!=True):
contact==False
#These two "ifs" don't appear to be working D:
if (ground.colliderect(characterRect)):
y=y-1
contact == True
if(ledge1.colliderect(characterRect)):
y=y-1
contact == True
#Renderings
surface.fill(white)
pygame.draw.rect(surface,black,(x,y,l,w))
pygame.draw.rect(surface,firebrick,(0,385,700,385))
pygame.draw.rect(surface,firebrick,(340,350,100,5))
fpsClock.tick(80)
pygame.display.flip()
pygame.quit()

You are initializing:
characterRect= pygame.Rect(x,y,l,w)
Then you update x,y,l,w variables independently and forget about the characterRect, so the characterRect rect is always on the same position.
You can make the updates directly on the characterRect or assign the new y value before checking for colliderect.
#Justin Pearce corrections are also important, otherwise your code will not work correctly.
Also take a look at PEP 8. For a good looking python code, you should remove brackets around the if conditions.

You have this:
if(contact!=True):
contact==False
#These two "ifs" don't appear to be working D:
if (ground.colliderect(characterRect)):
y=y-1
contact == True
if(ledge1.colliderect(characterRect)):
y=y-1
contact == True
Should that not be?:
if(contact!=True):
contact=False
#These two "ifs" don't appear to be working D:
if (ground.colliderect(characterRect)):
y=y-1
contact = True
if(ledge1.colliderect(characterRect)):
y=y-1
contact = True
Double equals is a comparison operator, not an assignment operator.

Related

How to correctly initialize a Boolean as a variable for use as a "switch"

#Bounce bullet when it reaches corners.
def bullet_bounce(rect):
bottom_corner = False
if bottom_corner == False:
rect.y +=5
if rect.bottom == 400:
bottom_corner = True
if bottom_corner == True:
rect.y -=5
if rect.top == 0:
bottom_corner = False
While working with pygame to create Pong I want to make a rectangle bounce every time it reaches the corner of the screen. I came up with this code and it works as intented when I initialize bottom_corner outside the game loop and then run the rest of the code inside the loop. But since it would look much cleaner I want to implement this code inside as a function. The problem is since the function is called inside the loop bottom_corner is initialized as False every time the loop runs which leads to only the first half of the code being iterated. Do you have any recommendations on how to fix this issue?
You should define variable outside function and send to function as parameter and get it back with return
def bullet_bounce(rect, bottom_corner):
if not bottom_corner:
rect.y +=5
if rect.bottom == 400:
bottom_corner = True
else:
rect.y -=5
if rect.top == 0:
bottom_corner = False
return bottom_corner
# --- start program ---
bottom_corner = False
# --- some loop ---
while True:
bottom_corner = bullet_bounce(rect, bottom_corner)
I am not sure if I understood your question. But I recommend you avoid making equality comparisons between boolean values(True, False) and boolean variables. Instead of saying if bottom_corner == True use if bottom_corner: and else:
And to switch boolean variables you can use the not operator
#Bounce bullet when it reaches corners.
def bullet_bounce(rect):
if bottom_corner: rec.y -=5
else rec.y += 5
if (rec.bottom == 400) or (rect.top == 0):
# switch it based on previous value
bottom_corner = not bottom_corner

TypeError: argument 1 must be pygame.Surface, not str after window.blit() [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I am trying to learn the pygame module and I keep getting 'TypeError: argument 1 must be pygame.Surface, not str' when running it after reorganizing my code and rearranging it into classes, the code below shows the class if it is all that is needed, I have also included the full code if the first bit of code doesn't work.
class player(object):
def __init__(self,x,y,width,height,jH):
self.x= x # x coordinate of the character
self.y = y # y coordinate of the character
self.w = width # width of the character
self.h = height # Height of the character
self.v = 20 # Speed of the character
self.jump = False # Will determine whether the character is jumping
self.jumpC = jH # Jump variable
self.direction = "left"
self.isWalking = 0
def redraw(self,wind):
global number
global previousnumber
global selectedimage
if previousnumber == number:
selectedimage = walkUp[0]
previousnumber = number
window.blit(selectedimage,(self.x,self.y))
Above is the class, the full code is below if it is needed.
import pygame # importing module
pygame.init() # initiates pygame
window = pygame.display.set_mode((500,500)) # creates a 500x500px window
pygame.display.set_caption("PYGAME TEST") # sets caption, self explanatory
class player(object):
def __init__(self,x,y,width,height,jH):
self.x= x # x coordinate of the character
self.y = y # y coordinate of the character
self.w = width # width of the character
self.h = height # Height of the character
self.v = 20 # Speed of the character
self.jump = False # Will determine whether the character is jumping
self.jumpC = jH # Jump variable
self.direction = "left"
self.isWalking = 0
def redraw(self,wind):
global number
global previousnumber
global selectedimage
if previousnumber == number:
selectedimage = walkUp[0]
previousnumber = number
window.blit(selectedimage,(self.x,self.y))
run = True # variable used in a loop, its true if the program is still running.
walkRight = [pygame.image.load('right1.png'), pygame.image.load('right2.png'), pygame.image.load('right3.png')]
walkLeft = [pygame.image.load('left1.png'), pygame.image.load('left2.png'), pygame.image.load('left3.png')]
walkUp = [pygame.image.load('front1.png').convert(), pygame.image.load('front2.png'), pygame.image.load('front3.png')]
walkDown = [pygame.image.load('back1.png'), pygame.image.load('back2.png'), pygame.image.load('back3.png')]
bg = pygame.image.load('bg.jpg')
clock = pygame.time.Clock()
number = 0
selectedimage = walkUp[number]
previousnumber = -2
def spriteupdate(d,a,man):
global number
if man.direction != d:
man.direction = d
changed(0,a,man)
else:
number+= 1
if number>2:
number = 0
changed(number,a)
else:
changed(number,a,troop)
def changed(numb, direction,man):
global number
global selectedimage
number = numb
selectedimage = man.direction[number]
def update(man):
troop.redraw(window)
window.blit(bg,(0,0))
pygame.display.update()
troop = player(250,250,10,10,10)
while run:
clock.tick(15)# FPS
for event in pygame.event.get():
if event.type == pygame.QUIT: # if event quits, presses the x button
run = False # breaks loop
keys = pygame.key.get_pressed() # gets the keys pressed
if keys[pygame.K_LEFT]:# pressed left arrow, x coordinate changes by - speed
#direction, array
spriteupdate("left",walkLeft,troop)
if troop.x <= -10:
pass
else:
troop.x-=troop.v
if keys[pygame.K_RIGHT]: # similar here
spriteupdate("right", walkRight,troop)
if troop.x >= 470:
pass
else:
troop.x+=troop.v
if not(troop.jump): # Only works if the character isn't jumping
if keys[pygame.K_SPACE]: # The character is jumping. changes variable.
troop.jump = True
if keys[pygame.K_UP]: # same here
spriteupdate("down",walkDown,troop)
if troop.y <= -10:
pass
else:
troop.y-=troop.v
if keys[pygame.K_DOWN]: # same here
spriteupdate("up",walkUp,troop)
if troop.y >= 470:
pass
else:
troop.y+=troop.v
else:
if troop.jumpC >= -10: # jumpC is already bigger than -10, this is called
num = 1 # number, will be set to -1 if jumpC becomes a negative number
if troop.jumpC < 0:
num = -1 # there.
troop.y-= (troop.jumpC ** 2) * 0.5 * num # y subtracted (goes up) by the square of jumpC * half * num
troop.jumpC -= 1 # Subtracts 1
else:
troop.jump = False # If Jump c is smaller than -10, jump has finished.
troop.jumpC = 10 # reset variable
update(troop)
pygame.quit()
OUTPUT:
pygame 1.9.6
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File line 107, in <module>
update(troop)
File line 56, in update
troop.redraw(window)
File , line 25, in redraw
window.blit(selectedimage,(self.x,self.y))
TypeError: argument 1 must be pygame.Surface, not str
This output is above, hope that helps.
On line 53 selectedimage is changed from a surface to a string with :
selectedimage = man.direction[number]
I see one problem right away. In the player.redraw method, wind is the argument passed, but in the body of the method you use window. Try changing the argument:
def redraw(self, window):

Why wont my collision detection or falling images work?

I have recently written this code where fruits and other things fall from the top of the screen and the frog at the bottom has to try and catch them. The collision detection is not working and neither is the falling images as they seem to just fall and get stuck at the top of the screen. This is most of my code as I cannot seem to work out which area the actual error is in:
import pygame, sys, time, random
from pygame.locals import *
######### constants ##########
jumpvel=20
fallingspeed=0.5
running= True
blue= [129,183 ,253]
pink=[255,174,201]
textcolour= [255,255,255]
x=700//2
y=1000//2
score=0
#### fruits and naughty ######
thingylist= ['fruit1.bmp','fruit2.bmp','fruit3.bmp','fruit4.bmp','fruit5.bmp','fruit1.bmp','fruit2.bmp','fruit3.bmp','fruit4.bmp','fruit5.bmp','naughty1.bmp','naughty2.bmp','naughty3.bmp',]
all_things=[]
for i in range (12):
new_thing_image=pygame.image.load(thingylist[(random.randrange(0,12))])
new_thing_image.set_colorkey(pink)
new_thing_rect=new_thing_image.get_rect()
new_thing_rect.x=random.randrange(0,950)
new_thing_rect.y=-random.randrange(50,500)
all_things.append([new_thing_image,new_thing_rect])
################collision###############
def checkCollision (frog_rect,all_things,score):
collides_with=None
for i in range (len(all_things)):
thing_rect=all_things[i][1]
if (frog_rect.colliderect(thing_rect)):
score=score+100
return collides_with
######## initialising screen#########
pygame.init()
gamedisplay=pygame.display.set_mode((1000,600)) #making the screen
pygame.display.set_caption('frog')
clock=pygame.time.Clock()# frames per second
bg=pygame.image.load('actual clouds.bmp').convert()
############ initialising sprites##############
frog= pygame.image.load('actual frog.bmp')
frog.set_colorkey(blue)
frog_rect=frog.get_rect()
frog_rect.centerx=(x)
frog_rect.centery=(y)
####### score###########
pygame.font.init()
font= pygame.font.SysFont ('Dubai MS', 48)
##########drawing things#############
def drawThings (all_things):
for item in all_things:
new_thing_image, new_thing_rect= item
gamedisplay.blit(new_thing_image, (new_thing_rect.x, new_thing_rect.y))
#########update display function###########
def update(x,y,all_things,score):
gamedisplay.blit(bg,[0,0])
gamedisplay.blit(frog,(x,y))
for thing in range (len(all_things)):
new_thing_rect=all_things[i][1]
#thing_rect.y=thing_rect.y+fallingspeed
new_thing_rect.y+= fallingspeed
drawThings(all_things)
label=font.render("score "+ str(score) ,1,textcolour)
gamedisplay.blit(label,(750,10))
pygame.display.update()
pygame.time.delay(50)
#########main game loop ############
while running == True:
gamedisplay.blit(bg,[0,0])
gamedisplay.blit(frog,(x,y))
drawThings(all_things)
label=font.render("score "+ str(score) ,1,textcolour)
gamedisplay.blit(label,(750,10))
pygame.display.flip()
pygame.event.pump()
key=pygame.key.get_pressed()
########### escape ###########
if key [pygame.K_ESCAPE]:
sys.exit()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
########### controls ##############
if key[pygame.K_LEFT]:
x -=2
elif key[pygame.K_RIGHT]:
x +=2
elif key[pygame.K_SPACE]or key[pygame.K_UP]:
for i in range (5):
y -= jumpvel
update(x,y,all_things,score)
for i in range (5):
y += jumpvel
update(x,y,all_things,score)
######## limits ####################
if x < 10:
x = 10
elif (x > (900 - 2)):
x= 900-2
######### falling###########
for item in all_things:
new_thing_image, new_thing_rect= item
#new_thing_rect=all_things[i][1]
#thing_rect.y=thing_rect.y+fallingspeed
new_thing_rect.y+= fallingspeed
############collision detection##########
detect=checkCollision (frog_rect, all_things,score)
if (detect !=None):
score=score+100
update(x,y,all_things,score)
The 'things' are meant to fall to the bottom of the screen for the frog to catch but they are all seemingly getting stuck at the top. When i tested the code for collision detection even when the two images collided it had no effect on the score- meaning something is not working.
Falling images stop because of this
fallingspeed = 0.5
rect uses integer values to keep positon so it will rount 0.5 to integer - int(0.5) == 0.
When you have y = 0 and you add 0.5 - so you could expect y = 0.5 - it will round it to y = 0. In next loop you will add again 0.5 and it will round it to y = 0 again. This way it stops on y = 0
When you have ie y = -5 and you add 0.5 then you could expect -4.5 but it round to -4 (not -5) so it is moving.
Use
fallingspeed = 1
and it will not stop on y = 0
old_y = 0
new_y = int(old_y+0.5)
print(old_y, new_y)
# 0 0
old_y = -5
new_y = int(old_y+0.5)
print(old_y, new_y)
# -5 -4
Sometimes in more complex code it is important to keep position in float values and then you would have to use float variables instead of rect to keep positon and copy it to rect only when you have to draw or check collision
collisions
Inside checkCollision you set collides_with = None but you never change it to `True
def checkCollision (frog_rect,all_things, score):
collides_with = None
for i in range (len(all_things)):
thing_rect = all_things[i][1]
if (frog_rect.colliderect(thing_rect)):
score=score+100
collides_with = True
return collides_with
You could write it shorter
def checkCollision(frog_rect,all_things, score):
collides_with = None
for thing_image, thing_rect in all_things:
if frog_rect.colliderect(thing_rect):
score = score+100
collides_with = True
return collides_with
Now it should change score in line
detect = checkCollision(frog_rect, all_things, score)
if detect:
score = score+100
But if you want to change it inside checkCollision then you have to return value score. Variable score inside checkCollision is local variable and it doesn't change value in global variable score
def checkCollision(frog_rect,all_things, score):
collides_with = None
for thing_image, thing_rect in all_things:
if frog_rect.colliderect(thing_rect):
score = score+100
collides_with = True
return collides_with, score
and then you have to assing to global score
detect, score = checkCollision(frog_rect, all_things, score)

Low frame rate, possibly because un-accelerated graphics

So I began creating a game a couple months ago using pygame, it has a top view similar to Starcraft, Age of empires etc... I have written around 1320 lines of code to create the basis of my game; however, I am experiencing issues with frame rate when blitting images and believe this is because I cannot use accelerated graphics with pygame. The way I am currently blitting images is by blitting all images ahead of time on a surface which I then subsurface to create a blit image of my entire screen. Is there a more effective way that i should be utilizing?
So my assumption is that would be huge mess to look through and I do not want to waste your guy's time. Essentially any time I blit a surface the size of my screen my framerate drops by ~20 frames, is there a way I can avoid this in pygame?
##PYGAME INITATE##
import pygame, os
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
_W,_H = pygame.display.Info().current_w, pygame.display.Info().current_h
flags = pygame.DOUBLEBUF | pygame.HWSURFACE
gameDisplay = pygame.display.set_mode((_W,_H),pygame.FULLSCREEN ) ## CREATES SCREEN YOU DISPLAY ON ##
gameDisplay.fill((0,0,0)) ## FILL COLOR OF SCREEN ##
pygame.display.set_caption("Dope Game") ## SETS NAME ##
gameClock = pygame.time.Clock() ## CLOCK OF THE GAME ##
import math
import os
import random
import copy
SQRT = math.sqrt
PI = math.pi
cos = math.cos
sin = math.sin
## REPEATABLE FUNCTIONS ##
def loadScale(file,command,sizeX,sizeY):
temp = pygame.image.load(file)
tempInfo = temp.get_rect()
tempInfo1,tempInfo2,tempInfo3,tempInfo4 = temp.get_rect()
tempInfo3 = int(tempInfo3)
tempInfo4 = int(tempInfo4)
if (command == "ratio"):
tempInfo3 = tempInfo3*sizeX
tempInfo4 = tempInfo4*sizeY
temp = pygame.transform.scale(temp,(int(tempInfo3),int(tempInfo4) ) )
elif (command == "size"):
temp = pygame.transform.scale(temp, (sizeX,sizeY) )
return(temp)
## NON GAME RELATED CLASSES ##
class EnterFrame():
def __init__(self,frameReset,function,parse,reset):
self.frameReset = frameReset
self.currentFrame = frameReset
self.function = function
self.parse = parse
self.reset = reset
if (self.reset != "onComplete"):
self.reset = (reset-1)
enterFrameTable.append(self)
def step(self,enterFrameTable):
if (self.currentFrame == 0):
self.function(self.parse)
if (self.reset != "onComplete"):
if (self.reset > 0):
self.currentFrame = self.frameReset
self.reset = self.reset-1
else:
enterFrameTable.remove(self)
del self
else:
self.currentFrame = self.frameReset
else:
self.currentFrame = self.currentFrame-1
class PlayerCreation():
def __init__(self):
self.x = _W
self.y = _H
self.view = [1600,1600]
self.viewShift = []
self.viewChangeSpeed = 25
def moveView(self,key):
add = EnterFrame(0,self.moveViewAction,key,"onComplete")
self.viewShift.append([add,key])
def moveViewAction(self,key):
if (key == "up"):
self.view[1] = self.view[1]-self.viewChangeSpeed
Map.recenterView()
if (self.view[1] < 0):
self.view[1] = 0
elif (key == "right"):
self.view[0] = self.view[0]+self.viewChangeSpeed
Map.recenterView()
if (self.view[0] > Map.tileSize*4):
self.view[0] = Map.tileSize*4
elif (key == "down"):
self.view[1] = self.view[1]+self.viewChangeSpeed
Map.recenterView()
if (self.view[1] > Map.tileSize*4):
self.view[1] = Map.tileSize*4
elif (key == "left"):
self.view[0] = self.view[0]-self.viewChangeSpeed
Map.recenterView()
if (self.view[0] < 0):
self.view[0] = 0
def endMoveView(self,key):
for i in range(len(self.viewShift)-1,-1,-1 ):
if (self.viewShift[i][1] == key):
enterFrameTable.remove(self.viewShift[i][0])
del self.viewShift[i]
class ImageCreation():
def __init__(self,name,image,type,hitBox):
self.name = name
self.image = image
self.type = type
self.hitBox = hitBox
self.rect = self.image.get_rect()
if (self.hitBox != "none"):
self.shiftX = hitBox[0][0]
self.shiftY = hitBox[0][1]
for i in range(1,len(hitBox) ):
if (hitBox[i][0] < self.shiftX):
self.shiftX = hitBox[i][0]
if (hitBox[i][1] < self.shiftY):
self.shiftY = hitBox[i][1]
else:
self.shiftX = self.rect[2]/2
self.shiftY = self.rect[3]/2
imageTable.append(self)
def draw(self,x,y):
image = self.image
self.blit = gameDisplay.blit(image,(x,y) )
class MapCreation():
def __init__(self):
self.tileSize = 800
self.size = self.tileSize*10
self.tiles = []
self.loadedTiles = []
self.surface = pygame.Surface([self.tileSize*5,self.tileSize*5], pygame.SRCALPHA, 32)
self.centerTile = [5,5]
self.drawPoint = [(_W-self.tileSize)/2,(_H-self.tileSize)/2]
self.amount = round(self.size/self.tileSize)
backGround = loadScale("Grass.png","size",self.tileSize,self.tileSize)
for i in range(0,self.amount):
for u in range(0,self.amount):
image = copy.copy(backGround)
newTile = Tile(image,[u*self.tileSize,i*self.tileSize])
self.tiles.append(newTile)
info = imageFind("House.png")
tile = self.tiles[55]
imageObject(info,tile,[240,50])
self.loadTiles("center")
def recenterView(self):
if (Player.view[0] > 3*self.tileSize):
self.centerTile[0] = self.centerTile[0]+1
Player.view[0] = Player.view[0]-self.tileSize
self.loadTiles("right")
print("right")
elif (Player.view[0] < 1*self.tileSize):
self.centerTile[0] = self.centerTile[0]-1
Player.view[0] = Player.view[0]+self.tileSize
self.loadTiles("center")
print("center")
if (Player.view[1] > 3*self.tileSize):
self.centerTile[1] = self.centerTile[1]+1
Player.view[1] = Player.view[1]-self.tileSize
self.loadTiles("center")
print("center")
elif (Player.view[1] < 1*self.tileSize):
self.centerTile[1] = self.centerTile[1]-1
Player.view[1] = Player.view[1]+self.tileSize
self.loadTiles("center")
print("center")
def loadTiles(self,load):
tileIndex = self.centerTile[0]+self.centerTile[1]*self.amount
if (load == "center"):
self.loadedTiles = []
for i in range(-2,3):
for u in range(-2,3):
loadTile = tileIndex + i*self.amount + u
self.loadedTiles.append(self.tiles[loadTile])
self.tiles[loadTile].loaded = [u+2,i+2]
self.surface = pygame.Surface([self.tileSize*5,self.tileSize*5], pygame.SRCALPHA, 32)
for i in range(0,len(self.loadedTiles) ):
sx = self.loadedTiles[i].loaded[0]*self.tileSize
sy = self.loadedTiles[i].loaded[1]*self.tileSize
self.surface.blit(self.loadedTiles[i].buttomLayer,(sx,sy) )
for i in range(0,len(self.loadedTiles) ):
sx = self.loadedTiles[i].loaded[0]*(self.tileSize+2)
sy = self.loadedTiles[i].loaded[1]*(self.tileSize+2)
self.surface.blit(self.loadedTiles[i].middleLayer,(sx,sy) )
for i in range(0,len(self.loadedTiles) ):
sx = self.loadedTiles[i].loaded[0]*(self.tileSize+2)
sy = self.loadedTiles[i].loaded[1]*(self.tileSize+2)
self.surface.blit(self.loadedTiles[i].topLayer,(sx,sy) )
elif (load == "right"):
self.loadedTiles = []
for i in range(-2,3):
for u in range(-2,3):
loadTile = tileIndex + i*self.amount + u
self.loadedTiles.append(self.tiles[loadTile])
self.tiles[loadTile].loaded = [u+2,i+2]
## OLD METHOD THAT WASNT WORKING ##
##subSurf = self.surface.subsurface(self.tileSize,0,self.tileSize*1,self.tileSize*5)
##self.surface = pygame.Surface([self.tileSize*5,self.tileSize*5], pygame.SRCALPHA, 32)
##self.surface.blit(subSurf,(0,0) )
## NEW METHOD ##
self.surface.scroll(dx=-self.tileSize*1,dy=0)
def draw(self):
global Player
image = self.surface.subsurface(Player.view[0],Player.view[1],self.tileSize,self.tileSize)
image = pygame.transform.scale(image,(self.tileSize,self.tileSize) )
gameDisplay.blit(image,((_W-self.tileSize)/2,(_H-self.tileSize)/2) )
image = pygame.transform.scale(self.surface,(300,300) )
gameDisplay.blit(image,(0,0 ) )
class Tile():
def __init__(self,image,coords):
transparentSurface = pygame.Surface([1000,1000], pygame.SRCALPHA, 32)
self.x = coords[0]
self.y = coords[1]
self.loaded = False
self.buttomLayer = image
self.middleLayer = copy.copy(transparentSurface)
self.topLayer = transparentSurface
class imageObject():
def __init__(self,info,tile,coords):
self.info = info
self.image = info.image
self.rect = self.image.get_rect()
self.x = coords[0]
self.y = coords[1]
self.hitBox = []
if (self.info.hitBox != "none"):
for i in range(0,len(self.info.hitBox) ):
self.hitBox.append([self.info.hitBox[i][0]+self.x,self.info.hitBox[i][1]+self.y])
#self.object = createObject(self.hitBox,True,"none")
if (info.type == "background"):
tile.buttomLayer.blit(self.image,(self.x,self.y) )
if (info.type == "object"):
tile.middleLayer.blit(self.image,(self.x,self.y) )
if (info.type == "object alphas"):
tile.topLayer.blit(self.image,(self.x,self.y) )
def imageFind(name):
for i in range(0,len(imageTable) ):
if (name == imageTable[i].name):
return(imageTable[i])
return("none")
def imageLoad(types):
if (types == "basic"):
image = loadScale("House.png","ratio",1,1)
basicHouse = ImageCreation("House.png",image,"object",[[190, 375], [350, 375], [350, 235], [190, 235]])
image = loadScale("Grass.png","ratio",1,1)
grass = ImageCreation("Grass.png",image,"background","none")
def enterFrameHandle(enterFrameTable):
for i in range(len(enterFrameTable)-1,-1,-1 ):
enterFrameTable[i].step(enterFrameTable)
def EventHandle(event):
global Player
if (event.type == pygame.QUIT):
endGame()
elif(event.type == pygame.KEYDOWN):
key = (pygame.key.name(event.key) )
if (key == "escape"):
endGame()
elif (key == "up" or key == "right" or key == "down" or key == "left"):
Player.moveView(key)
elif(event.type == pygame.KEYUP):
key = (pygame.key.name(event.key) )
if (key == "up" or key == "right" or key == "down" or key == "left"):
Player.endMoveView(key)
def endGame():
global QuitGame
QuitGame = True
def mainLoop():
## GLOBALS ##
global QuitGame
QuitGame = False
global Player
Player = PlayerCreation()
## MAIN TABLES ##
global enterFrameTable
enterFrameTable = []
global basicObjectTable
basicObjectTable = []
## TEMP TABLES ##
global imageTable
imageTable = []
## START UP LOOPS ##
imageLoad("basic")
global Map
Map = MapCreation()
## Temporary ##
while (QuitGame == False):
enterFrameHandle(enterFrameTable)
for event in pygame.event.get():
EventHandle(event)
Map.draw()
pygame.display.update() ## updates the screen ##
gameDisplay.fill([0,0,0]) ## Clears screen for next frame ##
gameClock.tick(64) ## The FPS ##
fps = gameClock.get_fps()
if (fps < 64 and fps != 0):
fps = gameClock.get_fps()
print("FPS HAS DROPPED TOO LOW DOWN TO",fps)
runGameNow = True
mainLoop()
pygame.quit()
quit()
Issue with path finder is due to picking up on itteration lines when finding nearest point.
The problem is quite straight forward really.
Your view the amount of lines as disease and the cause must be Pygame. Usually, or at least in almost any case - if you're not a god at using the language and library, the problem is probably not the language or the library. Because odds are you are no wear near to pushing the limits of what the two can deliver for you.
One dead give-away of this fact is, if you look at what your eyes see on screen, you'll quickly notice that whenever your background is about to loop around - that's when the glitch/frame drop occur.
Obviously, this COULD be a cause of the library doing something suspicious.. if it was the library that did the actual loop-around.
But in your code, this is a implementation you've done yourself.
So the best bet is to start looking there.
Just to be extremely confident in where the delay occurs - we can have a look at the profiler that comes with Python (cProfiler).
python -m cProfile -o sample_data.pyprof awesome_game.py
pyprof2calltree -i sample_data.pyprof -k
The result would show something along the lines of:
From this we can see that a lot of processing time goes to pygame.Surface and enterFrameHandle. And I bet that pygame.Surface is called within enterFrameHandle somewhere down the line.
So the best bet is to start at the end of the eventFrameHandle chain.
And in this breakdown, that's loadTiles.
And straight off the bat, a lot of warning signs in here.
There's at least four loops in here.. and loops are bad, because those take processing time.
I added debug information by simply doing:
def loadTiles
if load == "center":
print('Loading center')
elif load == "right":
print('Loading right')
Apparently center gets triggered whenever the glitch occurs.
So that narrows it down a little further. So i added timers around each for loop.
Loop one: takes 0.0 seconds
Loop two: takes 0.29 seconds
Loop three: takes 0.5 seconds
Loop four: takes 0.6 seconds
All in all, these are EXTREMELY bad for you, since they directly impact the render sequence. So why is this? Lets break it down even further.
We'll start off with the first loop that takes up 0.3 seconds:
for i in range(0,len(self.loadedTiles) ):
sx = self.loadedTiles[i].loaded[0]*self.tileSize
sy = self.loadedTiles[i].loaded[1]*self.tileSize
self.surface.blit(self.loadedTiles[i].buttomLayer,(sx,sy) )
So len(self.loadedTiles) will be 25.
That means this loop has to iterate 25 times per render cycle where loadTiles("center") is called.
Each cycle takes almost exactly 0.01 seconds which amounts up to 0.25 seconds per full loop. It's not terrible, well it is.. If you want 60 FPS out of your game, no loop or function call can take more than 0.0166 seconds in total.
So we've already overrun our desired FPS target. So we gotta knock a few milliseconds off this if we wanna get anywhere.
The loop in itself is not that bad, i mean 25 iterations, a modern PC can do that in less time than time() can measure. So, it all points to self.surface.blit().. Which from experience, it sure is.
This also correlate with the fact that our graph above spends 30% of the total CPU time in pygame.surface.blit.. So here's our first thief.
Looking at the rest of the loops, they are essentially the same, except with bigger numbers in the math which takes slight longer to calculate, hence probably the time difference in the loops.
So, what can we do to shrink down .blit times?
Well we can stop calling blit in every loop iteration, and just move the sprite objects positions and then blit them one by one.
But sensei, that's almost the same thing?
Well yes my soon to be ninja turtle, it is.. That's why, we'll move the sprites in to batches/groups, and render the group. Thus, we can change the positions (fast operation), and because we moved the rendering outside of the loops, we can also convert them into a group object and render the group.
First, we'll convert your objects into pygame.sprite.Sprite, these are clever little objects that contain both collision detection, moving arounds etc.
self.grass = pygame.sprite.Sprite()
self.grass.image = pygame.image.load("Grass.png")
self.grass.rect = pygame.Rect(0, 0, 60, 60)
And you add this into a rendering group (a batch):
self.main_group = pygame.sprite.Group(self.grass)
Bam, and instead of updating 25 sprites, now you can do:
self.main_group.draw(gameDisplay)
And bam, SPEED!
Now, your code is NOT designed for this.
So this will take a while to fix and correct this design flaw.
Because this would take hours for me in order to keep your original code as close as possible, i ignored this and reworked your entire MapCreation and modified your PlayerCreation and how you move the map around (I changed from trying to recenter each time, it's a nice idea but far to complex to implement 1PM rewriting someone else's code).. So.. Here's the new approach to your problem:
import pygame, os
from time import time
from collections import OrderedDict
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
_W,_H = pygame.display.Info().current_w, pygame.display.Info().current_h
flags = pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.RESIZABLE
gameDisplay = pygame.display.set_mode((500, 500))
gameDisplay.fill((0,0,0)) ## FILL COLOR OF SCREEN ##
pygame.display.set_caption("Dope Game") ## SETS NAME ##
gameClock = pygame.time.Clock() ## CLOCK OF THE GAME ##
import math
import os
import random
import copy
SQRT = math.sqrt
PI = math.pi
cos = math.cos
sin = math.sin
## REPEATABLE FUNCTIONS ##
def loadScale(file,command,sizeX,sizeY):
temp = pygame.image.load(file)
tempInfo = temp.get_rect()
tempInfo1,tempInfo2,tempInfo3,tempInfo4 = temp.get_rect()
tempInfo3 = int(tempInfo3)
tempInfo4 = int(tempInfo4)
if (command == "ratio"):
tempInfo3 = tempInfo3*sizeX
tempInfo4 = tempInfo4*sizeY
temp = pygame.transform.scale(temp,(int(tempInfo3),int(tempInfo4) ) )
elif (command == "size"):
temp = pygame.transform.scale(temp, (sizeX,sizeY) )
return(temp)
## NON GAME RELATED CLASSES ##
class EnterFrame():
def __init__(self,frameReset,function,parse,reset):
self.frameReset = frameReset
self.currentFrame = frameReset
self.function = function
self.parse = parse
self.reset = reset
if (self.reset != "onComplete"):
self.reset = (reset-1)
enterFrameTable.append(self)
def step(self,enterFrameTable):
if (self.currentFrame == 0):
self.function(self.parse)
if (self.reset != "onComplete"):
if (self.reset > 0):
self.currentFrame = self.frameReset
self.reset = self.reset-1
else:
enterFrameTable.remove(self)
del self
else:
self.currentFrame = self.frameReset
else:
self.currentFrame = self.currentFrame-1
class PlayerCreation():
def __init__(self):
self.x = _W
self.y = _H
self.view = [1600,1600]
self.viewShift = []
self.viewChangeSpeed = 25
def moveView(self,key):
add = EnterFrame(0,self.moveViewAction,key,"onComplete")
self.viewShift.append([add,key])
def moveViewAction(self,key):
if (key == "up"):
self.view[1] = self.view[1]-self.viewChangeSpeed
Map.move_tile(0, 1) # Player moves up, so the tiles should move down -> (0, 1) == (x, y)
if (self.view[1] < 0):
self.view[1] = 0
elif (key == "right"):
self.view[0] = self.view[0]+self.viewChangeSpeed
Map.move_tile(-1, 0)
if (self.view[0] > Map.tileSize*4):
self.view[0] = Map.tileSize*4
elif (key == "down"):
self.view[1] = self.view[1]+self.viewChangeSpeed
Map.move_tile(0, -1)
if (self.view[1] > Map.tileSize*4):
self.view[1] = Map.tileSize*4
elif (key == "left"):
self.view[0] = self.view[0]-self.viewChangeSpeed
Map.move_tile(1, 0)
if (self.view[0] < 0):
self.view[0] = 0
def endMoveView(self,key):
for i in range(len(self.viewShift)-1,-1,-1 ):
if (self.viewShift[i][1] == key):
enterFrameTable.remove(self.viewShift[i][0])
del self.viewShift[i]
class ImageCreation():
def __init__(self,name,image,type,hitBox):
self.name = name
self.image = image
self.type = type
self.hitBox = hitBox
self.rect = self.image.get_rect()
if (self.hitBox != "none"):
self.shiftX = hitBox[0][0]
self.shiftY = hitBox[0][1]
for i in range(1,len(hitBox) ):
if (hitBox[i][0] < self.shiftX):
self.shiftX = hitBox[i][0]
if (hitBox[i][1] < self.shiftY):
self.shiftY = hitBox[i][1]
else:
self.shiftX = self.rect[2]/2
self.shiftY = self.rect[3]/2
imageTable.append(self)
def draw(self,x,y):
image = self.image
self.blit = gameDisplay.blit(image,(x,y) )
class MapCreation():
def __init__(self):
self.tileSize = 800
self.size = self.tileSize*10
self.tiles = []
self.centerTile = [5,5]
self.amount = round(self.size/self.tileSize)
self.sprites = OrderedDict()
self.grass_image = pygame.image.load("Grass.png")
self.grass_group = pygame.sprite.Group()
for x in range(0, self.amount*60, 60): ## 10*60, but we step 60 pixels, so in the end, this will be 10 steps.
for y in range(0, self.amount*60, 60): ## Which is the same as `for x in range(self.amount)` but we scale it up
## to give us pixels instead of just the ammount.
index = len(self.sprites) # -- Generate a index for this sprite. Used for access later (to update pos for instace)
## == Create the sprite, add a image to it and define a position and size.
self.sprites[index] = pygame.sprite.Sprite()
self.sprites[index].image = self.grass_image
self.sprites[index].rect = pygame.Rect(x, y, 60, 60)
## == Then add the sprite to the grass group.
self.grass_group.add(self.sprites[index])
def move_tile(self, dx, dy):
for index in self.sprites:
x, y, width, height = self.sprites[index].rect
## == this is how you move the sprites:d
self.sprites[index].rect = pygame.Rect(x+dx, y+dy, 60, 60)
def draw(self):
self.grass_group.update()
self.grass_group.draw(gameDisplay)
class Tile():
def __init__(self,image,coords):
transparentSurface = pygame.Surface([1000,1000], pygame.SRCALPHA, 32)
self.x = coords[0]
self.y = coords[1]
self.loaded = False
self.buttomLayer = image
self.middleLayer = copy.copy(transparentSurface)
self.topLayer = transparentSurface
class imageObject():
def __init__(self,info,tile,coords):
self.info = info
self.image = info.image
self.rect = self.image.get_rect()
self.x = coords[0]
self.y = coords[1]
self.hitBox = []
if (self.info.hitBox != "none"):
for i in range(0,len(self.info.hitBox) ):
self.hitBox.append([self.info.hitBox[i][0]+self.x,self.info.hitBox[i][1]+self.y])
#self.object = createObject(self.hitBox,True,"none")
if (info.type == "background"):
tile.buttomLayer.blit(self.image,(self.x,self.y) )
if (info.type == "object"):
tile.middleLayer.blit(self.image,(self.x,self.y) )
if (info.type == "object alphas"):
tile.topLayer.blit(self.image,(self.x,self.y) )
def imageFind(name):
for i in range(0,len(imageTable) ):
if (name == imageTable[i].name):
return(imageTable[i])
return("none")
def imageLoad(types):
if (types == "basic"):
image = loadScale("House.png","ratio",1,1)
basicHouse = ImageCreation("House.png",image,"object",[[190, 375], [350, 375], [350, 235], [190, 235]])
image = loadScale("Grass.png","ratio",1,1)
grass = ImageCreation("Grass.png",image,"background","none")
def enterFrameHandle(enterFrameTable):
for i in range(len(enterFrameTable)-1,-1,-1 ):
enterFrameTable[i].step(enterFrameTable)
def EventHandle(event):
global Player
if (event.type == pygame.QUIT):
endGame()
elif(event.type == pygame.KEYDOWN):
key = (pygame.key.name(event.key) )
if (key == "escape"):
endGame()
elif (key == "up" or key == "right" or key == "down" or key == "left"):
Player.moveView(key)
elif(event.type == pygame.KEYUP):
key = (pygame.key.name(event.key) )
if (key == "up" or key == "right" or key == "down" or key == "left"):
Player.endMoveView(key)
def endGame():
global QuitGame
QuitGame = True
def mainLoop():
## GLOBALS ##
global QuitGame
QuitGame = False
global Player
Player = PlayerCreation()
## MAIN TABLES ##
global enterFrameTable
enterFrameTable = []
global basicObjectTable
basicObjectTable = []
## TEMP TABLES ##
global imageTable
imageTable = []
## START UP LOOPS ##
imageLoad("basic")
global Map
Map = MapCreation()
## Temporary ##
while (QuitGame == False):
enterFrameHandle(enterFrameTable)
for event in pygame.event.get():
EventHandle(event)
pygame.display.update() ## updates the screen ##
gameDisplay.fill([0,0,0]) ## Clears screen for next frame ##
#Map.draw()
Map.grass_group.update()
Map.grass_group.draw(gameDisplay)
gameClock.tick(64) ## The FPS ##
fps = gameClock.get_fps()
if (fps < 64 and fps != 0):
fps = gameClock.get_fps()
print("FPS HAS DROPPED TOO LOW DOWN TO",fps)
pygame.display.flip()
runGameNow = True
mainLoop()
pygame.quit()
quit()
This code rarely drop down in FPS.
And when it does, it drops down to ~63 FPS because I moved the window around (that's a taxing thing to do because all the OpenGL references need to compensate for the new window position and events that triggers within Pygame (resize events, move events etc are taxing, but occurs once and not every render loop.. so it's acceptable).
Conclusion
It's rarely the library's fault for slow performance.
Stop thinking as a developer for a second, and watch the screen and see if you can find clues to as where the fault may lie. In this case a obvious glitch occurred every time the tiles rotated around the screen edge.. That's a clue! Not a single programming skill needed to visually see this bug.
Use a profiler and debug where your code spends most of the time.
Try to see how others have solved game designs.. You're on the right track by putting everything in classes, but that mostly helps your readability of the code - try to see what's in other peoples classes and how they use Pygame calls.
All in all, it's quite a fun and nice code you got going.
It's just not made for performance and it's hard to code in afterwards.. so maybe it's not such a bad idea to start all over? But keep your old code base for reference and avoid the obvious traps:
loops in the render sequence
rendering objects one by one, use batches instead
Only do loops to update positions/stats, not graphics!
Last note on the topic: Graphic cards are huge power houses. They can calculate a few million operations a second if not more. And each time you blit something to the screen, the graphics card needs to interrupt it's calculations and memory allocation to flip around buffers and update what you see on the screen.. And that's slow work because it's not calculations, it's operational tasks (which usually includes waiting for queues and signals).. So instead of throwing hundreds of blit's a second to the graphics card, throw math operations at it by sending it textures (math data describing how things look).. and then call blit (draw, in this case) once and let the graphics card take all the math you threw at it - do one big logical operation - and presto.. x100 the speed already.

Updating a Score keeper

I am having a lot of difficulty at the moment with updating the score on the GUI of my basic game in python.
I currently have the code such that it appears in the top left hand corner, with the score appearing as "O". However, the score remains at 0 for the entirety of the program (as it is only getting rendered once. I am unsure how to constantly rerender the score to update it? I have tried many methods and have been unsuccessful. The part of the code with the score render and my attempt at keeping track of the score using the variable conlisionNumber is:
new = bears[:]
for bear in new: #this makes a copy of the array
if player.colliderect(bear):
windowSurface.blit(bearImageTwo, bear)
windowSurface.blit(playerImageTwo, player)
def explosion():
for bear in bears:
if player.colliderect(bear) and (moveLeft == False and moveRight == False and moveUp == False and moveDown == False):
bears.remove(bear)
colisionNumber += 1
if player.colliderect(bear) and (moveLeft == False and moveRight == False and moveUp == False and moveDown == False):
t = Timer(1, explosion)
t.start()
scoreFont = pygame.font.SysFont("impact", 20)
score = scoreFont.render("SCORE:" + str(colisionNumber), True, (192,192,192))
windowSurface.blit(score, (10,10))
Please note: at the start of the program I put:
global colisionNumber
colisionNumber = 0
This was done because if I defined colisionNumber within explosion, then colisionNumber would not be defined in the score variable.
Any help would be greatly appreciated.
colisionNumber = 0
def explosion():
global colisionNumber
colisionNumber += 1

Categories

Resources