forked from rczyrnik/pygame_tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpygame_tutorial_mole.py
89 lines (70 loc) · 2.46 KB
/
pygame_tutorial_mole.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
# run as pythonw test_click.py
import pygame
import random
from pygame.locals import *
# Initialize Pygame
pygame.init()
pygame.display.set_caption("Whack The Mole!")
# Create The Screen
screen_width, screen_height = 400, 300
screen = pygame.display.set_mode((screen_width, screen_height))
#Create The Backgound
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((1,166,17))
# Create And Display The Mole
mole_size = 80
mole = pygame.image.load('img/mole.png')
mole = pygame.transform.scale(mole, (mole_size,mole_size))
mole_rect = mole.get_rect()
mole_rect.topleft = (screen_width-mole_size)/2, (screen_height-mole_size)/2
# Create and Display the Hammer
hammer_size = 80
hammer = pygame.image.load('img/hammer_0.png')
hammer = pygame.transform.scale(hammer, (hammer_size,hammer_size))
hammer_rect = hammer.get_rect()
hammer_rect.topleft = (0, 0)
# Add The Mole To The Screen#Display The Background
screen.blit(background, (0, 0))
screen.blit(mole, mole_rect)
screen.blit(hammer, hammer_rect)
# Display The Screen
pygame.display.flip()
# Main Loop
move_x = move_y = level = 1
going = True
while going:
mouse_pos = pygame.mouse.get_pos()
hammer_rect.center = mouse_pos
for event in pygame.event.get():
if event.type == QUIT:
going = False
elif event.type == pygame.MOUSEBUTTONDOWN:
hammer = pygame.image.load('img/hammer_1.png')
hammer = pygame.transform.scale(hammer, (hammer_size,hammer_size))
mouse_position = pygame.mouse.get_pos()
if mole_rect.collidepoint(mouse_position):
move_x *= 1.3
move_y *= 1.3
level += 1
else:
print(level)
going = False
elif event.type == pygame.MOUSEBUTTONUP:
hammer = pygame.image.load('img/hammer_0.png')
hammer = pygame.transform.scale(hammer, (hammer_size,hammer_size))
# Move the Mole
mole_rect = mole_rect.move((move_x, move_y))
if mole_rect.left < 0 or mole_rect.right > screen_width:
move_x = -move_x
mole_rect = mole_rect.move((move_x, move_y))
if mole_rect.top < 0 or mole_rect.bottom > screen_height:
move_y = -move_y
mole_rect = mole_rect.move((move_x, move_y))
#Draw Everything
screen.blit(background, (0, 0))
screen.blit(mole, mole_rect)
screen.blit(hammer, hammer_rect)
pygame.display.flip()
# Quit The Game
pygame.quit()