How to draw 10000+ 2d graphical objects quickly? - python

I need to draw tens of thousands, in the future probably hundreds of thousands of simple 2d objects (circles, rectangles, some filled, labeled...) to a widget.
In the same program I need GUI widgets (buttons, text input, checkboxes).
I tried Gtk, Qt and SDL with C++ and Python. First result was to be expected: C++ and Python show the same performance, as they call the same C or C++ routines in the backend.
Second result is that none of the libraries made a big difference. In numbers: 22500 rectangles (150*150) needed approximately a second to update. As there will be constant upadating due to (a) new data, i.e. more rectangles, and (b) user interaction, i.e. zooming, panning etc., a second is way to long!
What would be a faster way. Small examples are very much appreciated. Python and C++ is good. Other libraries should be easily accessible and installable on Linux.
Maybe I am just doing it wrong.
ps. I am not posting my test codes, because I don't want to bias answers. And I don't want my code to be corrected, I want to the fastest way to do it...
edit:
Alright, I will add my gtk test:
#!/bin/env python2
import gtk
import gobject
import gtk.gdk
class GraphWidget(gtk.DrawingArea):
__gsignals__ = {
'expose-event': 'override',
'clicked' : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_STRING, gtk.gdk.Event))
}
def __init__(self,window):
gtk.DrawingArea.__init__(self)
#self.win = window
self.zoom_ratio = 1.0
self.dx = 40
self.dy = 40
def do_expose_event(self, event):
cr = self.window.cairo_create()
cr.set_source_rgba(1.0, 0.9, 0.8, 1.0)
cr.paint()
cr.translate(self.dx, self.dy)
cr.scale(self.zoom_ratio,self.zoom_ratio)
self.draw(cr)
def draw(self, cr):
n = 150
cr.set_source_rgba(0.,1.,1.,1.0)
for i in range(n):
for j in range(n):
cr.arc(i*30, j*30, 10, 0, 6.2832)
cr.close_path()
cr.fill()
cr.set_source_rgba(0.,0.,1.,1.0)
for i in range(n):
for j in range(n):
cr.arc(i*30, j*30, 10, 0, 6.2832)
cr.move_to(i*30-10, j*30)
cr.show_text("hu")
cr.stroke()
def on_zoom(self, zoom_factor):
self.zoom_ratio *= zoom_factor
self.queue_draw()
def on_translate(self,dx,dy):
self.dx += dx
self.dy += dy
self.queue_draw()
class TestWindow(gtk.Window):
def __init__(self):
gtk.Window.__init__(self)
self.widget = GraphWidget(self)
self.add(self.widget)
self.show_all()
# connect key press events
self.connect('key-press-event', self.on_key_press_event)
self.connect('destroy', gtk.main_quit)
self.widget.queue_draw()
def on_key_press_event(self, widget, event):
if event.keyval == gtk.keysyms.space and not (event.state & gtk.gdk.CONTROL_MASK):
self.on_run(widget)
return True
elif event.keyval == gtk.keysyms.r:
self.on_refresh(widget)
return True
elif event.keyval == gtk.keysyms.Left:
self.widget.on_translate(-100, 0)
elif event.keyval == gtk.keysyms.Right:
self.widget.on_translate(100, 0)
elif event.keyval == gtk.keysyms.Up:
self.widget.on_translate(0, -100)
elif event.keyval == gtk.keysyms.Down:
self.widget.on_translate(0, 100)
elif event.keyval == gtk.keysyms.Page_Down:
self.widget.on_zoom(0.7)
elif event.keyval == gtk.keysyms.Page_Up:
self.widget.on_zoom(1.3)
if __name__ == '__main__':
win = TestWindow()
gtk.main()
And the SDL experiment:
#!/usr/bin/env python
import sdl2
import sdl2.ext as sdl2ext
dx = 0
dy = 0
zoom_factor = 1.
n_objects = 150
sdl2ext.init()
window = sdl2ext.Window('hallo',
size=(800, 600),
flags= sdl2.SDL_WINDOW_RESIZABLE)
window.show()
renderer = sdl2ext.RenderContext(window)
renderer.color = sdl2ext.Color(255,155,25)
def draw():
renderer.clear()
for i in xrange(n_objects):
for j in xrange(n_objects):
renderer.fill([int((i*30+dx)*zoom_factor),
int((j*30+dy)*zoom_factor),
int(20*zoom_factor),
int(20*zoom_factor)],
sdl2ext.Color(255,25,55))
renderer.draw_rect([int((i*30+dx)*zoom_factor),
int((j*30+dy)*zoom_factor),
int(20*zoom_factor),
int(20*zoom_factor)],
sdl2ext.Color(255,255,255))
renderer.present()
draw()
running = True
while running:
for e in sdl2ext.get_events():
if e.type == sdl2.SDL_QUIT:
running = False
break
if e.type == sdl2.SDL_KEYDOWN:
if e.key.keysym.sym == sdl2.SDLK_ESCAPE:
running = False
break
elif e.key.keysym.sym == sdl2.SDLK_RIGHT:
dx += 50
draw()
elif e.key.keysym.sym == sdl2.SDLK_LEFT:
dx -= 50
draw()
elif e.key.keysym.sym == sdl2.SDLK_UP:
dy += 50
draw()
elif e.key.keysym.sym == sdl2.SDLK_DOWN:
dy -= 50
draw()
elif e.key.keysym.sym == sdl2.SDLK_PAGEUP:
zoom_factor *= 1.2
draw()
elif e.key.keysym.sym == sdl2.SDLK_PAGEDOWN:
zoom_factor /= 1.2
draw()
The Qt test was done by my colleague so I don't have the code right now...

