[英]How do I make player_1 move with keys?
I want to make a Pong game in Python. 我想用Python制作Pong游戏。 I already have the windows and the player models.
我已经有了窗户和播放器型号。
I tried to make it with 我试图做到
if keys[pygame.K_LEFT]:
x -= speed
if keys[pygame.K_LEFT]:
x += speed
this is my whole code: 这是我的整个代码:
import pygame
from pygame import *
import math
import random
clock = pygame.time.Clock() # fps
pygame.init() # start
title = pygame.display.set_caption("Pong")
width = 640
height = 480
screen = pygame.display.set_mode((width, height)) # screen
running = True
speed = 10
white = (255, 255, 255)
x = 120
y = 5
player_1 = pygame.draw.rect(screen, white, (240, 430, x, y))
player_2 = pygame.draw.rect(screen, white, (240, 40, x, y))
pong = pygame.draw.circle(screen, white, (300, 235), 4)
pygame.display.update()
fps = clock.tick(60)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= speed
if keys[pygame.K_LEFT]:
x += speed
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
running = False
I wanted to make the lower player to move with the arrow keys but nothing happens at all. 我想让下层玩家用箭头键移动,但什么也没发生。
Put 放
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
x -= speed
if keys[pygame.K_LEFT]:
x += speed
player_1 = pygame.draw.rect(screen, white, (240, 430, x, y))
player_2 = pygame.draw.rect(screen, white, (240, 40, x, y))
pong = pygame.draw.circle(screen, white, (300, 235), 4)
pygame.display.update()
fps = clock.tick(60)
within the while loop. 在while循环中。
Also, change 另外,改变
if keys[pygame.K_LEFT]:
x += speed
to 至
if keys[pygame.K_RIGHT]:
x += speed
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.