我正在用 pygame 制作一个简单的游戏,我想移动一张图片,但该图片似乎每次点击都会制作,这根本不是我想要的,我想通过按键来移动它,但每次时间移动了一个
代码:
# import modules
import pygame
from pygame.locals import *
import sys
import os
#################
pygame.init()
#################
#Colors
red = (255 , 0 , 0) # RED
green = (0, 255, 0) # GREEN
blue = (10, 60, 225) # BLUE
white = (255, 255, 255) # WHITE
black = (0, 0, 0) # BLACK
# window
window = pygame.display.set_mode((600, 400)) # window size
pygame.display.set_caption("Ball away") # title
window.fill(white) # backgrond color
img = pygame.image.load("bin/images/icon.png") # window icon
pygame.display.set_icon(img) # load icon
# screen
#lines
lineup = pygame.draw.line(window, black, (0,35), (100000,0),4)
linedown = pygame.draw.line(window, black, (0,350), (1000000,4),4)
#TEXTS
ping_text = "Ping : 50"
font = pygame.font.SysFont(None, 25)
t_p = font.render(ping_text, True, (0, 0, 0))
window.blit(t_p, (515, 10))
count_win = "0 - 0"
font = pygame.font.SysFont(None, 30)
c_w = font.render(count_win, True, (0, 0, 0))
window.blit(c_w, (284, 10))
exit_ico = pygame.image.load("bin/images/exit.png")
exit_i = pygame.transform.scale(exit_ico,(30, 30))
window.blit(exit_i , (3, 3))
# players
# RED
P1_x = 3
P1_y = 140
speed = 5
Player_RED = pygame.image.load("bin/images/Player1_RED.png")
player1_red = pygame.transform.scale(Player_RED,(80, 85))
# BLUE
P2_x = 520
P2_y = 145
speed = 5
Player_BLUE = pygame.image.load("bin/images/Player2_BLUE.png")
player2_blue = pygame.transform.scale(Player_BLUE,(80, 85))
# ball
Ball_lets = pygame.transform.scale(img,(35,35))
window.blit(Ball_lets, (300, 170))
# soundObj = pygame.mixer.Sound('bin/sounds/Music.mp3')
# soundObj.play()
# main loop
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
os.system('cls')
running = False
if event.type == KEYUP:
if event.key == K_w:
P1_y -= speed
if event.key == K_s:
P1_y += speed
window.blit(player1_red, (P1_x, P1_y))
if event.type == KEYUP:
if event.key == K_UP:
P2_y -= speed
if event.key == K_DOWN:
P2_y += speed
window.blit(player2_blue, (P2_x, P2_y))
pygame.display.update()
pygame.quit()
sys.exit()
缩进不是我的问题,我是 Stack Overflow 的新手
问题在于你是在 每次 按键事件发生时都在绘制图像,而不是在特定位置更新图像的位置。这样一来,看起来图像就在不断地被“绘制”出来,而不是移动。
以下是如何修改代码以正确移动图像的方法:
# import modules
import pygame
from pygame.locals import *
import sys
import os
#################
pygame.init()
#################
# ... [其余的代码与之前相同] ...
# main loop
running = True
while running:
# 处理事件
for event in pygame.event.get():
if event.type == QUIT:
os.system('cls')
running = False
if event.type == KEYDOWN: # 检查按键按下事件
if event.key == K_w:
P1_y -= speed
if event.key == K_s:
P1_y += speed
if event.key == K_UP:
P2_y -= speed
if event.key == K_DOWN:
P2_y += speed
# 更新游戏状态
# ... [这里可以添加任何游戏逻辑,例如球的移动、碰撞检测等] ...
# 绘制游戏画面
window.fill(white) # 清除屏幕
#lines
lineup = pygame.draw.line(window, black, (0,35), (100000,0),4)
linedown = pygame.draw.line(window, black, (0,350), (1000000,4),4)
#TEXTS
window.blit(t_p, (515, 10))
window.blit(c_w, (284, 10))
window.blit(exit_i , (3, 3))
# players
window.blit(player1_red, (P1_x, P1_y)) # 在新位置绘制图像
window.blit(player2_blue, (P2_x, P2_y)) # 在新位置绘制图像
# ball
window.blit(Ball_lets, (300, 170))
# 更新显示
pygame.display.update()
pygame.quit()
sys.exit()
主要的变化是:
-
事件处理
: 我们现在在
KEYDOWN
事件中检查按键按下,这意味着只有当按键被按下时才会移动图像。 -
游戏循环结构
: 我们将代码分为三个部分:
- 事件处理 : 处理用户的输入。
- 更新游戏状态 : 更新游戏对象的位置和其他属性。
- 绘制游戏画面 : 将更新后的游戏画面绘制到屏幕上。
-
清除屏幕
: 在每一帧开始时使用
window.fill(white)
清除屏幕,以防止图像出现“拖尾”现象。
通过这些修改,你的图像现在应该可以平滑地移动了。
标签:python,pygame From: 67230513