-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrowusingpygame.py
126 lines (112 loc) · 3.67 KB
/
arrowusingpygame.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
def arrow():
import pygame,sys
import random,math
from pygame import mixer
#Initialising pygame and setting up window
pygame.init()
screen=pygame.display.set_mode((800,600))
pygame.display.set_caption("Arrow!")
icon=pygame.image.load("assets/target.png")
pygame.display.set_icon(icon)
background=pygame.image.load("assets/arrowbackground.png")
bowimage=pygame.image.load('assets/bow128.png')
arrowimage=pygame.image.load('assets/arrow128.png')
clock=pygame.time.Clock()
appleimage=pygame.image.load('assets/apple.png')
mixer.music.load("assets/arrowbgm.wav")
mixer.music.play(-1)
arrowsound=mixer.Sound("assets/arrowshoot.mp3")
def arrow(x,y):
screen.blit(arrowimage,(x,y))
def bow(x,y):
screen.blit(bowimage,(x-20,y))
def apple(x,y):
screen.blit(appleimage,(x,y))
bowY=250
bowX=50
arrowY=250
arrowX=50
bowYchange=3
arrowYchange=3
arrowXchange=0
appleX=765
appleY=random.randint(100,400)
appleYspeed=0
score=0
textfont=pygame.font.Font("assets/alagard.ttf",48)
life=3
userinfo=''
gameoverimg=pygame.image.load('assets/gameover.png')
def show_score():
score_text=textfont.render("Score : " + str(score),True,(255,255,255))
screen.blit(score_text,(300,20))
score_text=textfont.render("Lives : " + str(life),True,(255,255,255))
screen.blit(score_text,(570,20))
#Game Loop
running=True
while running:
#screen.fill((255,255,255))
screen.blit(background,(0,0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type== pygame.KEYDOWN:
if event.key==pygame.K_SPACE:
arrowsound.play()
arrowXchange=10
arrowYchange=0
if event.key==pygame.K_ESCAPE:
running=False
mixer.music.load("assets/arcadebackground.wav")
mixer.music.play(-1)
if event.key==pygame.K_RETURN:
if life==0:
bowY=250
bowX=50
arrowY=250
arrowX=50
bowYchange=3
arrowYchange=3
arrowXchange=0
appleX=765
print(userinfo)
life=3
score=0
if life == 0:
userinfo+=event.unicode
if event.key==pygame.K_BACKSPACE:
userinfo=userinfo[0:-2]
arrowY+=arrowYchange
arrowX+=arrowXchange
bowY+=bowYchange
arrow(arrowX,arrowY)
bow(bowX,bowY)
apple(appleX,appleY)
#collision
distance=math.sqrt((appleX-arrowX)**2 + (appleY-(arrowY+50))**2)
if distance<=88:
arrowX=bowX
arrowY=bowY
arrowXchange=0
arrowYchange=bowYchange
appleY=random.randint(100,400)
score+=1
if bowY<=0 or bowY>=472:
bowYchange*=-1
if arrowY<=0 or arrowY>=472:
arrowYchange*=-1
if arrowX>=672:
arrowX=bowX
arrowY=bowY
arrowXchange=0
arrowYchange=bowYchange
life-=1
show_score()
if life == 0:
screen.blit(gameoverimg,(0,0))
arrowYchange=0
bowYchange=0
arrowXchange=0
clock.tick(60)
pygame.display.update()