Looks like you're drawing every single object regardless of whether or not it's in the viewable area. Have you considered storing an array of the positions of each object, and then test each to determine if it's in the screen's viewable area before drawing it?
I did a quick and dirty test to try it out (the code could be much cleaner, I'm sure), and it's much more responsive drawing only what's visible:
First I created some variables to determine the visible boundary (you would update the boundaries every time move and zoom, window resize, etc happen):
boundX = [-60, 160]
boundY = [-60, 160]
In the draw event, I'm checking if the position is within the boundry before drawing. I also consolidated your loop to increase efficiency, you can draw and add text in the same iteration. Even testing this with n = 50000 is more responsive than what was there before.
def draw(self, cr):
n = 150
for i in range(n):
if min(boundX) < i * 30 < max(boundX):
for j in range(n):
if min(boundY) < j * 30 < max(boundY):
cr.set_source_rgba(0.,1.,1.,1.0)
cr.arc(i*30, j*30, 10, 0, 6.2832)
cr.close_path()
cr.fill()
cr.set_source_rgba(0.,0.,1.,1.0)
cr.arc(i*30, j*30, 10, 0, 6.2832)
cr.move_to(i*30-10, j*30)
cr.show_text("hu")
cr.stroke()
And in the keypress events, just increment and decrement the boundry by the amount the widget is being translated:
def on_key_press_event(self, widget, event):
if event.keyval == gtk.keysyms.space and not (event.state & gtk.gdk.CONTROL_MASK):
self.on_run(widget)
return True
elif event.keyval == gtk.keysyms.r:
self.on_refresh(widget)
return True
elif event.keyval == gtk.keysyms.Left:
boundX[0] += 100
boundX[1] += 100
self.widget.on_translate(-100, 0)
elif event.keyval == gtk.keysyms.Right:
boundX[0] -= 100
boundX[1] -= 100
self.widget.on_translate(100, 0)
elif event.keyval == gtk.keysyms.Up:
boundY[0] += 100
boundY[1] += 100
self.widget.on_translate(0, -100)
elif event.keyval == gtk.keysyms.Down:
boundY[0] -= 100
boundY[1] -= 100
self.widget.on_translate(0, 100)
elif event.keyval == gtk.keysyms.Page_Down:
self.widget.on_zoom(0.7)
elif event.keyval == gtk.keysyms.Page_Up:
self.widget.on_zoom(1.3)

Related

Trying to make an object jump using tkinter

from tkinter import *
window = Tk()
canvas = Canvas(window, width=1280, height=720)
canvas.pack()
a = (0, 50), (50, 100) # coordinates of the rectangle
rect = canvas.create_rectangle(a, fill="red")
#rect = canvas.create_oval(a, fill="red")
speed = 5 # speed of the rectangle
jump = False
def keypress(event):
x = 0
y = 0
if event.char == "a": x-= speed
elif event.char == "d": x+= speed
elif event.char == "w": y-= speed
elif event.char == "s": y+= speed
elif event.char == " ":
y = 50
jump = True
while jump:
y = 0
jump = False
canvas.move(rect, x, y)
canvas.update()
canvas.after(1)
window.bind("<Key>", keypress)
window.mainloop()
This is my code, im trying to make the rectangle jump but whenever i add the jump code, everything stops working even the normal "asdw" movement that would work otherwise
In case you just want to do a simple jumping animation where the rectangle goes up and down again, just use physics: Define a gravitational pull and an initial vertical velocity and then loop until the ground is reached again:
elif event.char == " ":
diff = 0 ## Difference to initial level
y = -3 ## Initial speed in y direction
grav = .1 ## Gravitation
while diff >= 0: ## While it is still jumping (higher than initially)
canvas.move(rect, x, y)
canvas.update()
sleep(.01) ## Pause for 1/100 second
diff-=y ## Update current jumping height
y+=grav ## Update the speed in y direction
y = 0 ## Just so it is not moved again, afterwards
To make this code snippet work, you have to import time.sleep at the beginning of the program:
from time import sleep

Arrow keys input with graphics.py doesn't work as intended

I've just started learning Python (as of now the only other programming language I'm fluent in is C, so this is all very new to me), and I've decided to make some simple game with it just to get used to it. I'd like it to be an arrow keys controlled game and I'm using the graphics.py library which to my understanding is very beginner friendly.
This is what I came up with:
from graphics import *
win = GraphWin("Game", 800, 500)
def getXInput():
inputBuffer = win.checkKey()
inputResult = 0
if inputBuffer == "Right":
inputResult = 1
elif inputBuffer == "Left":
inputResult = -1
return inputResult
def getYInput():
inputBuffer = win.checkKey()
inputResult = 0
if inputBuffer == "Down":
inputResult = 1
elif inputBuffer == "Up":
inputResult = -1
return inputResult
class player:
def __init__(self):
self.x = 400
self.y = 250
self.speed = 3
self.canMove = True
self.txt = Text(Point(self.x, self.y), "test")
self.txt.draw(win)
def move(self):
while self.canMove == True:
xMove = getXInput() * self.speed
yMove = getYInput() * self.speed
self.txt.move(xMove, yMove)
def main():
pl = player()
pl.move()
win.close()
main()
Player input is managed through the getXInput and getYInput functions, which costantly checks if the arrow keys are pressed and return -1, 0 or 1 accordingly.
Then the result of those functions is costantly multiplied for the player's attribute "speed" and that's how the game is able to move the player character in the correct direction. This kinda works: it works perfectly on the x axis, but it acts weird on y axis, registering inputs only once in a while. Putting the yMove assignment before the xMove one within the move method reverses the problem, making vertical movement perfect and horizontal movement very sloppy. I really can't understand what's causing this, can anybody help?

