How to setup distance between two objects - python

A week ago i started learning python turtle, an i would like to ask somebody what should i do with setting distance between these objects. Three objects - i use goto method with random but these objects is setting very close to each other. I know something about the method distance, but im not always successful with it.
Thanx for every help
import turtle
import time
import random
my_space = turtle.Screen()
my_space.setup(width=1200, height=700)
my_space.title("Dula´s Ford Game")
my_space.addshape("ford.gif")
my_space.tracer(0)
ford = turtle.Turtle()
ford.speed(0)
ford.penup()
ford.goto(-400,0)
ford.shape("ford.gif")
ford.direction = "stop"
car1 = turtle.Turtle("circle")
car2 = turtle.Turtle("turtle")
car3 = turtle.Turtle()
car_list = [car1,car2,car3]
for car in car_list:
car.penup()
car.speed(0)
other_car_x = 400
other_car_y = random.randint(-100,100)
car.goto(other_car_x,other_car_y)
car.seth(180)
car.fd(10)
def right():
rside = ford.xcor()
ford.setx(rside+20)
if rside > 500:
ford.setx(rside)
else:
ford.setx(rside+20)
def left():
lside = ford.xcor()
if lside < -500:
ford.setx(lside)
else:
ford.setx(lside-20)
def up():
upside = ford.ycor()
if upside > 100:
ford.sety(upside)
else:
ford.sety(upside+20)
def down():
downside = ford.ycor()
if downside < -100:
ford.sety(downside)
else:
ford.sety(downside-20)
def start():
ford.direction = "start"
my_space.listen()
my_space.onkeypress(start,key="space")
my_space.onkeypress(right,key="Right")
my_space.onkeypress(left,key="Left")
my_space.onkeypress(up,key="Up")
my_space.onkeypress(down,key="Down")
while True:
my_space.update()
if ford.direction == "start":
for car in range(0,random.randint(1,len(car_list))):
speedy = random.randint(15,50)
enemy = car_list[car]
enemy.speed(speedy)
enemy.fd(speedy)
if enemy.xcor()<-500:
enemy.hideturtle()
enemy.goto(400,random.randint(-100,100))
if enemy.distance(enemy) < 30:
enemy.goto(400,random.randint(-100,100))
enemy.showturtle()
time.sleep(0.1)
my_space.exitonclick()

Related

Player on Player collision in pygame

I'm trying to create a collision detection between 4 controllable characters on an RPG battle map. Here is the function I'm using
def player_collission(Lord_x,Lord_y,Journeyman_x,Journeyman_y,Archer_x,Archer_y,
Cleric_x,Cleric_y):
print("Running")
if abs(Lord_x - Journeyman_x) <= 0 and abs(Lord_y - Journeyman_y) <= 0:
print("Colission detected")
return True
elif abs(Lord_x - Archer_x) <= 0 and abs(Lord_y - Archer_y) <= 0:
print("Colission detected")
return True
elif abs(Lord_x - Cleric_x) <= 0 and abs(Lord_y == Cleric_y) <= 0:
print("Colission detected")
return True
elif abs(Journeyman_x - Archer_x) <= 0 and abs(Journeyman_y - Archer_y) <= 0:
print("Colission detected")
return True
elif abs(Journeyman_x - Cleric_x) <= 0 and abs(Journeyman_y - Cleric_y) <= 0:
print("Colission detected")
return True
elif abs(Archer_x - Cleric_x) <= 0 and abs(Archer_y == Cleric_y) <= 0:
print("Colission detected")
return True
else:
return False #I didnt use classes so it has alot of if statements
if player_up:
p_collide = player_collission(Lord_x,Lord_y,Journeyman_x,Journeyman_y,Archer_x,Archer_y,
Cleric_x,Cleric_y)
if current_player == "lord":
if p_collide != True:
Lord_y -= tile_increment
if Lord_y <= 0:
Lord_y = 50
What happens is that the characters still move into each other but it detects the collision after it has already moved into each other and freezes all movement. I'm not sure how to re arrange it to make it work properly.
You detect collision when it has already happened. This is why you see characters overlapping.
Instead, you should detect if a collision is going to happen, an prevent a motion that would lead to that.
An example:
def move(coords, velocity):
"""Moves coords according to velocity."""
x, y = coords # Unpack a 2-tuple.
vx, vy = velocity
return (x + vx, y + vy)
tom_coords = (0, 0) # Tom is in the corner.
tom_v = (1, 1) # Tom moves by diagonal.
jerry_coords = (5, 0) # Jerry is a bit away from Tom.
jerry_v = (0, 1) # Jerry moves vertically.
while True:
new_tom_coords = move(tom_coords, tom_v) # Tom moves without fear.
new_jerry_coords = move(jerry_coords, jerry_v)
if new_jerry_coords == new_tom_coords: # Would be a collision!
new_jerry_coords = jerry_coords # Back to previous tile.
vx, vy = jerry_v
jerry_v = (-vx, -vy) # Jerry runs back.
print("Collision imminent, Jerry runs away!")
else:
jerry_coords = new_jerry_coords # Only update if no collision.
# Could also check collisions with walls, etc.
tom_coords = new_tom_coords
# Not inside pygame, so just print it and wait for a key press.
print('Tom:', tom_coords, 'Jerry:', jerry_coords)
input("Enter to continue, Ctrl+C to stop ")
Run it in and see how Tom and Jerry come close to each other but never occupy the same tile.

