-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshooter-game.py
170 lines (127 loc) · 4.74 KB
/
shooter-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
# Rob's notes are the best!
# We have access to pygame, because we did:
# $ pip install pygame
# it is NOT part of core. This is a 3rd party module.
import pygame
from pygame.sprite import Group, groupcollide
import random
from random import randint
import time # I imported this but haven't used it yet. I think I should probably use it, what do you think?
# -----CUSTOM CLASSES HERE-----
from Player import Player
from Bad_guy import Bad_guy
from Bullet import Bullet
# Have to init the pygame object so we can use it
pygame.init()
# Screen size is a tuple
screen_size = (1000,730)
# Because we are going to paint the background, we need a tuple for the color
background_image = pygame.image.load("./images/bg.png")
# start_screen = pygame.image.load("./images/home_screen.png")
# Create a screen for pygame to use to draw on
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("S.Miles")
the_player = Player('./images/hero_sunny.png',100,100,screen)
# Make a bad_guy
bad_guy = Bad_guy(screen)
# make a group for the bad_guys
# bad_guys = Group()
bad_guys = pygame.sprite.Group()
bad_guys.add(bad_guy)##adding ghost to screen!important!
# ghost2 = Bad_guy(screen)
# ghost3 = Bad_guy(screen)
# ghost4 = Bad_guy(screen)
# ghost5 = Bad_guy(screen)
# bad_guys.add(ghost2)
# bad_guys.add(ghost3)
# bad_guys.add(ghost4)
# bad_guys.add(ghost5)
good_guy = pygame.image.load("./images/ghost_sunny.png")
# Make a new Group called bullets. Group is a pygame "list"
#Loading all the "bullet" items.
cookie_img = pygame.image.load("./images/cookie.png")
cupcake_img = pygame.image.load("./images/cupcake.png")
doughnut_img = pygame.image.load("./images/doughnut.png")
icecream_img = pygame.image.load("./images/icecream.png")
# cookie_img = Bullet(screen,the_player,the_player.x)
# cupcake_img = Bullet(screen,the_player,the_player.x)
bullets = pygame.sprite.Group()
# bullets.add(cookie_img) ###THIS DOESN'T WORK! HOW CAN I HAVE UNIQUE 'BULLETS?' ARRAY? I think I need a list.
# bullets.add(icecream_img)
# bullets.add(doughnut_img)
game_on = True;
# Set up the main game loop
while game_on: #will run forever (until break)
# Loop through all the pygame events.
# This is pygame's escape hatch. (Quit)
for event in pygame.event.get():
# print event
if event.type == pygame.QUIT:
game_on = False
elif event.type == pygame.KEYDOWN:
print event.key
# print "User pressed a key!!!"
if event.key == 273:
# user pressed up!
# the_player.y -= the_player.speed
the_player.should_move("up",True)
elif event.key == 274:
# the_player.y += the_player.speed
the_player.should_move("down",True)
if event.key == 275:
# the_player.x += the_player.speed
the_player.should_move("right",True)
elif event.key == 276:
# the_player.x -= the_player.speed
the_player.should_move("left",True)
elif event.key == 32:
# 32 = SPACE BAR... FIRE!!!!
new_bullet = Bullet(screen, the_player, 1)
bullets.add(new_bullet)
elif event.type == pygame.KEYUP:
if event.key == 273:
the_player.should_move("up",False)
elif event.key == 274:
the_player.should_move("down",False)
if event.key == 275:
the_player.should_move("right",False)
elif event.key == 276:
the_player.should_move("left",False)
screen.blit(background_image, [0,0]);
for bad_guy in bad_guys:
# update the bad guy (based on where the player is)
bad_guy.update_me()
# draw the bad guy
bad_guy.draw_me()
# # Must be after fill, or we won't be able to see the hero
# screen.blit(the_player.image, [the_player.x,the_player.y])
the_player.draw_me()
the_player.keep_on_screen()
bad_guy.keep_on_screen()
for bullet in bullets:
# update the bullet location
bullet.update()
# draw the bullet on the screen
bullet.draw_bullet()
# bullet.bullet_randomization()
# Check for collions...
bullet_hit = groupcollide(bullets,bad_guys,True,False)
if (bullet_hit):
bad_guys.add(Bad_guy(screen))
# bad_guy.image = pygame.transform.scale(good_guy,(95,100))
bad_guy.up_and_away()
# a bunch of failed logic
# if len(bad_guys) == 0:
# bad_guys.add(Bad_guy(screen))
# if len(bullets) == 0:
# bullets.add(Bullet(screen))
# print bullet_hit
# flip the screen, i.e.clear it so we can draw again... and again... and again
pygame.display.flip()
###############################################
# WHAT I WANT/HOW I WANT MY BULLETS TO BEHAVE #
# 1. LOAD IMAGES (4 DIFFERENT IMAGES) TO A GROUP OR LIST OR "SOMETHING"
# 2. EACH TIME THE USER PRESSES SPACE BAR/32, THE IMAGE GROUP CYCLES THROUGH THE FOUR DIFFERENT IMAGES.
# --use a for loop to iterate through the group (look at the example from shuffle deck in blackjack game.)
#look up .shuffle or whatever method that Rob mentioned.
# clock = pygame.time.Clock() ?? Do I need this?