Swinging pendulum does not work

I am having a little trouble with this project. I have to create a pendulum using key handles and the code I have for the key's up and down don't seem to be working. "up" is suppose to make the pendulum go faster and "down" makes it go slower. This is the code that I have so far. can somebody please help.
from tkinter import * # Import tkinter
import math
width = 200
height = 200
pendulumRadius = 150
ballRadius = 10
leftAngle = 120
rightAngle = 60
class MainGUI:
def __init__(self):
self.window = Tk() # Create a window, we may call it root, parent, etc
self.window.title("Pendulum") # Set a title
self.canvas = Canvas(self.window, bg = "white",
width = width, height = height)
self.canvas.pack()
self.angle = leftAngle # Start from leftAngle
self.angleDelta = -1 # Swing interval
self.delay = 200
self.window.bind("<Key>",self.key)
self.displayPendulum()
self.done = False
while not self.done:
self.canvas.delete("pendulum") # we used delete(ALL) in previous lab
# here we only delete pendulum object
# in displayPendulum we give the tag
# to the ovals and line (pendulum)
self.displayPendulum() # redraw
self.canvas.after(self.delay) # Sleep for 100 milliseconds
self.canvas.update() # Update canvas
self.window.mainloop() # Create an event loop
def displayPendulum(self):
x1 = width // 2;
y1 = 20;
if self.angle < rightAngle:
self.angleDelta = 1 # Swing to the left
elif self.angle > leftAngle:
self.angleDelta = -1 # Swing to the right
self.angle += self.angleDelta
x = x1 + pendulumRadius * math.cos(math.radians(self.angle))
y = y1 + pendulumRadius * math.sin(math.radians(self.angle))
self.canvas.create_line(x1, y1, x, y, fill="blue", tags = "pendulum")
self.canvas.create_oval(x1 - 2, y1 - 2, x1 + 2, y1 + 2,
fill = "red", tags = "pendulum")
self.canvas.create_oval(x - ballRadius, y - ballRadius,
x + ballRadius, y + ballRadius,
fill = "green", tags = "pendulum")
def key(self,event):
print(event.keysym)
print(self.delay)
if event.keysym == 'up':
print("up arrow key pressed, delay is",self.delay)
if self.delay >10:
self.delay -= 1
if event.keysym == 'Down':
print("Down arrow key pressed,delay is",self.delay)
if self.delay < 200:
self.delay += 1
if event.keysym=='q':
print ("press q")
self.done = True
self.window.destroy()
MainGUI()
The root of the problem is that the event keysym is "Up" but you are comparing it to the all-lowercase "up". Naturally, the comparison fails every time. Change the if statement to if event.keysym == 'Up':
Improving the animation
In case you're interested, there is a better way to do animation in tkinter than to write your own infinite loop. Tkinter already has an infinite loop running (mainloop), so you can take advantage of that.
Create a function that draws one frame, then have that function arrange for itself to be called again at some point in the future. Specifically, remove your entire "while" loop with a single call to displayFrame(), and then define displayFrame like this:
def drawFrame(self):
if not self.done:
self.canvas.delete("pendulum")
self.displayPendulum()
self.canvas.after(self.delay, self.drawFrame)
Improving the bindings
Generally speaking, your code will be marginally easier to manage and test if you have specific bindings for specific keys. So instead of a single catch-all function, you can have specific functions for each key.
Since your app supports the up key, the down key, and the "q" key, I recommend three bindings:
self.window.bind('<Up>', self.onUp)
self.window.bind('<Down>', self.onDown)
self.window.bind('<q>', self.quit)
You can then define each function to do exactly one thing:
def onUp(self, event):
if self.delay > 10:
self.delay -= 1
def onDown(self, event):
if self.delay < 200:
self.delay += 1
def onQuit(self, event):
self.done = True
self.window.destroy()

