Skip to content

Commit 61db3e0

Browse files
committed
update projects
1 parent 36bbb1a commit 61db3e0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+579
-6
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
Sp19/*
22
Fa19/*
3-
Stephen_Fa18/*
43
rubric*
54
Sp20/*

ProjectTemplate.zip

25 KB
Binary file not shown.

README.md

+5-1

Stephen_Fa18/ProjectNotebook.ipynb

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Final Project: Sprite Animation"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Objective: \n",
15+
"Using the module pygame, the objective of this project is to animate an RPG character's movement across a background. \n",
16+
"\n",
17+
"\n",
18+
"A window is created as a display, with the background image uploaded to the display. The window's dimensions are created to fit the dimensions of the background image.\n",
19+
"\n",
20+
"The window is constantly redrawn (updated) to delete old frames and create new ones.\n",
21+
"\n",
22+
"The program draws in our player, and listens for key inputs (up, down, left, right) and responds by changing directional booleans to True or False, and moving our player in that direction. The booleans listen for key inputs, as they are later used to animate our player.\n",
23+
"\n",
24+
"This is accomplished by loading in 8 images for each direction (left, right,up and down) into a list and loading through the images throughout the duration of each boolean.\n",
25+
"\n",
26+
"The player can not walk off of the screen, as conditionals for movement create artificial borders by taking the size of the player, their steplength, and the size of the window into account.\n",
27+
"\n",
28+
"The FPS is set as 32, a multiple of 8 in order to match the number of images that loop. Each image should be displayed for 4 frames.\n",
29+
"\n",
30+
"## Controls:\n",
31+
"\n",
32+
"Control your player by using the up, down, left and right arrows!\n",
33+
"\n",
34+
"Thank you for a great quarter."
35+
]
36+
},
37+
{
38+
"cell_type": "code",
39+
"execution_count": 1,
40+
"metadata": {
41+
"scrolled": false
42+
},
43+
"outputs": [
44+
{
45+
"name": "stdout",
46+
"output_type": "stream",
47+
"text": [
48+
"pygame 1.9.4\n",
49+
"Hello from the pygame community. https://www.pygame.org/contribute.html\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"import pygame\n",
55+
"from my_module import functions as fn"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 2,
61+
"metadata": {
62+
"scrolled": true
63+
},
64+
"outputs": [
65+
{
66+
"ename": "IndexError",
67+
"evalue": "list index out of range",
68+
"output_type": "error",
69+
"traceback": [
70+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
71+
"\u001b[1;31mIndexError\u001b[0m Traceback (most recent call last)",
72+
"\u001b[1;32m<ipython-input-2-3409e42bb943>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 79\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 80\u001b[0m man.draw(win, char, walkright, # Calls to our animation function, inputting our window and lists of character\n\u001b[1;32m---> 81\u001b[1;33m walkleft, walkup, walkdown) # images to sequence.\n\u001b[0m\u001b[0;32m 82\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 83\u001b[0m fn.redraw_game_window(win, bg, dw_half, # Calls our function for updating the game window, inputting the window,\n",
73+
"\u001b[1;32m~\\Desktop\\COGS 18\\MyProjectFolder\\my_module\\functions.py\u001b[0m in \u001b[0;36mdraw\u001b[1;34m(self, win, char, walkright, walkleft, walkup, walkdown)\u001b[0m\n\u001b[0;32m 128\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 129\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mright\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m# If our character is moving right, loop across the list of images.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 130\u001b[1;33m \u001b[0mwin\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mblit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mwalkright\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwalkcount\u001b[0m\u001b[1;33m//\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# After selecting an image, print it at the character's current x and\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 131\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwalkcount\u001b[0m\u001b[1;33m+=\u001b[0m\u001b[1;36m1\u001b[0m \u001b[1;31m# y co-ordinate. Increment walkcount, and blit and image again while\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 132\u001b[0m \u001b[1;31m# right is true.\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
74+
"\u001b[1;31mIndexError\u001b[0m: list index out of range"
75+
]
76+
}
77+
],
78+
"source": [
79+
"pygame.init() # Initializes pygame\n",
80+
"pygame.display.set_caption(\"Sprite Run\") # This sets the title of our window to \"Sprite Run\"\n",
81+
"\n",
82+
"image_dir = 'images/rpg_character/' # The pathway for our character image, so that it is convenient\n",
83+
" # to load in our character images.\n",
84+
"\n",
85+
"bg = pygame.image.load('images/background.png') # Uses a method which loads an image from our directory, and sets\n",
86+
" # the image equal to a variable, in this case bg.\n",
87+
"\n",
88+
"image_size = bg.get_rect() # This gets the size of our image (bg) and provides the rectangular\n",
89+
"w_center = image_size.center[1] # dimensions of our image. Center gives us the middle of the image.\n",
90+
"h_center = image_size.center[0] \n",
91+
"dw = image_size[2] # We use the image dimensions to fit our screen to. (width) \n",
92+
"dh = image_size[3] # We use the image dimensions to fit our screen to. (height)\n",
93+
"\n",
94+
"dw_half = dw/2 # Half of the size of the screen\n",
95+
"dh_half = dh/2 \n",
96+
"win = pygame.display.set_mode((dw,dh)) # Creates a window (pop-up) with the dimensions dw,dh (x,y).\n",
97+
"clock = pygame.time.Clock() # Defines clock in order to set a framerate later.\n",
98+
"\n",
99+
"\n",
100+
"char = pygame.image.load(image_dir+'tile000.png') # Loads the image for our character when he is not moving.\n",
101+
"\n",
102+
"\n",
103+
"\n",
104+
"# Below, we use the pygame.image.load method to load in a sequence of images from our directory.\n",
105+
"# Each direction is walkright, walkleft, walkup and walkdown hold a list of 8 images, which are saved in the order of\n",
106+
"# each animation and looped through later for us to animate our sprite's movement.\n",
107+
"\n",
108+
"#################################################################################################\n",
109+
"\n",
110+
"walkright = [pygame.image.load(image_dir+'tile024.png'),pygame.image.load(image_dir+'tile025.png'), \n",
111+
" pygame.image.load(image_dir+'tile026.png'),pygame.image.load(image_dir+'tile027.png'), \n",
112+
" pygame.image.load(image_dir+'tile028.png'),pygame.image.load(image_dir+'tile029.png'), \n",
113+
" pygame.image.load(image_dir+'tile030.png'),pygame.image.load(image_dir+'tile031.png')] \n",
114+
"\n",
115+
"walkleft =[pygame.image.load(image_dir+ 'tile016.png'),pygame.image.load(image_dir+'tile017.png'), \n",
116+
" pygame.image.load(image_dir+'tile018.png'),pygame.image.load(image_dir+'tile019.png'), \n",
117+
" pygame.image.load(image_dir+'tile020.png'),pygame.image.load(image_dir+'tile021.png'),\n",
118+
" pygame.image.load(image_dir+'tile022.png'),pygame.image.load(image_dir+'tile023.png')] \n",
119+
"\n",
120+
"walkup = [pygame.image.load(image_dir+'tile008.png'),pygame.image.load(image_dir+'tile009.png'),\n",
121+
" pygame.image.load(image_dir+'tile010.png'),pygame.image.load(image_dir+'tile011.png'),\n",
122+
" pygame.image.load(image_dir+'tile012.png'),pygame.image.load(image_dir+'tile013.png'),\n",
123+
" pygame.image.load(image_dir+'tile014.png'),pygame.image.load(image_dir+'tile015.png')] \n",
124+
"\n",
125+
"walkdown = [pygame.image.load(image_dir+'tile000.png'),pygame.image.load(image_dir+'tile001.png'),\n",
126+
" pygame.image.load(image_dir+'tile002.png'),pygame.image.load(image_dir+'tile003.png'),\n",
127+
" pygame.image.load(image_dir+'tile004.png'),pygame.image.load(image_dir+'tile005.png'),\n",
128+
" pygame.image.load(image_dir+'tile006.png'),pygame.image.load(image_dir+'tile007.png')] \n",
129+
" \n",
130+
"#################################################################################################\n",
131+
" \n",
132+
"man = fn.Player(dw_half,dh_half,20,30, 2.75) # Initialize our character, named 'man', at the center of the screen \n",
133+
" # (dw_half, dh_half), with a size of width=20 and height=30 and velocity (vel) \n",
134+
" # at 2.5. The size of our character is necessary to detect collisions with the \n",
135+
" # border of our game-window.\n",
136+
" \n",
137+
"run = True # Boolean to run our main-game loop.\n",
138+
"while run: # While run is true, run the main game loop repetitively.\n",
139+
" \n",
140+
" \n",
141+
" keys = pygame.key.get_pressed() # A pygame function that listens for keypresses (live inputs from the user).\n",
142+
" # Keys is defined as a tuple filled with 0's. When a key press is initiated, a\n",
143+
" # new tuple is made with a value of 1 at the corresponding entry, to show that\n",
144+
" # a key press has been made.\n",
145+
" \n",
146+
" clock.tick(32) # The framerate of our program, the number of frames or iterations of our code \n",
147+
" # that are run each second.\n",
148+
" \n",
149+
"\n",
150+
" \n",
151+
" for event in pygame.event.get(): # This gets events from the queue, commanded by the user. \n",
152+
" if event.type == pygame.QUIT: # If the user clicks exit (the red X at the top-right of a window)\n",
153+
" run = False # run is set to False. This will end our game loop.\n",
154+
" \n",
155+
" \n",
156+
" man.move(keys, dw, dh) # Calls to our move function, inputting our keys tuple and screen dimensions.\n",
157+
" \n",
158+
" man.draw(win, char, walkright, # Calls to our animation function, inputting our window and lists of character\n",
159+
" walkleft, walkup, walkdown) # images to sequence.\n",
160+
" \n",
161+
" fn.redraw_game_window(win, bg, dw_half, # Calls our function for updating the game window, inputting the window,\n",
162+
" w_center, h_center, dh_half) # background image, image_size, and halved screen dimensions for use.\n",
163+
" \n",
164+
"pygame.quit() # Once the game loop is done, exit the window.\n"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": null,
170+
"metadata": {},
171+
"outputs": [],
172+
"source": []
173+
},
174+
{
175+
"cell_type": "code",
176+
"execution_count": null,
177+
"metadata": {},
178+
"outputs": [],
179+
"source": []
180+
}
181+
],
182+
"metadata": {
183+
"kernelspec": {
184+
"display_name": "Python 3",
185+
"language": "python",
186+
"name": "python3"
187+
},
188+
"language_info": {
189+
"codemirror_mode": {
190+
"name": "ipython",
191+
"version": 3
192+
},
193+
"file_extension": ".py",
194+
"mimetype": "text/x-python",
195+
"name": "python",
196+
"nbconvert_exporter": "python",
197+
"pygments_lexer": "ipython3",
198+
"version": "3.7.1"
199+
}
200+
},
201+
"nbformat": 4,
202+
"nbformat_minor": 2
203+
}

Stephen_Fa18/images/background.PNG

203 KB
579 Bytes
566 Bytes
559 Bytes
566 Bytes
579 Bytes
572 Bytes
557 Bytes
572 Bytes
556 Bytes
549 Bytes
535 Bytes
549 Bytes
556 Bytes
556 Bytes
539 Bytes
556 Bytes
545 Bytes
551 Bytes
577 Bytes
549 Bytes
546 Bytes
556 Bytes
581 Bytes
555 Bytes
543 Bytes
548 Bytes
577 Bytes
546 Bytes
545 Bytes
556 Bytes
580 Bytes
558 Bytes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
"test_functions.py::test_Player"
3+
]
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)