forked from aavina/jubal-pygame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.py
176 lines (136 loc) · 5.35 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
#!/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
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
LEN_SPRT_X=64
LEN_SPRT_Y=64
# This is where the sprite is found on the sheet
SPRT_RECT_X=0
SPRT_RECT_Y=LEN_SPRT_Y
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 = SCREEN_X - LEN_SPRT_X
startY = 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, startY
# Add tiles
startx = 0
starty = SCREEN_Y - LEN_SPRT_Y/2
tile = Tile(IMAGESDICT['ground'])
tile.rect.topleft = startx, starty
# Sprite Groups
allsprites = pygame.sprite.RenderPlain(player)
environment = pygame.sprite.RenderPlain(tile)
# 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()
# Vertical logic
if keyinput.wasKeyPressed(K_UP):
player.jump()
# Check for collisions between player and environment
collision_list = pygame.sprite.spritecollide(player, environment, False)
# Update
allsprites.update()
environment.update()
# Draw
allsprites.draw(DISPLAYSURF)
environment.draw(DISPLAYSURF)
pygame.display.update()
fpsClock.tick(FPS)
# 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_normal': SHEET.subsurface(pygame.Rect(0, 0, LEN_SPRT_X, LEN_SPRT_Y)),
'j_rightface': SHEET.subsurface(pygame.Rect(SPRT_RECT_X, SPRT_RECT_Y, LEN_SPRT_X, LEN_SPRT_Y)),
'j_leftface': SHEET.subsurface(pygame.Rect(SPRT_RECT_X+(LEN_SPRT_X*5), SPRT_RECT_Y, LEN_SPRT_X, LEN_SPRT_Y)),
'bullet': SHEET.subsurface(pygame.Rect(SPRT_RECT_X+(LEN_SPRT_X*8), SPRT_RECT_Y*3, 2, 2)),
'ground': SHEET.subsurface(pygame.Rect(LEN_SPRT_X*4, SPRT_RECT_Y*5, LEN_SPRT_X, LEN_SPRT_Y/2)),
}
# Define the different animation types
animTypes = 'right_walk left_walk shoot_right shoot_left jump_right jump_left right_face left_face normal'.split()
# These tuples contain (base_x, base_y, numOfFrames)
# numOfFrames is in the x-direction
animTypesInfo = {
'right_walk': (SPRT_RECT_X+LEN_SPRT_X, SPRT_RECT_Y, 4),
'left_walk': (SPRT_RECT_X+(LEN_SPRT_X*6), SPRT_RECT_Y, 4),
'shoot_right': (LEN_SPRT_X, 0, 4),
'shoot_left': (LEN_SPRT_X*5, 0, 4),
'jump_right': (0, LEN_SPRT_Y*2, 7),
'jump_left': (0, LEN_SPRT_Y*3, 7),
'normal': (0, 0, 1),
'right_face': (0, LEN_SPRT_Y, 1),
'left_face': (0, LEN_SPRT_Y*3, 1)
}
animObjs = {}
for animType in animTypes:
xbase = (animTypesInfo[animType])[0]
ybase = (animTypesInfo[animType])[1]
numFrames = (animTypesInfo[animType])[2]
imagesAndDurations = [(SHEET.subsurface(pygame.Rect(xbase+(LEN_SPRT_X*num), ybase, LEN_SPRT_X, LEN_SPRT_Y)), DURATION) for num in range(numFrames)]
loopforever = True
if(animType == 'shoot_right' or animType == 'shoot_left'):
loopforever = False
animObjs[animType] = pyganim.PygAnimation(imagesAndDurations, loop=loopforever)
return IMAGESDICT, animObjs
if __name__ == "__main__":
main()