Strange things with tkinter's canvas.after()

Ive been having some issues where if the shot is fired multiple times before it leaves the canvas it will speed up each time it is redrawn until its faster than the canvas can update it. Ive attributed the problem to the canvas.after command because if I impliment this as a while loop using the time.sleep command it works fine (unfortunately I can use the implementation of the code because it works as two separate loops)
#imports
from tkinter import *
#The Game
class Game:
def __init__(self):
#creating the static canvas background
self.window = Tk()
self.window.title('Shoot your friends')
self.canvas = Canvas(width= 900,
height= 900,
cursor= 'circle')
self.canvas.pack()
self.canvas.create_line(450, 900,
450, 0,
dash = (10))
self.p1_ship = PhotoImage(file = "red_ship.gif")
self.p2_ship = PhotoImage(file = "blue_ship.gif")
self.p1_laser = PhotoImage(file = "red_laser.gif")
self.p2_laser = PhotoImage(file = "blue_laser.gif")
#Buttons at the bottom
self.frame = Frame(self.window)
self.frame.pack()
#Determining the state of edge teleporting and toggling it
self.etb = True
def et():
if self.etb == True:
self.etb = False
Et["text"] = "Turn On Edge Teleporting"
else:
self.etb = True
Et["text"] = "Turn Off Edge Teleporting"
print ("Edge Telepoting Toggled")
Et = Button(self.frame, text="Turn Off Edge Teleporting", command = et, cursor= 'double_arrow')
Et.grid(row=0,column=0)
self.Rfb = False
def rf():
if self.Rfb == True:
self.Rfb = False
Rf["text"] = "Turn On Rapid Fire "
else:
self.Rfb = True
Rf["text"] = "Turn Off Rapid Fire"
print ("Rapid Fire Toggled")
Rf = Button(self.frame, text="Turn On Rapid Fire", command = rf, cursor= 'cross')
Rf.grid(row=0,column=1)
def restart():
print ('restart')
restart_b = Button(self.frame, text="Restart Game", command = restart, fg='Blue', bg= 'red', cursor='exchange' )
restart_b.grid(row=0,column=2)
self.y_p1 = 400
self.y_p2 = 400
self.ship_p1 = self.canvas.create_image(40, 450, image=self.p1_ship)
self.ship_p2 = self.canvas.create_image(860, 450, image=self.p2_ship)
self.canvas.move(self.ship_p1,0,0)
self.canvas.move(self.ship_p2,0,0)
# Functions that handle movement of the ships taking into account multiple variables
#For example If edge teleporting is ON the ship will teleport to the top of the screen if it is at the bottom and the down key is pressed and vice versa
#My implementation of this may not be the most efficient but I like the options it gives me for adding future features and it looks cool.
def p1_up(event):
if self.etb == True and self.y_p1 >= 100:
self.canvas.move(self.ship_p1,0,-100)
self.y_p1 += -100
elif self.etb == True:
self.canvas.move(self.ship_p1,0,+800)
self.y_p1 += +800
elif self.y_p1 >= 100:
self.canvas.move(self.ship_p1,0,-100)
self.y_p1 += -100
def p1_down(event):
if self.etb == True and self.y_p1 <= 799:
self.canvas.move(self.ship_p1,0,+100)
self.y_p1 += 100
elif self.etb == True:
self.canvas.move(self.ship_p1,0,-800)
self.y_p1 += -800
elif self.y_p1 <= 799:
self.canvas.move(self.ship_p1,0,+100)
self.y_p1 += 100
def p2_up(event):
if self.etb == True and self.y_p2 >= 100:
self.canvas.move(self.ship_p2,0,-100)
self.y_p2 += -100
elif self.etb == True:
self.canvas.move(self.ship_p2,0,+800)
self.y_p2 += +800
elif self.y_p2 >= 100:
self.canvas.move(self.ship_p2,0,-100)
self.y_p2 += -100
def p2_down(event):
if self.etb == True and self.y_p2 <= 799:
self.canvas.move(self.ship_p2,0,+100)
self.y_p2 += 100
elif self.etb == True:
self.canvas.move(self.ship_p2,0,-800)
self.y_p2 += -800
elif self.y_p2 <= 799:
self.canvas.move(self.ship_p2,0,+100)
self.y_p2 += 100
# Functions for shooting
self.p1_shot_out = False
self.p2_shot_out = False
def p1_shoot(event):
if self.p1_shot_out == True:
self.canvas.delete(self.laser_p1)
#draws the laser
self.laser_p1 = self.canvas.create_image(50, self.y_p1 +50, image=self.p1_laser)
self.x_p1_laser = 50
self.p1_shot_out = True
self.window.after(1, p1_shoot_move)
def p1_shoot_move():
#moves the laser until its outside the canvas
if self.x_p1_laser >= 930:
pass
else:
self.canvas.move(self.laser_p1,5,0)
self.x_p1_laser += 5
self.canvas.update()
self.window.after(3, p1_shoot_move)
def p2_shoot(event):
if self.p2_shot_out == True:
self.canvas.delete(self.laser_p2)
#draws the laser
self.laser_p2 = self.canvas.create_image(750, self.y_p2 +50, image=self.p2_laser)
self.x_p2_laser = 750
self.p2_shot_out = True
self.window.after(4, p2_shoot_move)
def p2_shoot_move():
#moves the laser until its outside the canvas
if self.x_p2_laser <= -110:
pass
else:
self.canvas.move(self.laser_p2,-5,0)
self.x_p2_laser += -5
self.canvas.update()
self.window.after(4, p2_shoot_move)
# Key bindings that trigger their respective functions
self.canvas.bind('w', p1_up)
self.canvas.bind('s', p1_down)
self.canvas.bind('<Up>', p2_up)
self.canvas.bind('<Down>', p2_down)
self.canvas.bind('<space>', p1_shoot)
self.canvas.bind('<Control_R>', p2_shoot)
self.canvas.focus_set()
# this mainloop thing is some sort of witchcraft! OH MY!!!
self.window.mainloop()
Game()
The problem is that each time you "shoot", you call after. If you "shoot" five times in rapid succession, you'll have 5 "moves" queued up. Each adds 5 to the x location, and they all will run just a few ms apart, so the laser will appear to be moving very fast. The five will run in rapid succession, then 3ms later they will run in rapid succession again, and so on.
What you need to do is keep a reference to the thing you schedule with after, and cancel it before starting a new shot. That, or keep an independent record of the x/y coordinate for each shot if you want more than one shot being active at a time.
For example:
def p1_shoot(event):
....
# cancel any previous shot
if self.p1_job is not None:
self.window.after_cancel(self.p1_job)
self.p1_job = self.window.after(1, p1_shoot_move)
Of course, you need to set self.p1_job every time you call after, and you need to initialize it to None at the start, and when the bullet is destroyed.

