python - 为什么仙人掌在改变速度时比地面移动得更快?
问题描述
我正在尝试制作一款类似于 google chrome 中的 Trex 游戏。我做了一个 SPEEDEVENT,这样每当 1 秒过去时,速度就会增加 0.1。在改变速度之前,仙人掌和地面都以相同的速度移动,但是一旦速度改变,仙人掌开始移动得更快(地面的速度也增加了)。当我将速度设置为增加 0.000000001 时也会发生这种情况,所以这不是关于速度的价值,而是关于我改变了速度的事实。无论如何,这是代码,您可能最需要关注的部分是move_cactus
移动所有仙人掌并返回移动列表的函数,以及#Ground
游戏循环中的注释下的内容(这就是我移动地面的地方)
import pygame
from random import choice, randint
def manage_surface():
global current_surface
if leg == 'rear':
if crouching:
current_surface = 3
if not crouching:
current_surface = 1
if leg == 'front':
if crouching:
current_surface = 4
if not crouching:
current_surface = 2
def change_leg():
global leg
if leg == 'rear':
leg = 'front'
return
else: leg = 'rear'
def spawn_cactus():
cactus_count = randint(1, 3)
x_position = screen_width + 200
big_cactus = False
for count in range(cactus_count):
new_cactus_surface = choice(cactus_surfaces)
if big_cactus: new_cactus_surface = choice(cactus_surfaces[5:-1])
new_cactus_rect = new_cactus_surface.get_rect(midbottom = (x_position, ground_level))
new_cactus_rect.left = x_position
new_cactus_mask = pygame.mask.from_surface(new_cactus_surface)
cactus_list.append([new_cactus_surface, new_cactus_rect, new_cactus_mask])
x_position += new_cactus_rect.width
if new_cactus_surface in cactus_surfaces[0:5]: big_cactus = True
def draw_cactus():
for cactus in cactus_list:
screen.blit(cactus[0], cactus[1])
def move_cactus(cactuses):
moved_cactus_list = []
for cactus in cactuses:
if cactus[1].right < 0:
continue
cactus[1].centerx -= speed
moved_cactus_list.append(cactus)
return moved_cactus_list
def check_collision():
trex_x, trex_y = trex_rect.topleft
for cactus in cactus_list:
cactus_x, cactus_y = cactus[1].topleft
offset = (cactus_x - trex_x, cactus_y - trex_y)
if trex_mask.overlap(cactus[2], offset):
return True
pygame.init()
fps = 60
clock = pygame.time.Clock()
screen_width = 1280
screen_height = 720
screen = pygame.display.set_mode([screen_width, screen_height])
#Game Variables
initial_speed = 7
speed = initial_speed
initial_gravity = 1
gravity = initial_gravity
game_active = False
SPEEDEVENT = pygame.USEREVENT + 2
pygame.time.set_timer(SPEEDEVENT, 1000)
#Ground
ground_surface = pygame.image.load('assets/ground.png').convert_alpha()
ground_width, ground_height = ground_surface.get_size()
ground_last_x_pos = 0
crouch = False
#Trex
trex_surfaces = []
trex_images = ['trex0.png', 'trex1.png', 'trex2.png', 'trex1-1.png', 'trex2-1.png', 'trex3.png']
for trex in trex_images:
trex_surfaces.append(pygame.image.load(f'assets/{trex}').convert_alpha())
ground_level = 710
leg = 'rear'
jumping = False
jump_power = 22
crouching = False
manage_surface()
trex_rect = trex_surfaces[current_surface].get_rect(center = (150, ground_level))
trex_mask = pygame.mask.from_surface(trex_surfaces[current_surface])
TREXSTEP = pygame.USEREVENT
pygame.time.set_timer(TREXSTEP, 200)
#cactus
cactus_surfaces = []
cactus_images = ['bigcactus1.png', 'bigcactus2.png', 'bigcactus3.png', 'bigcactus4.png', 'bigcactus5.png', 'cactus1.png', 'cactus2.png', 'cactus3.png', 'cactus4.png', 'cactus5.png', 'cactus6.png']
for cactus in cactus_images:
cactus_surfaces.append(pygame.image.load(f'assets/{cactus}').convert_alpha())
cactus_list = []
SPAWNPIKE = pygame.USEREVENT + 1
pygame.time.set_timer(SPAWNPIKE, 2000)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP and not crouching:
if not game_active: game_active = True
jumping = True
if event.key == pygame.K_DOWN and not jumping:
trex_rect.bottom += 17
crouching = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
crouching = False
if event.type == TREXSTEP and not jumping:
change_leg()
if event.type == SPAWNPIKE:
spawn_cactus()
if event.type == SPEEDEVENT and game_active and cactus_list:
speed += 0.1
if not game_active: continue
#Background Color
screen.fill((255, 255, 255))
#Ground
screen.blit(ground_surface, (ground_last_x_pos, 676))
screen.blit(ground_surface, (ground_last_x_pos+ground_width, 676))
if ground_last_x_pos < -ground_width:
ground_last_x_pos = 0
ground_last_x_pos -= speed
#Cactus
draw_cactus()
cactus_list = move_cactus(cactus_list)
#Trex
manage_surface()
trex_rect = trex_surfaces[current_surface].get_rect(center = trex_rect.center)
trex_mask = pygame.mask.from_surface(trex_surfaces[current_surface])
if jumping: trex_rect.bottom -= jump_power
trex_rect.bottom += gravity
gravity += initial_gravity
if trex_rect.bottom > ground_level:
trex_rect.bottom = ground_level
gravity = initial_gravity
jumping = False
if check_collision():
game_active = False
current_surface = 5
speed = initial_speed
screen.blit(trex_surfaces[current_surface], trex_rect)
pygame.display.update()
clock.tick(fps)
pygame.quit()
解决方案
推荐阅读
- javascript - 使用 javascript 生成要下载的文件。如何使用没有 HTTP 标头的“另存为”对话框?
- sql - 将存储过程运行到与数据库不同的兼容性级别
- android - 如何在 Android 库项目中按测试类型分隔源文件?
- kubernetes-ingress - Nginx入口控制器速率限制不起作用
- python-2.7 - sqlite3 & python:获取主键和外键列表
- javascript - React-leaflet 如何动态更改矩形颜色?
- linux - 从用户空间读取非阻塞 i2c
- wordpress - 安装 nginx 和 wordpress 子文件夹的问题——永久链接不起作用
- python-3.x - 通过networkx着色节点
- java - 在 Windows 7、8 和 10 上检索所有当前安装的软件