-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
98 lines (74 loc) · 2.1 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
import pygame
pygame.init()
WIDTH = 500
HEIGHT = 500
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("pesawat2an")
MERAH = (250, 50, 50)
PUTIH = (255, 255, 255)
HIJAU = (50, 250, 50)
class Peluru:
def __init__(self, x, y):
self.size = 50
self.x = x
self.y = y
self.langkah = 5
def tampilkan(self):
pygame.draw.rect(screen, MERAH, (self.x, self.y, self.size, self.size))
def gerak(self):
self.y = self.y - self.langkah
def keatas(self):
if self.y < 0:
return True
class Pesawat:
def __init__(self):
self.size = 50
self.x = WIDTH / 2
self.y = HEIGHT - self.size
self.tombolKiri = False
self.tombolKanan = False
self.langkah = 3
def tampilkan(self):
gambar = pygame.image.load("player.png")
gambar = pygame.transform.scale(gambar, (self.size, self.size))
screen.blit(gambar, (self.x, self.y))
# pygame.draw.rect(screen, HIJAU, (self.x, self.y, self.size, self.size))
def gerak(self):
if self.tombolKanan:
self.x = self.x + self.langkah
if self.tombolKiri:
self.x = self.x - self.langkah
def main():
run = True
while run:
for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False
# KEYDOWN
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
run = False
elif e.type == pygame.KEYDOWN and e.key == pygame.K_RIGHT:
pesawat.tombolKanan = True
elif e.type == pygame.KEYDOWN and e.key == pygame.K_LEFT:
pesawat.tombolKiri = True
elif e.type == pygame.KEYDOWN and e.key == pygame.K_SPACE:
bullets.append(Peluru(pesawat.x, pesawat.y))
# KEYUP
if e.type == pygame.KEYUP:
pesawat.tombolKanan = False
pesawat.tombolKiri = False
screen.fill(PUTIH)
# PELULU
for bullet in bullets:
bullet.tampilkan()
bullet.gerak()
if bullet.keatas():
bullets.remove(bullet)
# PESAWAT
pesawat.tampilkan()
pesawat.gerak()
pygame.display.update()
if __name__ == "__main__":
pesawat = Pesawat()
bullets = []
main()