copy

1
2
3
4
5
6
7
8
9
10
11
# config.py 存放配置
import os,sys
resource_path = os.path.join(os.path.dirname(__file__),'..','data')
fist_image = os.path.join(resource_path,'fist.bmp')
whimp_image = os.path.join(resource_path,'nut.bmp')

punch_sound = os.path.join(resource_path,'punch.wav')
whiff_sound = os.path.join(resource_path,'whiff.wav')

whimp_speed = 8
whimp_rotate = 12
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
# fist.py 
import os,sys
import pygame
import config
from config import fist_image
from pygame.locals import *
from tool import load_image


class Fist(pygame.sprite.Sprite):
'''拳头类,可出击'''
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image , self.rect = load_image(fist_image,-1)
self.punching = False

def update(self):
''' 重写父类的 update '''
mouse_pos = pygame.mouse.get_pos()
# self.rect.midtop = mouse_pos
if self.punching:
self.rect.midtop = ( mouse_pos[0] + 5 , mouse_pos[1] + 5)
else:
self.rect.midtop = mouse_pos

def punch(self,target):
''' 出击函数,检测是否与 target 产生碰撞'''
self.punching = True
if(self.rect.colliderect(target.rect)):
return True
else:
return False

def unpunch(self):
'''恢复'''
self.punching = False
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
# whimp.py
import os,sys
import pygame
from config import whimp_image,whimp_rotate,whimp_speed
from pygame.locals import *
from tool import load_image


class Whimp(pygame.sprite.Sprite):
'''猴子类,不要问为什么图片是个坚果,就是可爱'''
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image ,self.rect = load_image(whimp_image,-1)
self.origin = self.image
# 设置初始位置
self.rect.midtop = (self.rect.width / 2 , 0)
screen = pygame.display.get_surface()
self.right_limit = screen.get_rect().right - self.rect.width / 2
self.left_limit = self.rect.width / 2
self.angle = 0
self.speed = whimp_speed

def update(self):
''' 重写父类的 update '''
if self.angle:
self._spin()
else:
self._walk()

def _walk(self):
''' 猴子跑步 '''
# if not self.angle:
target_x = self.rect.centerx + self.speed
if target_x > self.right_limit or target_x < self.left_limit:
self.speed = -self.speed
self.image = pygame.transform.flip(self.image,-1,0)
else:
self.rect.centerx = target_x

def _spin(self):
''' 猴子眩晕 '''
center = self.rect.center
self.angle += whimp_rotate
if self.angle >= 360:
self.angle = 0
self.image = self.origin
self.image = pygame.transform.rotate(self.origin,self.angle)
self.rect = self.image.get_rect(center = center)

def punched(self):
''' 被锤了,QAQ '''
self.angle = 1
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
# tool.py 
import os,sys
import pygame
from pygame.locals import *
from pygame.compat import geterror

def load_image(name, colorkey=None):
fullname = name
try:
image = pygame.image.load(fullname)
except pygame.error as message:
print("Cannot load image:", fullname)
raise SystemExit(message)
image = image.convert()
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, RLEACCEL)
return image, image.get_rect()

def load_sound(name):
class NoneSound:
def play(self):
pass

if not pygame.mixer or not pygame.mixer.get_init():
return NoneSound()
fullname = name
try:
sound = pygame.mixer.Sound(fullname)
except pygame.error:
print("Cannot load sound: %s" % fullname)
raise SystemExit(str(geterror()))
return sound
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
# main.py
import os,sys
import time
import pygame
from whimp import Whimp
from fist import Fist
from pygame.locals import *
from tool import load_sound
from config import whiff_sound,punch_sound

def main():

if not pygame.font:
print('sorry, this game no font')
if not pygame.mixer.Sound:
print('sorry,this game no sound')

# 初始化背景,文字等
pygame.init()
screen = pygame.display.set_mode((400,80))
bg = pygame.Surface(screen.get_size())
bg = bg.convert()
bg.fill((250,250,250))
pygame.mouse.set_visible(0)
# screen.blit(bg,(0,0)) 要从前往后画

if pygame.font:
text = pygame.font.Font(None,32).render('Hit The Nut And Win $$$',1,(10,10,10))
bg.blit(text,(screen.get_width() / 2 - text.get_width() /2,30))
screen.blit(bg,(0,0))
pygame.display.flip()
print(text.get_rect())


# 准备资源,精灵,音效等
clock = pygame.time.Clock()
whimp = Whimp()
fist = Fist()
punch_s = load_sound(punch_sound)
whiff_s = load_sound(whiff_sound)

allsprite = pygame.sprite.RenderPlain((whimp,fist))

# 主循环
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT or event.type == pygame.K_ESCAPE:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if fist.punch(whimp):
whimp.punched()
punch_s.play()
else:
whiff_s.play()
if event.type == pygame.MOUSEBUTTONUP:
fist.unpunch()

allsprite.update()

screen.blit(bg,(0,0))
allsprite.draw(screen)
pygame.display.flip()



if __name__ == "__main__":
main()

official

chimp

show

thinking

1、分清楚每一层需要干什么,比如一开始企图在 chimp 的 update 里写 event。。。但实际上应该是先获得当前帧的所有 event,然后再更新数据,最后在 update(更新渲染)
2、如果一时间不能接受那么多函数的话,可以用自己的方式代替,比如 move_ip 可以写成 x=xx, y=yy

remaining

pygame.transform.rotate() 每次变化都会有锯齿,然后如果一直用变换后的去变换,不到 10 次就会内存溢出,所以代码中一直是使用的原始图像变换