Skip to content

Commit e450375

Browse files
author
Kumar
committed
Merge pygame_tutorials repository
adds amazon price tracking project adds python with databases
1 parent 281e537 commit e450375

35 files changed

+2930
-11
lines changed

Diff for: Milestone Project 1/Tic_tac.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ def win_check(board, mark):
7070
or ( # down the right side
7171
board[7] == mark and board[5] == mark and board[3] == mark
7272
)
73-
or (board[9] == mark and board[5] == mark and board[1] == mark) # diagonal
73+
# diagonal
74+
or (board[9] == mark and board[5] == mark and board[1] == mark)
7475
) # diagonal
7576

7677

Diff for: Pygame_tutorial/BouncingBall/ball.png

12.8 KB
Loading

Diff for: Pygame_tutorial/BouncingBall/main.py

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import pygame
2+
3+
# initialize the pygame
4+
pygame.init()
5+
6+
# create the screen
7+
X = 800
8+
Y = 600
9+
screen = pygame.display.set_mode((X, Y))
10+
running = True
11+
12+
# title
13+
pygame.display.set_caption("Bouncing Ball")
14+
15+
# Ball
16+
ballImg = pygame.image.load("ball.png")
17+
ballX = 10 # position x
18+
ballY = 10 # position y
19+
ballX_change = 2 # velocity x
20+
ballY_change = 2 # velocity y
21+
22+
# Game Loop
23+
while running:
24+
# set background
25+
screen.fill((0, 0, 0))
26+
27+
# set ball
28+
screen.blit(ballImg, (ballX, ballY))
29+
30+
# check for events
31+
for event in pygame.event.get():
32+
if event.type == pygame.QUIT:
33+
running = False
34+
35+
# update ball
36+
ballX += ballX_change
37+
ballY += ballY_change
38+
39+
# collision detection
40+
# top
41+
if ballX >= X - 64:
42+
ballX_change = -1.5
43+
# bottom
44+
elif ballX <= 0:
45+
ballX_change = 1.5
46+
47+
# right
48+
if ballY >= Y - 64:
49+
ballY_change = -1.5
50+
# left
51+
elif ballY <= 0:
52+
ballY_change = 1.5
53+
54+
credit_font = pygame.font.Font("freesansbold.ttf", 12)
55+
credit = credit_font.render("Created By: KUMAR SHANU", True, (255, 255, 0))
56+
screen.blit(credit, (10, 580))
57+
# update frame
58+
pygame.display.update()

Diff for: Pygame_tutorial/Space Invadors/images/background.png

3.62 MB
Loading

Diff for: Pygame_tutorial/Space Invadors/images/bullet.png

721 Bytes
Loading

Diff for: Pygame_tutorial/Space Invadors/images/enemy.png

3.39 KB
Loading

Diff for: Pygame_tutorial/Space Invadors/images/space-ship.png

4.3 KB
Loading

Diff for: Pygame_tutorial/Space Invadors/images/ufo.png

1.42 KB
Loading

Diff for: Pygame_tutorial/Space Invadors/main.py

