脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - 基于Python-Pycharm实现的猴子摘桃小游戏(源代码)

基于Python-Pycharm实现的猴子摘桃小游戏(源代码)

2021-09-07 00:15一个超会写Bug的安太狼 Python

这篇文章主要介绍了基于Python-Pycharm实现的猴子摘桃小游戏,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

源码及注释:

?
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import pygame
from sys import exit
from random import randint
import time
import os
 
# 定义窗口分辨率
screen_width = 700
screen_height = 600
 
current_path = os.path.abspath(os.path.dirname(__file__))
root_path = current_path[:current_path.find("monkey-picking-peach\\") + len("monkey-picking-peach\\")] \
   + "resource\\images\\"
# 图片
background_image_path = root_path + "background.jpg"
monkey_image_path = root_path + "monkey.png"
apple_image_path = root_path + "apple.png"
jump_status = false
over_flag = false
start_time = none
offset = {pygame.k_left: 0, pygame.k_right: 0, pygame.k_up: 0, pygame.k_down: 0}
 
# 定义画面帧率
frame_rate = 60
 
# 定义动画周期(帧数)
animate_cycle = 30
 
ticks = 0
clock = pygame.time.clock()
 
 
# 猴子类
class monkey(pygame.sprite.sprite):
 # 苹果的数量
 apple_num = 0
 
 def __init__(self, mon_surface, monkey_pos):
  pygame.sprite.sprite.__init__(self)
  self.image = mon_surface
  self.rect = self.image.get_rect()
  self.rect.topleft = monkey_pos
  self.speed = 5
 
 # 控制猴子的移动
 def move(self, _offset):
  global jump_status
  x = self.rect.left + _offset[pygame.k_right] - _offset[pygame.k_left]
  y = self.rect.top + _offset[pygame.k_down] - _offset[pygame.k_up]
  if y < 0:
   self.rect.top = 0
   jump_status = true
  elif y >= screen_height - self.rect.height:
   self.rect.top = screen_height - self.rect.height
   jump_status = false
  else:
   self.rect.top = y
   jump_status = true
 
  if x < 0:
   self.rect.left = 0
  elif x > screen_width - self.rect.width:
   self.rect.left = screen_width - self.rect.width
  else:
   self.rect.left = x
 
 # 接苹果
 def picking_apple(self, app_group):
 
  # 判断接到几个苹果
  picked_apples = pygame.sprite.spritecollide(self, app_group, true)
 
  # 添加分数
  self.apple_num += len(picked_apples)
 
  # 接到的苹果消失
  for picked_apple in picked_apples:
   picked_apple.kill()
 
 
# 苹果类
class apple(pygame.sprite.sprite):
 def __init__(self, app_surface, apple_pos):
  pygame.sprite.sprite.__init__(self)
  self.image = app_surface
  self.rect = self.image.get_rect()
  self.rect.topleft = apple_pos
  self.speed = 1
 
 def update(self):
  global start_time
  if start_time is none:
   start_time = time.time()
  self.rect.top += (self.speed * (1 + (time.time() - start_time) / 40))
  if self.rect.top > screen_height:
   # 苹果落地游戏结束
   global over_flag
   over_flag = true
   self.kill()
 
 
# 初始化游戏
pygame.init()
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("猴子接苹果")
 
# 载入图片
background_surface = pygame.image.load(background_image_path).convert()
monkey_surface = pygame.image.load(monkey_image_path).convert_alpha()
apple_surface = pygame.image.load(apple_image_path).convert_alpha()
 
# 创建猴子
monkey = monkey(monkey_surface, (200, 500))
 
# 创建苹果组
apple_group = pygame.sprite.group()
 
# 分数字体
score_font = pygame.font.sysfont("arial", 40)
 
# 主循环
while true:
 
 if over_flag:
  break
 
 # 控制游戏最大帧率
 clock.tick(frame_rate)
 
 # 绘制背景
 screen.blit(background_surface, (0, 0))
 
 if ticks >= animate_cycle:
  ticks = 0
 
 # 产生苹果
 if ticks % 30 == 0:
  apple = apple(apple_surface,
      [randint(0, screen_width - apple_surface.get_width()), -apple_surface.get_height()])
  apple_group.add(apple)
 
 # 控制苹果
 apple_group.update()
 
 # 绘制苹果组
 apple_group.draw(screen)
 
 # 绘制猴子
 screen.blit(monkey_surface, monkey.rect)
 ticks += 1
 
 # 接苹果
 monkey.picking_apple(apple_group)
 
 # 更新分数
 score_surface = score_font.render(str(monkey.apple_num), true, (0, 0, 255))
 screen.blit(score_surface, (620, 10))
 # 更新屏幕
 pygame.display.update()
 
 for event in pygame.event.get():
  if event.type == pygame.quit:
   pygame.quit()
   exit()
 
  # 控制方向
  if event.type == pygame.keydown:
   if event.key in offset:
    if event.key == pygame.k_up:
     offset[event.key] = 80
    else:
     offset[event.key] = monkey.speed
  elif event.type == pygame.keyup:
   if event.key in offset:
    offset[event.key] = 0
 
 # 移动猴子
 if jump_status:
  offset[pygame.k_down] = 5
  offset[pygame.k_up] = 0
 monkey.move(offset)
 
# 游戏结束推出界面
score_surface = score_font.render(str(monkey.apple_num), true, (0, 0, 255))
over_surface = score_font.render(u"game over!", true, (0, 0, 255))
screen.blit(background_surface, (0, 0))
screen.blit(score_surface, (620, 10))
screen.blit(over_surface, (250, 270))
 
while true:
 
 pygame.display.update()
 for event in pygame.event.get():
  if event.type == pygame.quit:
   pygame.quit()
   exit()

食用指南: 使用的图片

monkey.png:

基于Python-Pycharm实现的猴子摘桃小游戏(源代码)

background.jpg:

基于Python-Pycharm实现的猴子摘桃小游戏(源代码)

apple.png:

基于Python-Pycharm实现的猴子摘桃小游戏(源代码)

这是我的文件目录,学习者也可改为自己的:

基于Python-Pycharm实现的猴子摘桃小游戏(源代码)

更改的代码位置:

?
1
2
root_path = current_path[:current_path.find("monkey-picking-peach\\") + len("monkey-picking-peach\\")] \
   + "resource\\images\\"

游戏截图:

基于Python-Pycharm实现的猴子摘桃小游戏(源代码)

到此这篇关于基于python-pycharm实现的猴子摘桃小游戏的文章就介绍到这了,更多相关python 猴子摘桃小游戏内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/m0_46278037/article/details/113872428

延伸 · 阅读

精彩推荐