|
| 1 | +# Tested with version: 5.5.0.2 |
| 2 | +# by @Lightnet |
| 3 | + |
| 4 | +from pyray import * |
| 5 | + |
| 6 | + |
| 7 | +currentFrame = 0 |
| 8 | +framesCounter = 0 |
| 9 | +framesSpeed = 8 |
| 10 | +# Initialization |
| 11 | +SCREEN_WIDTH = 800 |
| 12 | +SCREEN_HEIGHT = 450 |
| 13 | + |
| 14 | +MAX_FRAME_SPEED = 15 |
| 15 | +MIN_FRAME_SPEED = 1 |
| 16 | + |
| 17 | +init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [texture] example - sprite anim") |
| 18 | + |
| 19 | +#need to set in case of animation snyc |
| 20 | +set_target_fps(60) # Set our game to run at 60 frames-per-second |
| 21 | + |
| 22 | +scarfy = load_texture("resources/scarfy.png") # Texture loading |
| 23 | + |
| 24 | +frameRec = Rectangle(0.0, 0.0, scarfy.width/6, scarfy.height) |
| 25 | + |
| 26 | +position = Vector2(350.0, 280.0) |
| 27 | + |
| 28 | +# Main game loop |
| 29 | +while not window_should_close(): # Detect window close button or ESC key |
| 30 | + |
| 31 | + framesCounter += 1 |
| 32 | + |
| 33 | + if framesCounter >= 60/framesSpeed: |
| 34 | + framesCounter = 0 |
| 35 | + currentFrame += 1 |
| 36 | + if currentFrame > 5: |
| 37 | + currentFrame = 0 |
| 38 | + |
| 39 | + frameRec.x = float(currentFrame) * float(scarfy.width/6) |
| 40 | + # Control speed animation |
| 41 | + if (is_key_pressed(KeyboardKey.KEY_RIGHT)): |
| 42 | + framesSpeed += 1 |
| 43 | + elif is_key_pressed(KeyboardKey.KEY_LEFT): |
| 44 | + framesSpeed -= 1 |
| 45 | + |
| 46 | + if framesSpeed > MAX_FRAME_SPEED: |
| 47 | + framesSpeed = MAX_FRAME_SPEED |
| 48 | + elif framesSpeed < MIN_FRAME_SPEED: |
| 49 | + framesSpeed = MIN_FRAME_SPEED |
| 50 | + |
| 51 | + begin_drawing() |
| 52 | + |
| 53 | + clear_background(RAYWHITE) |
| 54 | + #draw sheet block |
| 55 | + draw_rectangle_lines(15, 40, scarfy.width, scarfy.height, LIME) |
| 56 | + #draw current frame render |
| 57 | + draw_rectangle_lines(15 + int(frameRec.x), 40 + int(frameRec.y), int(frameRec.width), int(frameRec.height), RED) |
| 58 | + draw_text("FRAME SPEED: ", 165, 210, 10, DARKGRAY) |
| 59 | + draw_text(f" FPS {framesSpeed}", 575, 210, 10, DARKGRAY) #format string |
| 60 | + draw_text("PRESS RIGHT/LEFT KEYS to CHANGE SPEED!", 290, 240, 10, DARKGRAY) |
| 61 | + #display bar framesSpeed cap |
| 62 | + for i in range(MAX_FRAME_SPEED): |
| 63 | + if i < framesSpeed: |
| 64 | + draw_rectangle(250 + 21*i, 205, 20, 20, RED) |
| 65 | + draw_rectangle_lines(250 + 21*i, 205, 20, 20, MAROON) |
| 66 | + #draw sprite sheet texture |
| 67 | + draw_texture(scarfy, 15, 40, WHITE) |
| 68 | + #draw sprite animation |
| 69 | + draw_texture_rec(scarfy, frameRec, position,WHITE) |
| 70 | + |
| 71 | + draw_text("(c) Scarfy sprite by Eiden Marsal", SCREEN_WIDTH - 200, SCREEN_HEIGHT - 20, 10, GRAY) |
| 72 | + |
| 73 | + end_drawing() |
| 74 | + |
| 75 | +# De-Initialization |
| 76 | +unload_texture(scarfy) |
| 77 | + |
| 78 | +close_window() # Close window and OpenGL context |
0 commit comments