+201
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
"""
2+
Space Invaders
3+
Version - 1.0.0
4+
Simple Game Using Pygame 1.9.6
5+
#its_Kumar
6+
7+
"""
8+
9+
import math
10+
import random
11+
12+
import pygame
13+
from pygame import mixer
14+
15+
# initialize the pygame
16+
pygame.init()
17+
18+
# create the screen
19+
screen = pygame.display.set_mode((800, 600))
20+
running = True
21+
22+
# title and icon
23+
pygame.display.set_caption("Space Invador")
24+
icon = pygame.image.load("images/ufo.png")
25+
pygame.display.set_icon(icon)
26+
background = pygame.image.load("images/background.png")
27+
28+
# Background Sound
29+
mixer.music.load("sounds/background.wav")
30+
mixer.music.play(-1)
31+
32+
# Player
33+
playerImg = pygame.image.load("images/space-ship.png")
34+
playerX = 370
35+
playerY = 480
36+
playerX_change = 0
37+
playerY_change = 0
38+
39+
# Enemy
40+
enemyImg = []
41+
enemyX = []
42+
enemyY = []
43+
enemyX_change = []
44+
enemyY_change = []
45+
num_enemy = 6
46+
47+
for i in range(num_enemy):
48+
enemyImg.append(pygame.image.load("images/enemy.png"))
49+
enemyX.append(random.randint(0, 735))
50+
enemyY.append(random.randint(50, 150))
51+
enemyX_change.append(1)
52+
enemyY_change.append(30)
53+
54+
# Bullet
55+
bulletImg = pygame.image.load("images/bullet.png")
56+
bulletX = 0
57+
bulletY = 480
58+
bulletX_change = 0
59+
bulletY_change = 20
60+
61+
# "ready" - You can't see the bullet
62+
# "fire" - Bullet is moving
63+
bullet_state = "ready"
64+
65+
# Score
66+
score_value = 0
67+
font = pygame.font.Font("freesansbold.ttf", 32)
68+
textX = 10
69+
textY = 10
70+
71+
# Game over text
72+
over_font = pygame.font.Font("freesansbold.ttf", 64)
73+
74+
credit_font = pygame.font.Font("freesansbold.ttf", 12)
75+
76+
77+
def player(x, y):
78+
screen.blit(playerImg, (x, y))
79+
80+
81+
def enemy(x, y, i):
82+
screen.blit(enemyImg[i], (x, y))
83+
84+
85+
def fire_bullet(x, y):
86+
global bullet_state
87+
bullet_state = "fire"
88+
screen.blit(bulletImg, (x + 16, y + 10))
89+
90+
91+
def is_collision(X1, Y1, X2, Y2):
92+
distance = math.sqrt(math.pow(X2 - X1, 2) + math.pow(Y2 - Y1, 2))
93+
if distance < 27:
94+
return True
95+
else:
96+
return False
97+
98+
99+
def show_score(x, y):
100+
score = font.render("Score : " + str(score_value), True, (255, 255, 255))
101+
screen.blit(score, (x, y))
102+
103+
104+
def game_over_text():
105+
over_text = over_font.render("GAME OVER!!! ", True, (255, 255, 255))
106+
screen.blit(over_text, (200, 256))
107+
108+
109+
# Game Loop
110+
while running:
111+
screen.fill((0, 0, 0))
112+
113+
# background image
114+
screen.blit(background, (0, 0))
115+
116+
for event in pygame.event.get():
117+
if event.type == pygame.QUIT:
118+
running = False
119+
120+
# if keystroke is pressed check it
121+
if event.type == pygame.KEYDOWN:
122+
if event.key == pygame.K_LEFT:
123+
playerX_change = -5
124+
if event.key == pygame.K_RIGHT:
125+
playerX_change = 5
126+
if event.key == pygame.K_UP:
127+
playerY_change = -1
128+
if event.key == pygame.K_DOWN:
129+
playerY_change = 1
130+
if event.key == pygame.K_SPACE:
131+
if bullet_state == "ready":
132+
bulletX = playerX
133+
bulletY = playerY
134+
bullet_sound = mixer.Sound("sounds/laser.wav")
135+
bullet_sound.play()
136+
fire_bullet(bulletX, bulletY)
137+
138+
if event.type == pygame.KEYUP:
139+
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
140+
playerX_change = 0
141+
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
142+
playerY_change = 0
143+
144+
playerX += playerX_change
145+
playerY += playerY_change
146+
if playerX <= 0:
147+
playerX = 0
148+
elif playerX >= 736:
149+
playerX = 736
150+
if playerY <= 0:
151+
playerY = 0
152+
elif playerY >= 480:
153+
playerY = 480
154+
155+
# Enemy Movement
156+
for i in range(num_enemy):
157+
158+
# Game Over
159+
if enemyY[i] > 420:
160+
for j in range(num_enemy):
161+
enemyY[j] = 2000
162+
game_over_text()
163+
break
164+
165+
enemyX[i] += enemyX_change[i]
166+
if enemyX[i] <= 0:
167+
enemyX_change[i] = 4
168+
enemyY[i] += enemyY_change[i]
169+
elif enemyX[i] >= 736:
170+
enemyX_change[i] = -4
171+
enemyY[i] += enemyY_change[i]
172+
173+
# Collision
174+
collision = is_collision(enemyX[i], enemyY[i], bulletX, bulletY)
175+
if collision:
176+
expo_sound = mixer.Sound("sounds/explosion.wav")
177+
expo_sound.play()
178+
bulletY = 480
179+
bullet_state = "ready"
180+
score_value += 1
181+
enemyX[i] = random.randint(0, 735)
182+
enemyY[i] = random.randint(50, 150)
183+
184+
enemy(enemyX[i], enemyY[i], i)
185+
186+
# Bullet movement
187+
if bulletY <= 0:
188+
bulletY = 480
189+
bullet_state = "ready"
190+
191+
if bullet_state is "fire":
192+
fire_bullet(bulletX, bulletY)
193+
bulletY -= bulletY_change
194+
195+
player(playerX, playerY)
196+
show_score(textX, textY)
197+
credit = credit_font.render("Created By: KUMAR SHANU", True, (255, 255, 0))
198+
credit2 = credit_font.render("#its_Kumar", True, (255, 255, 0))
199+
screen.blit(credit, (10, 580))
200+
screen.blit(credit2, (730, 580))
201+
pygame.display.update()

Diff for: Pygame_tutorial/Space Invadors/sounds/background.wav

4.62 MB
Binary file not shown.

Diff for: Pygame_tutorial/Space Invadors/sounds/explosion.wav

329 KB
Binary file not shown.

Diff for: Pygame_tutorial/Space Invadors/sounds/laser.wav

33.4 KB
Binary file not shown.

Diff for: Pygame_tutorial/Space Invadors/space.png

695 KB
Loading

0 commit comments

Comments
 (0)