-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGame.py
213 lines (165 loc) · 6.27 KB
/
Game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python
import pygame, sys, pyganim, os
from pygame.locals import *
from Player import Player
from Input import Input
from Tile import Tile
from Bullet import Bullet
pygame.init()
FPS = 30 # frames per second settings
fpsClock = pygame.time.Clock()
DURATION = 0.1
# Screen size
SCREEN_X=400
SCREEN_Y=400
# This is the length of the sprite box
LEN_SPRT_X=64
LEN_SPRT_Y=64
# Dimension of the player sprite
SPRITE_SHOOT_WIDTH = 46
SPRITE_WALK_WIDTH = 18
SPRITE_WIDTH = 24
SPRITE_HEIGHT = 60
# Sprite Offsets
OFFSET_X = 18
OFFSET_Y = 4
def main():
# Determine assets
sprite_asset, bullet_sound_asset = DetermineAssets()
# Load sprite assets
IMAGESDICT, animObjs = LoadSpriteAssets(sprite_asset)
# Main game surface
DISPLAYSURF = pygame.display.set_mode((SCREEN_X, SCREEN_Y)) #Make the screen
# Colors
BLACK = (0,0,0)
# Calculate starting position of player
startX, startY = SCREEN_X/2 - LEN_SPRT_X, SCREEN_Y - LEN_SPRT_Y
# Hold info on keys pressed, held, released
keyinput = Input()
# Initialize gamemap and Player
player = Player(IMAGESDICT, animObjs, bullet_sound_asset)
player.rect.topleft = startX, 0
# Create map sprite group
environment = pygame.sprite.Group()
# Open and parse map file
parseMap(IMAGESDICT, environment)
# Sprite group
allsprites = pygame.sprite.RenderPlain(player)
# Start game loop
while True:
# Clear key info
keyinput.clearKeys()
# Draw screen black
DISPLAYSURF.fill(BLACK)
# Check for game events
for event in pygame.event.get():
# Reset player direction
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
# Handle key presses
keyinput.keyDownEvent(event.key)
elif event.type == KEYUP:
keyinput.keyUpEvent(event.key)
# Player horizontal logic
if keyinput.isKeyHeld(K_LEFT) and keyinput.isKeyHeld(K_RIGHT):
player.stopMoving()
elif keyinput.isKeyHeld(K_LEFT):
player.moveLeft()
elif keyinput.isKeyHeld(K_RIGHT):
player.moveRight()
elif keyinput.wasKeyPressed(K_SPACE):
# Play player shooting animation
player.shoot()
elif keyinput.wasKeyPressed(K_ESCAPE):
pygame.quit()
sys.exit()
elif not player.shooting:
player.stopMoving()
# Toggle Blue/Red character
if keyinput.wasKeyPressed(K_c):
player.toggleColor()
# Vertical logic
if keyinput.wasKeyPressed(K_UP):
player.jump()
# Remove bullets that leave screen
for sprite in allsprites:
if isinstance(sprite, Bullet):
if sprite.rect[0] > SCREEN_X or sprite.rect[0] < 0:
allsprites.remove(sprite)
# Update
allsprites.update(environment)
environment.update(allsprites)
# Draw
allsprites.draw(DISPLAYSURF)
environment.draw(DISPLAYSURF)
pygame.display.update()
fpsClock.tick(FPS)
# Parses a map file and creates the game map from it
def parseMap(IMAGESDICT, environment):
# Open the map
f = open('map1.txt', 'r')
# Set start points
baseX, baseY = 0, 0
count = 0
for line in f:
baseX = 0
for char in line:
if char == 'g':
# Add ground tile here
startx, starty = baseX*32, baseY*32
tile = Tile(IMAGESDICT['ground'])
tile.rect.topleft = startx, starty
environment.add(tile)
baseX += 1
baseY += 1
f.close()
# Determines what filesystem accessor to use and retreives graphic and sound assets
def DetermineAssets():
# Find out if in Windows or Unix/Linux then load SpriteSheet
if os.path.isfile('assets\\jubal_64.png'):
SHEET = pygame.image.load('assets\\jubal_64.png')
bullet_sound = pygame.mixer.Sound("assets\\bullet.wav")
elif os.path.isfile('assets//jubal_64.png'):
SHEET = pygame.image.load('assets//jubal_64.png')
bullet_sound = pygame.mixer.Sound("assets//bullet.wav")
return SHEET, bullet_sound
def LoadSpriteAssets(SHEET):
# Global dictionary that contains all static images
IMAGESDICT = {
'j_rightface': SHEET.subsurface(pygame.Rect(OFFSET_X, LEN_SPRT_Y + 4, SPRITE_WIDTH, SPRITE_HEIGHT)),
'bullet': SHEET.subsurface(pygame.Rect(LEN_SPRT_X*8, LEN_SPRT_Y*3, 2, 2)),
'ground': SHEET.subsurface(pygame.Rect(LEN_SPRT_X*4, LEN_SPRT_Y*5, LEN_SPRT_X/2, LEN_SPRT_Y/2)),
}
IMAGESDICT['j_leftface'] = pygame.transform.flip(IMAGESDICT['j_rightface'], True, False)
# Define the different animation types
animTypes = 'right_walk shoot_right jump_right'.split()
# These tuples contain (base_x, base_y, numOfFrames)
# numOfFrames is in the x-direction
animTypesInfo = {
'right_walk': (LEN_SPRT_X + SPRITE_WIDTH, LEN_SPRT_Y + OFFSET_Y, 4),
'shoot_right': (LEN_SPRT_X + OFFSET_X, OFFSET_Y, 4),
'jump_right': (OFFSET_X, (LEN_SPRT_Y*2) + OFFSET_Y, 7),
}
# Create the animated objects dictionary
animObjs = {}
for animType in animTypes:
xbase = (animTypesInfo[animType])[0]
ybase = (animTypesInfo[animType])[1]
numFrames = (animTypesInfo[animType])[2]
loopforever = True
if(animType == 'right_walk'):
imagesAndDurations = [(SHEET.subsurface(pygame.Rect(xbase+(LEN_SPRT_X*num), \
ybase, SPRITE_WALK_WIDTH, SPRITE_HEIGHT)), DURATION) for num in range(numFrames)]
elif(animType == 'shoot_right'):
loopforever = False
imagesAndDurations = [(SHEET.subsurface(pygame.Rect(xbase+(LEN_SPRT_X*num), \
ybase, SPRITE_SHOOT_WIDTH, LEN_SPRT_Y)), DURATION) for num in range(numFrames)]
elif(animType == 'jump_right'):
imagesAndDurations = [(SHEET.subsurface(pygame.Rect(xbase+(LEN_SPRT_X*num), \
ybase, SPRITE_WIDTH, LEN_SPRT_Y)), DURATION) for num in range(numFrames)]
animObjs[animType] = pyganim.PygAnimation(imagesAndDurations, loop=loopforever)
return IMAGESDICT, animObjs
if __name__ == "__main__":
main()