-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyGame.py
134 lines (110 loc) · 3.04 KB
/
pyGame.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
import pygame
pygame.init()
win_height = 500
win_width = 500
win = pygame.display.set_mode((win_width, win_height))
pygame.display.set_caption("Cubes")
walkRight = [pygame.image.load('right1.png'),
pygame.image.load('right2.png'),
pygame.image.load('right3.png'),
pygame.image.load('right4.png'),
pygame.image.load('right5.png')]
walkLeft = [pygame.image.load('left1.png'),
pygame.image.load('left2.png'),
pygame.image.load('left3.png'),
pygame.image.load('left4.png'),
pygame.image.load('left5.png')]
bg = pygame.image.load('back.jpg')
playerStand_Left = pygame.image.load('stand_left.png')
playerStand_Right = pygame.image.load('stand_right.png')
clock = pygame.time.Clock()
x = 50
y = 350
width = 40
height = 60
speed = 5
isJump = False
jumpCount = 10
left = False
standLeft = False
right = False
animCount = 0
class bullet():
def __init__(self, x, y, rad, color, facing):
self.x = x
self.y = y
self.rad = rad
self.color = color
self.facing = facing
self.vel = 8 * facing
def draw(self, win):
pygame.draw.circle(win, self.color, (self.x, self.y), self.rad)
def drawWindow():
global animCount
win.blit(bg, (0, 0))
if animCount + 1 >= 25:
animCount = 0
if left:
win.blit(walkLeft[animCount // 5], (x, y))
animCount += 1
elif right:
win.blit(walkRight[animCount // 5], (x, y))
animCount += 1
elif standLeft:
win.blit(playerStand_Left, (x, y))
else:
win.blit(playerStand_Right, (x, y))
for bull in bullets:
bull.draw(win)
pygame.display.update()
run = True
bullets = []
while run:
clock.tick(30)
pygame.time.delay(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bul in bullets:
if bul.x < win_height and bul.x > 0:
bul.x += bul.vel
else:
bullets.pop(bullets.index(bul))
keys = pygame.key.get_pressed()
if keys[pygame.K_f]:
if standLeft == False:
facing = 1
else:
facing = -1
if len(bullets) < 5:
bullets.append(bullet(round(x + width // 2),
round(y + height // 2), 5, (0, 255, 0), facing))
if keys[pygame.K_LEFT] and x > 5:
x -= speed
left = True
right = False
standLeft = True
elif keys[pygame.K_RIGHT] and x < win_width - width - 5:
x += speed
left = False
right = True
standLeft = False
else:
left = False
right = False
animCount = 0
if not isJump:
if keys[pygame.K_SPACE]:
isJump = True
else:
if jumpCount >= -10:
if jumpCount < 0:
y += (jumpCount ** 2) / 2
else:
y -= (jumpCount ** 2) / 2
jumpCount -= 1
else:
isJump = False
jumpCount = 10
drawWindow()
pygame.quit()