Pyglet game movement lagging behind

So, my snake makes a continuous movement, but if I press any key it goes back in time and lags back and forward. Here is a video: https://youtu.be/KCesu5bGiS8
My guess would be to update the key input faster, but when I do that everything updates faster, so the snake goes faster etc.
Code (as requested in text form) here:
import pyglet
import random
pyglet.resource.path = ["resources"]
pyglet.resource.reindex()
# sets the resource path
class Snake_Window(pyglet.window.Window):
a = 0
dtx = 160
dty = 200
# sets the basic direction and snake body x and y
def __init__(self):
super(Snake_Window, self).__init__(width=1280, height=720)
# sets resolution and inherits
self.key_handler = pyglet.window.key.KeyStateHandler()
self.push_handlers(self.key_handler)
# sets keys
self.set_caption("Wild Snake")
# gives it a name
self.background_image = pyglet.resource.image("background.png")
self.food_image = pyglet.resource.image("food.png")
self.snake_head_image = pyglet.resource.image("snake_head.png")
self.snake_body_image = pyglet.resource.image("snake_body.png")
# makes images usable
self.center_image(self.food_image)
self.center_image(self.snake_head_image)
self.center_image(self.snake_body_image)
# centers the images using center_image
self.snake_head = pyglet.sprite.Sprite(img=self.snake_head_image, x=200, y=200)
self.snake_head.scale = 0.1
self.snake_head.rotation = 270
# sets snake_head as a image on screen
self.snake_body = pyglet.sprite.Sprite(img=self.snake_body_image, x=self.dtx, y=self.dty)
self.snake_body.scale = 0.1
self.snake_body.rotation = 90
# sets snake_body as a image on screen
self.background = pyglet.sprite.Sprite(img=self.background_image, x=0, y=0)
# sets background as a image on screen
self.food = []
# sets food
pyglet.clock.schedule_interval(self.game_tick, 0.1)
def center_image(self, image):
# sets the center of the image to the actual center
image.anchor_x = image.width / 2
image.anchor_y = image.height / 2
def update_snake_head(self):
# makes the snake head go and sets the x and y for the body
if self.a == 0:
self.snake_head.x += 40
self.dtx = self.snake_head.x - 40
self.dty = self.snake_head.y
elif self.a == 1:
self.snake_head.x -= 40
self.dtx = self.snake_head.x + 40
self.dty = self.snake_head.y
elif self.a == 2:
self.snake_head.y += 40
self.dty = self.snake_head.y - 40
self.dtx = self.snake_head.x
elif self.a == 3:
self.snake_head.y -= 40
self.dty = self.snake_head.y + 40
self.dtx = self.snake_head.x
def update_snake_body(self, dtx, dty):
# makes the snakes body go
self.snake_body.x = dtx
self.snake_body.y = dty
def game_tick(self, dt):
# updates snakes head, snakes body, key presses and sets the background
self.update_snake_head()
self.update_snake_body(self.dtx, self.dty)
self.draw_elements()
self.key_press()
print(dt)
def draw_elements(self):
# draws everything in window
self.clear()
self.background.draw()
self.snake_head.draw()
self.snake_body.draw()
def key_press(self):
# sets direction of snake upon key press and rotates his head accordingly
if self.key_handler[pyglet.window.key.RIGHT]:
if self.a == 1:
pass
else:
self.a = 0
self.snake_head.rotation = 270
elif self.key_handler[pyglet.window.key.LEFT]:
if self.a == 0:
pass
else:
self.a = 1
self.snake_head.rotation = 90
elif self.key_handler[pyglet.window.key.UP]:
if self.a == 3:
pass
else:
self.a = 2
self.snake_head.rotation = 180
elif self.key_handler[pyglet.window.key.DOWN]:
if self.a == 2:
pass
else:
self.a = 3
self.snake_head.rotation = 0
game_window = Snake_Window()
pyglet.app.run()
Converting all my comments into a answer instead. It won't solve your problem completely. But due to lack of time, I'll leave something useful at least that almost solves it.
The reason for these are a couple. One of them is that you use a scheduler to render stuff instead of using the built-in on_draw event. Can't say for sure, but a good guess is that the graphical buffer gets updated/flipped automatically in on_draw while you're doing your drawing and stuff in a side-chained render function. So moving all the rendering stuff into on_draw makes sense.
Another issue is that you don't trigger on the actual key press, but instead you need to time the key press to each tick in the scheduler you got going - which you also have mushed into the rendering function. Essentially you're doing eventhandling+rendering+updating+IO in one cluster*** of a function, heh. Instead, you should rely on on_key_press for keyboard events.
Lastly, you're doing math operations all over the place - any of which might be half way done when you're doing the actual rendering. That's why you might get ghosting or odd artifacts (some things aren't completely done updating positions etc).
But here is a almost working example of a few steps taken to get closer to what you want. If no one else (including you) haven't solved this by a few days I'll go ahead and re-write most of your code and point you in a few good directions (batches being one of them).
import pyglet
import random
pyglet.resource.path = ["resources"]
pyglet.resource.reindex()
# sets the resource path
class Snake_Window(pyglet.window.Window):
a = 0
dtx = 160
dty = 200
# sets the basic direction and snake body x and y
def __init__(self):
super(Snake_Window, self).__init__(width=1280, height=720)
# sets resolution and inherits
self.key_handler = pyglet.window.key.KeyStateHandler()
self.push_handlers(self.key_handler)
# sets keys
self.set_caption("Wild Snake")
# gives it a name
self.background_image = pyglet.resource.image("background.png")
self.food_image = pyglet.resource.image("food.png")
self.snake_head_image = pyglet.resource.image("snake_head.png")
self.snake_body_image = pyglet.resource.image("snake_body.png")
# makes images usable
self.center_image(self.food_image)
self.center_image(self.snake_head_image)
self.center_image(self.snake_body_image)
# centers the images using center_image
self.snake_head = pyglet.sprite.Sprite(img=self.snake_head_image, x=200, y=200)
self.snake_head.rotation = 270
# sets snake_head as a image on screen
self.snake_body = pyglet.sprite.Sprite(img=self.snake_body_image, x=self.dtx, y=self.dty)
self.snake_body.scale = 0.1
self.snake_body.rotation = 90
# sets snake_body as a image on screen
self.background = pyglet.sprite.Sprite(img=self.background_image, x=0, y=0)
# sets background as a image on screen
self.food = []
# sets food
pyglet.clock.schedule_interval(self.game_tick, 0.1)
def on_draw(self):
self.draw_elements()
def center_image(self, image):
# sets the center of the image to the actual center
image.anchor_x = image.width / 2
image.anchor_y = image.height / 2
def update_snake_head(self):
# makes the snake head go and sets the x and y for the body
if self.a == 0:
self.snake_head.x += 40
self.dtx = self.snake_head.x - 40
self.dty = self.snake_head.y
elif self.a == 1:
self.snake_head.x -= 40
self.dtx = self.snake_head.x + 40
self.dty = self.snake_head.y
elif self.a == 2:
self.snake_head.y += 40
self.dty = self.snake_head.y - 40
self.dtx = self.snake_head.x
elif self.a == 3:
self.snake_head.y -= 40
self.dty = self.snake_head.y + 40
self.dtx = self.snake_head.x
def update_snake_body(self, dtx, dty):
# makes the snakes body go
self.snake_body.x = dtx
self.snake_body.y = dty
def game_tick(self, dt):
# updates snakes head, snakes body, key presses and sets the background
self.update_snake_head()
self.update_snake_body(self.dtx, self.dty)
def draw_elements(self):
# draws everything in window
self.clear()
self.background.draw()
print('Head:', self.snake_head.x, self.snake_head.y, {0:'Right', 1:'Left', 2: 'Up', 3:'Down'}[self.a])
self.snake_head.draw()
self.snake_body.draw()
self.flip()
def on_key_press(self, symbol, modifier):
# sets direction of snake upon key press and rotates his head accordingly
if symbol == pyglet.window.key.ESCAPE:
pyglet.app.exit()
if symbol == pyglet.window.key.SPACE:
print('Here')
if symbol == pyglet.window.key.RIGHT:
if self.a == 1:
pass
else:
self.a = 0
self.snake_head.rotation = 270
elif symbol == pyglet.window.key.LEFT:
if self.a == 0:
pass
else:
self.a = 1
self.snake_head.rotation = 90
elif symbol == pyglet.window.key.UP:
if self.a == 3:
pass
else:
self.a = 2
self.snake_head.rotation = 180
elif symbol == pyglet.window.key.DOWN:
if self.a == 2:
pass
else:
self.a = 3
self.snake_head.rotation = 0
game_window = Snake_Window()
pyglet.app.run()
I'll leave you not only with this ish-working code, but a good advice.
Stop asking so many questions, and start learn ways to debug why things are happening the way they do. You're shooting in the dark right now, asking questions hoping someone will solve the problems for you so you can focus on the fun stuff - which is the game development itself.
But what you'll have a lot of use for later in life - is finding ways to debug, probe, understand and pinpoint why things are or aren't happening the way you want.
Put some print("moo") here and there, print some values, add logging/debugging all over the place until you get wiser. It's not always efficient, but it got me to this point with your code.

