forked from bhansa/fireball
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
154 lines (129 loc) · 5.43 KB
/
main.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
import sys
import pygame
from random import *
__author__ = 'bhansa'
def minmax(val, minval, maxval):
return min(max(val, minval), maxval)
class Constants:
BLACK = 0, 0, 0
WHITE = 255, 255, 255
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600
class Fireball:
def __init__(self):
self.vx = randint(-5, 5) # initial velocities
self.vy = randint(-5, 5)
self.x = randint(0, 600) # initial position
self.y = randint(0, 600)
self.fireimg = pygame.image.load("assets/fireball.png")
def update(self):
self.x = minmax(self.x + self.vx, 0, Constants.SCREEN_WIDTH - self.fireimg.get_width())
self.y = minmax(self.y + self.vy, 0, Constants.SCREEN_HEIGHT - self.fireimg.get_height())
if self.x == 0 or self.x == Constants.SCREEN_WIDTH - self.fireimg.get_width():
self.vx *= -1 # if ball hits edge of screen reverse the velocity, to simulate bouncing off edge of screen
if self.y == 0 or self.y == Constants.SCREEN_HEIGHT - self.fireimg.get_height():
self.vy *= -1 # same in y dir
# Main Class of the Game, holding the game state
class Game:
# Initializes the game
def __init__(self):
pygame.init()
self.size = self.width, self.height = Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT
self.screen = pygame.display.set_mode(self.size)
pygame.display.set_caption("Fireball")
self.ball = pygame.image.load("assets/mario.png")
pygame.key.set_repeat(10, 10)
self.clk = pygame.time.Clock()
self.loop = True
self.x, self.y = 0, 0
self.imgx, self.imgy = 0, 0
self.timer = 0 # timer to track fireballs
self.ball_list = []
for x in range(6):
self.ball_list.append(Fireball()) # list of on-screen fireballs
# Shows a message
def message_display(self, text):
txt = pygame.font.Font('freesansbold.ttf', 12)
s = txt.render(text, True, Constants.BLACK)
sr = s.get_rect()
sr.center = (400, 10)
self.screen.blit(s, sr)
# Function to run the whole game
def run(self):
while(self.loop):
self._loop()
# Runs in every game tick
def _loop(self):
for event in pygame.event.get():
self.timer += 1
if event.type == pygame.QUIT:
self.loop = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.x = -5
elif event.key == pygame.K_UP:
self.y = -5
elif event.key == pygame.K_RIGHT:
self.x = 5
elif event.key == pygame.K_DOWN:
self.y = 5
if event.type == pygame.KEYUP:
if event.key in [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN]:
self.x = 0
self.y = 0
if len(self.ball_list) >= 6:
self.ball_list.pop(0) # remove one fireball if there are more than six on-screen
self.imgx = minmax(self.imgx + self.x, 0, self.size[0] - self.ball.get_width())
self.imgy = minmax(self.imgy + self.y, 0, self.size[1] - self.ball.get_height())
self.screen.fill(Constants.WHITE)
self.screen.blit(self.ball, (self.imgx, self.imgy))
for i in self.ball_list:
i.update() # bitblt the fireballs
self.screen.blit(i.fireimg, (i.x, i.y))
text = "x: " + str(self.imgx) + " y: " + str(self.imgy)
self.message_display(text)
pygame.display.update()
self.clk.tick(60)
class SplashScreen:
""" Splash Screen """
def __init__(self):
pygame.init()
self.size = self.width, self.height = Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT
self.screen = pygame.display.set_mode(self.size)
pygame.display.set_caption("Fireball")
self.draw_text("Fireball", 48, Constants.WHITE, self.width / 2, self.height / 4)
self.draw_text("Press the arrow keys to move around.", 22, Constants.WHITE,
self.width / 2, self.height / 2.5)
self.draw_text('Press P to play or Q to quit', 22, Constants.WHITE,
self.width / 2, self.height / 2)
self.clk = pygame.time.Clock()
self.play_game = False
pygame.display.flip()
self.wait_for_key()
def draw_text(self, text, size, color, x, y):
font = pygame.font.Font('freesansbold.ttf', size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
self.screen.blit(text_surface, text_rect)
def wait_for_key(self):
waiting = True
while waiting:
self.clk.tick()
for event in pygame.event.get():
mouse = pygame.mouse.get_pos()
pygame.display.update()
if (event.type == pygame.KEYUP and event.key == pygame.K_q or
event.type == pygame.QUIT):
waiting = False
if event.type == pygame.KEYUP and event.key == pygame.K_p:
waiting = False
self.play_game = True
# Entry point for the game
def main():
game = Game()
game.run()
if __name__ == "__main__":
splash = SplashScreen()
if splash.play_game:
main()