Player moves correct speed across the screen, walk cycle is too fast, hoping for suggestions to do it properly

I'm jumping around tutorials trying to successfully complete my first game. I was taking a heavy object oriented approach for managing resources, but I felt that was clouding my understanding a bit, so I restarted. I currently have my character moving smoothly across the screen at a speed I like, my problem is my "walk cycle" of three images goes way to fast, and I get a persistence of vision effect on my little chicken's legs. I need to slow it down, so far I was able to achieve a desired effect by popping in a call to the clock after each decision loop, but I am worried that will slow down the entire game logic, as I am not sure if calling clock more than once is "freezing" my game update in time while the character decides to move. I had considered maybe making some type of equation comparing the time on the clock to the time on the clock before, to slow down the walk cycle key frames. Any help or suggestions, is there an easier method? Thanks a bunch.
import pygame, sys
from pygame.locals import *
pygame.init()
#Contstants
BLACK = (0, 0, 0)
SCREENWIDTH = 300
SCREENHEIGHT = 300
game_running = True
clock = pygame.time.Clock()
#variables
current = 0
class Player(object):
def __init__(self):
self.avatar_front = pygame.image.load("chicken_front.png")
self.avatar_back = pygame.image.load("chicken_back.png")
self.avatar_right = pygame.image.load("chicken_right_stand.png")
self.avatar_left = pygame.image.load("chicken_left_stand.png")
self.avatar_left_walk = pygame.image.load("chicken_left_walk1.png")
self.avatar_left_walk2 = pygame.image.load("chicken_left_walk2.png")
self.avatar_right = pygame.image.load("chicken_right_stand.png")
self.avatar_right_walk = pygame.image.load("chicken_right_walk1.png")
self.avatar_right_walk2 = pygame.image.load("chicken_right_walk2.png")
self.position = [0, 0]
self.current_direction = self.avatar_front
#SetUp
myScreen = pygame.display.set_mode((SCREENWIDTH, SCREENHEIGHT))
pygame.display.set_caption("Chicken Rush!")
pygame.display.set_icon(pygame.image.load("chicken_front.png"))
myPlayer = Player()
while game_running:
myScreen.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#RIGHT HERE DETERMINES WHICH IMAGE TO DISPLAY FOR WALKING LEFT->This is the part I need to # slow down
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_LEFT] and myPlayer.position[0] >= 0:
if current == 0:
myPlayer.current_direction = myPlayer.avatar_left
current += 1
elif current == 1:
myPlayer.current_direction = myPlayer.avatar_left_walk
current += 1
elif current == 2:
myPlayer.current_direction = myPlayer.avatar_left_walk2
current = 0
myPlayer.position[0] -= 3
if keys_pressed[K_RIGHT] and myPlayer.position[0] < SCREENWIDTH - 32:
myPlayer.position[0] += 3
if keys_pressed[K_UP] and myPlayer.position[1] >= 0:
myPlayer.position[1] -= 3
if keys_pressed[K_DOWN] and myPlayer.position[1] < SCREENHEIGHT - 35:
myPlayer.position[1] += 3
myScreen.blit(myPlayer.current_direction, (myPlayer.position[0], myPlayer.position[1]))
pygame.display.update()
clock.tick(28)
It seems like you could use a longer interval to slow down the image transitions. Keep in mind you'll need to play with the value of imageTransitionSpeed to get it to your liking, but something like this should help slow the transition down:
#RIGHT HERE DETERMINES WHICH IMAGE TO DISPLAY FOR WALKING LEFT->This is the part I need to # slow down
imageTransitionSpeed = 10
keys_pressed = pygame.key.get_pressed()
if keys_pressed[K_LEFT] and myPlayer.position[0] >= 0:
if current == 0:
myPlayer.current_direction = myPlayer.avatar_left
current += 1
elif current == imageTransitionSpeed:
myPlayer.current_direction = myPlayer.avatar_left_walk
current += 1
elif current == (imageTransitionSpeed * 2):
myPlayer.current_direction = myPlayer.avatar_left_walk2
current = 0
else:
current += 1
myPlayer.position[0] -= 3
if keys_pressed[K_RIGHT] and myPlayer.position[0] < SCREENWIDTH - 32:
myPlayer.position[0] += 3
if keys_pressed[K_UP] and myPlayer.position[1] >= 0:
myPlayer.position[1] -= 3
if keys_pressed[K_DOWN] and myPlayer.position[1] < SCREENHEIGHT - 35:
myPlayer.position[1] += 3
myScreen.blit(myPlayer.current_direction, (myPlayer.position[0], myPlayer.position[1]))
pygame.display.update()
clock.tick(28)

Categories

Resources