How to Make two separate object match rotations in Pygame

I am making an asteroid remake of sorts, on of the big things in asteroids is the fact that the bullets rotate to match the objects direction. I have managed to make the bullets come out of the ship, nut it stays the same upright rotation. I would Like to make the bullet match rotation with the ship, so it looks like the bullets are coming out of the ship. My code is long unfortunately, but I do not know what to take out to make it more compact. I just tried to add comments and hope I guess. Any help is greatly appreciated. The vast majority of the actions happens in the 'MOUSEBUTTONDOWN" code. I have a 'rotate" function for the ship already, I'm thinking maybe we have to just use that, but I am unable to implement it.
The Image for The Ship
Image for the bullet I Want to rotate
import pygame as game
import pygame.math as math
import random as r
'''
Here I defined things
'''
game.init()
game.display.set_caption("Asteroids")
screen = game.display.set_mode([800,600])
gameon=True
bgcolor = game.color.Color("#FFFFFF")
ship = game.image.load("ship.png")
ship = game.transform.scale(ship, (50, 50))
rect = ship.get_rect()
rect.x=400
rect.y=400
shipdir = 0
newship = ship
auto = False
arrow = game.image.load("bullet.png")
shiphitbox = ship.get_rect()
shiphitbox.x = 100
shiphitbox.y = 100
arrows=[]
#code to rotate the ship
def rotateship(shipdir):
newship = game.transform.rotate(ship, shipdir)
newrect = rect.copy()
newrect.center = newship.get_rect().center
return newship.subsurface(newrect).copy()
def getmove():
#returns x-y movement based on directrion facing
global shipdir
d=shipdir
if d >= 349 or d < 11:
return [0, -2]
elif d >=11 and d < 34:
return [-1,-2]
elif d >=34 and d < 56:
return [-2,-2] #
elif d >=56 and d < 79:
return [-2,-1]
elif d >=79 and d < 102:
return [-2,0]
elif d >=102 and d < 125:
return [-2,1]
elif d >=125 and d < 147:
return [-2,2] #
elif d >=147 and d < 170:
return [-1,2]
elif d >=170 and d < 191:
return [0,2]
elif d >=191 and d < 214:
return [1,2]
elif d >=214 and d < 237:
return [2,2] #
elif d >=237 and d < 260:
return [2,1]
elif d >=260 and d < 282:
return [2,0]
elif d >=282 and d < 305:
return [2,-1]
elif d >=305 and d < 328:
return [2,-2] #
elif d >=328 and d < 349:
return [1,-2]
##main loop of the game
while gameon:
screen.fill(bgcolor)
screen.blit(newship,(rect.x,rect.y))
key = game.key.get_pressed()
event=game.event.poll()
#controls
if key[game.K_a]:
if shipdir==0: shipdir=360
shipdir -= 1
newship = rotateship(shipdir)
if key[game.K_d]:
if shipdir==360: shipdir=0
shipdir += 1
newship = rotateship(shipdir)
if auto==True or key[game.K_SPACE] or key[game.K_w] :
move = getmove()
rect.x += move[0]
rect.y += move[1]
game.time.delay(5)
#shooting
if event.type==game.MOUSEBUTTONDOWN:
'''
This is where the shooting happens
'''
arrowbox = arrow.get_rect()
arrowbox.x = rect.x;
arrowbox.y = rect.y;
move = getmove()
data = [arrowbox, move[0], move[1]]
arrows.append(data)
if event.type==game.QUIT:
gameon=False;
#spawning projectiles
for bullet in arrows:
bullet[0].x += bullet[1]
bullet[0].y += bullet[2]
if bullet[0].x > 700 or bullet[0].x<0:
arrows.remove(bullet)
for bullet in arrows:
screen.blit(arrow,(bullet[0].x,bullet[0].y))
game.display.flip() #redraws / refreshes screen
##comes here when game is over
while True:
screen.fill(bgcolor)
game.display.flip()
event=game.event.poll()
if event.type == game.QUIT:
break
game.quit()
I would Like to make the bullet match rotation with the ship, so it looks like the bullets are coming out of the ship.
You have to know the angle of rotation for each separate bullet. Add the angle of rotation to the bullet data:
data = (arrowbox, move[0], move[1], shipdir)
arrows.append(data)
Create rotated bullets by pygame.transform.rotate and blit it:
for bullet in arrows:
rotBullet = pygame.transform.rotate(arrow, bullet[3])
screen.blit(rotBullet, (bullet[0].x, bullet[0].y))

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.

