-
Notifications
You must be signed in to change notification settings - Fork 18
/
course_10.py
131 lines (109 loc) · 3.9 KB
/
course_10.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
import pygame
successes, fails = pygame.init()
print(successes, fails)
move_right = [pygame.image.load("hero/R1.png"), pygame.image.load("hero/R2.png"), pygame.image.load("hero/R3.png"), pygame.image.load("hero/R4.png"), pygame.image.load("hero/R5.png"), pygame.image.load("hero/R6.png"), pygame.image.load("hero/R7.png"), pygame.image.load("hero/R8.png"), pygame.image.load("hero/R9.png")]
move_left = [pygame.image.load("hero/L1.png"), pygame.image.load("hero/L2.png"), pygame.image.load("hero/L3.png"), pygame.image.load("hero/L4.png"), pygame.image.load("hero/L5.png"), pygame.image.load("hero/L6.png"), pygame.image.load("hero/L7.png"), pygame.image.load("hero/L8.png"), pygame.image.load("hero/L9.png")]
bg = pygame.image.load("bg.jpg")
hero = pygame.image.load("hero/standing.png")
screenWidth = 700
screenHeight = 500
clock = pygame.time.Clock()
screen = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("cs younes game")
BLACK = (0, 0, 0) # RGB
WHITE = (255, 255, 255)
RED = (255, 0, 0)
class Player():
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.step = 5
self.left = False
self.right = False
self.moves = 0
self.speed = 10
self.isJumping = False
self.standing = True
def draw(self, screen):
if not self.standing:
if self.left:
screen.blit(move_left[self.moves // 2], (self.x, self.y))
self.moves += 1
if self.moves == 18:
self.moves = 0
elif self.right:
screen.blit(move_right[self.moves // 2], (self.x, self.y))
self.moves += 1
if self.moves == 18:
self.moves = 0
else:
if self.right:
screen.blit(move_right[0], (self.x, self.y))
else:
screen.blit(move_left[0], (self.x, self.y))
class Bullet:
def __init__(self, x, y, radius, color, direction, step):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.direction = direction
self.step = step * direction
def draw(self, screen):
pygame.draw.circle(screen, self.color,(self.x, self.y) ,self.radius)
man = Player(600, 400, 64, 64)
def redrawGame():
global moves
screen.blit(bg, (0, 0))
man.draw(screen)
for bullet in bullets:
bullet.draw(screen)
pygame.display.update()
bullets = []
while True:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()
keys = pygame.key.get_pressed()
for bullet in bullets:
if bullet.x < screenWidth and bullet.x > 0:
bullet.x += bullet.step
else:
bullets.remove(bullet)
if keys[pygame.K_w]:
if len(bullets) < 5:
direction = 0
if man.right:
direction = 1
else: direction = -1
bullets.append(Bullet( round(man.x + man.width // 2), round(man.y + man.height // 2), 5, RED, direction, 10))
if keys[pygame.K_LEFT] and man.x - man.step >= 0:
man.x -= man.step
man.left = True
man.right = False
man.standing = False
elif keys[pygame.K_RIGHT] and man.x + man.width + man.step <= screenWidth:
man.x += man.step
man.right = True
man.left = False
man.standing = False
else:
man.standing = True
man.moves = 0
if not man.isJumping:
if keys[pygame.K_SPACE]:
man.isJumping = True
else:
if man.speed >= -10 :
neg = 1
if man.speed < 0:
neg = -1
man.y -= (man.speed ** 2) * 0.25 * neg
man.speed -= 1
else:
man.speed = 10
man.isJumping = False
redrawGame()