GAMEBOX CS 1111 NOVEMBER 2019
GAMEBOX • https://cs1110.cs.virginia.edu/gamebox-summary.html • Save gamebox.py into every folder that uses gamebox • Gamebox is a simple game library built on top of Pygame • It supports • Animation • Collision Detection • Simply physics • More
IMPORT STATEMENTS • Always start with: import pygame import gamebox camera = gamebox.Camera(800, 600) # you can change these # dimensions to change the window size
CLICKING_EXAMPLE.PY • http://cs1110.cs.virginia.edu/files/cs1111_19fa/clicking_example.py • Let’s take a look at some of the mechanisms:
TIME LOOP • At the core of gamebox is the passage of time: gamebox.timer_loop(30, tick) • This tells gamebox to call the function “ tick ” 30 times per second. def tick(keys): • This defines the tick function to take in any pressed keys
TIME LOOP if pygame.K_UP in keys: • This detects if the “UP” arrow is being hit on the keyboard. • List of keys: https://www.pygame.org/docs/ref/key.html if camera.mouseclick: • Detects if the mouse is pressed (could be true several times in a row) camera.draw(logo) • Draws the logo as it’s current stored location camera.display() • Draws everything the camera “drew”
CAMERA Point of Origin (0,0) Y increases X increases
GAMEBOXES • A gamebox is a rectangle • You can draw images or color the rectangles • They can move • They support collision detection • They can change appearance • See 2. gameboxes on http://cs1110.cs.virginia.edu/gamebox-summary.html
GAMEBOXES MOVEMENT b1 = gamebox.from_color(50, 100, "red", 20, 40) # x, y, color, width, height b1.move(5, 6) b1.x -= 5 b1.center(50, 100)
EXAMPLES – SIMPLE ANIMATION • Starfield – Simple starfield animation https://cs1110.cs.virginia.edu/files/cs1111_19fa/starfield.py • Gangnam Style – Sprite images https://cs1110.cs.virginia.edu/files/cs1111_19fa/animation.py • Timer https://cs1110.cs.virginia.edu/files/cs1111_19fa/pygame_demo.py
COLLISION DETECTION print(b1.touches(b2)) • If b1 touches b2, return true print(b1.bottom_touches(b2)) • If the bottom of b1 touches b2, return true b1.move_to_stop_overlapping(b2) b1.move_both_to_stop_overlapping(b2)
EXAMPLES • Arrow-Keys: [movement, images, keyboard controls] https://cs1110.cs.virginia.edu/files/cs1111_19fa/arrow_keys.py • Infinite_jumper: [movement, jumping, collision detection, panning] https://cs1110.cs.virginia.edu/files/cs1111_19fa/infinite_jumper.py • Healthbar: [rudimentary AI, health bar, game_over screen] https://cs1110.cs.virginia.edu/files/cs1111_19fa/health_bar.py • Basic Carry Demo (Carrying an item) https://cs1110.cs.virginia.edu/files/cs1111_19fa/basic_carry_stuff.py
Recommend
More recommend