Can't make walking animations in pygame

I was trying to code a game in pygame, but then when I tried to make a walking animation it only displayed one of the sprites.
def go_left(time):
ness_current = 1
global ness
global is_walking_left
ness_list = [ness_walking,ness_standing]
current_time = pygame.time.get_ticks()
go_left.walking_steps = 1
now = 0
cooldown = 1000
flag = 0
ness = ness_list[ness_current]
print current_time - game_loop.animation_timer
if (current_time - game_loop.animation_timer) > 200:
print 'Changing'
if ness_current == 0:
print 'Changing to sprite 1'
now = pygame.time.get_ticks()
ness_current = 1
current_time = now
elif ness_current == 1:
print 'Changing to sprite 0'
if (current_time - game_loop.animation_timer) > 200:
ness_current = 0
current_time = now
else:
'Changing to sprite 0 because of sprite reset'
ness_current = 0
current_time = now
def stop_it():
global ness
ness = pygame.image.load('nessthekid.png').convert()
ness.set_colorkey(WHITE)
car_list = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
player_list = pygame.sprite.Group()
When I try to use this it only displays one of the sprites not the other for the character. I want it to swich every 1 or 2 seconds to make it look like its walking. Help is appreciated.
First, I highly suggest using a class, to avoid using global variables.
Second, as you set the current time back to now (0), you will make it so current_time - game_loop.animation_timer will always be negative. This will keep the statement from running.
For now, I suggest completely removing the "if (current_time - game_loop.animation_timer) > 200:" from your code.
Here is an example to get you started (obviously you will have to alter it to make it work for you)
class Ness:
def __init__(self):
self.all_images = [ness_walking,ness_standing]
self.current = 0
self.image = self.all_images[self.current]
def walk_left(self):
# Every 200 clicks
if pygame.time.get_ticks() % 200 == 0:
if self.current == 0:
self.current = 1
else:
self.current = 0
self.image = self.all_images[self.current]
As suggested by #Red Twoon put the stuff separated in class is a very good practice. Another thing that you should do is no rely in get_ticks directly but instead use some kind of time independent movement/animation.
You can achieve this using delta times in your game loop.
Game loop.
while(True):
delta = #Get the delta time.
handle_events();
update(delta);
draw(delta);
#Animation stuff.
time_to_animate_frame = 200;
time_since_last_update = 0;
...
time_since_last_update += delta;
if(time_since_last_update > time_to_animate_frame):
time_since_last_update - time_to_animate_frame;
#Do the animation.....

Categories

Resources