-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtutorial5.py
More file actions
105 lines (83 loc) · 2.51 KB
/
tutorial5.py
File metadata and controls
105 lines (83 loc) · 2.51 KB
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
import pygame
import random
# instalasi
pygame.init()
# mengatur ukuran layar/screen
height = 600
width = 600
screen = pygame.display.set_mode([height, width])
# Mengubah title screen
pygame.display.set_caption("Tutorial 3")
# memuat dan menggati logo/icon screen
icon = pygame.image.load("logo.png")
pygame.display.set_icon(icon)
# menambahkan background
background = pygame.image.load("background.jpg")
def plane(x,y):
image = pygame.image.load("plane.png")
screen.blit(image,(x,y))
x = 100
y = 300
x_point = 0
y_point = 0
# musuh
enemy_img = pygame.image.load("enemy.png")
class Enemy():
def __init__(self):
self.x = random.randint(599,600)
self.y = random.randint(0, 600)
self.pointx = -0.5
def move(self):
self.x += self.pointx
def draw(self):
screen.blit(enemy_img,(self.x, self.y))
enemy_list = []
for i in range(7):
new_enemy = Enemy()
enemy_list.append(new_enemy)
running = True
while running:
# menambhkan background
screen.blit(background, (0,0))
#loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
x_point -= 0.5
if event.key == pygame.K_RIGHT or event.key == ord('d'):
x_point += 0.5
if event.key == pygame.K_UP or event.key == ord('w'):
y_point -= 0.5
if event.key == pygame.K_DOWN or event.key == ord('s'):
y_point += 0.5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a'):
x_point = 0
if event.key == pygame.K_RIGHT or event.key == ord('d'):
x_point = 0
if event.key == pygame.K_UP or event.key == ord('w'):
y_point = 0
if event.key == pygame.K_DOWN or event.key == ord('s'):
y_point = 0
# Batasan
if x <= 0:
x = 0
elif x>= 525:
x= 525
if y <= 0:
y =0
elif y>= 550:
y = 550
x += x_point
y += y_point
# move and draw
for enemy in enemy_list:
enemy.move()
enemy.draw()
# menampilkan gambar ke dalam screen
plane(x,y)
# menampilkan musuh
pygame.display.update()
pygame.quit()