python 物理公式計算 計算重力 跳躍

2021-10-13 05:07:27 字數 2357 閱讀 4660

在pygame中計算重力的最佳方法是什麼?我基本上只需要它,所以當玩家按下「向上」時,角色就會跳起來。這是我目前為止的**(只有乙個白色螢幕和乙個移動的紅色塊)import pygame

import random

# define colors

black = ( 0, 0, 0)

white = ( 255, 255, 255)

green = ( 0, 255, 0)

red = ( 255, 0, 0)

#classes

class player(pygame.sprite.sprite):

def __init__(self, color, width, height):

pygame.sprite.sprite.__init__(self)

self.image = pygame.su***ce([width, height])

self.image.fill(color)

self.rect = self.image.get_rect()

def move(self, x_change, y_change):

self.rect.x += x_change

self.rect.y += y_change

#lists

all_sprites_list = pygame.sprite.group()

#spawn player

player = player(red,16,16)

all_sprites_list.add(player)

player.rect.x = 0

player.rect.y = 484

#initalize

pygame.init()

#set the width and height of the screen [width,height]

screen_height = 700

screen_width = 500

size=[screen_height,screen_width]

screen=pygame.display.set_mode(size)

#name on top tab

pygame.display.set_caption("my game")

#dont change

done = false

clock=pygame.time.clock()

#main loop

while done == false:

for event in pygame.event.get(): # user did something

if event.type == pygame.quit: # if user clicked close

done = true # quit

if event.type == pygame.keyup:

# if it is an arrow key, reset vector back to zero

if event.key == pygame.k_left:

none

keydown = pygame.key.get_pressed()

if keydown[pygame.k_right]:

player.move(3, 0)

if keydown[pygame.k_left]:

player.move(-3, 0)

if keydown[pygame.k_down]:

player.move(0, 3)

if keydown[pygame.k_up]:

player.move(0,-3)

#if player hits side of screen, do this

if player.rect.x < 0:

player.rect.x = 0

if player.rect.x > 684:

player.rect.x = 684

if player.rect.y < 0:

player.rect.y = 0

if player.rect.y > 484:

player.rect.y = 484

#clear screen

screen.fill(white)

#drawing

all_sprites_list.draw(screen)

#fps lock

clock.tick(60)

#update screen

pygame.display.flip()

# close the window and quit.

pygame.quit()

Python 計算思維訓練 公式計算

庫函式的使用 高斯函式的計算 from math import pi,sqrt,exp deftest list for m,s,x inlist begin a 1 s sqrt 2 pi b 1 2 x m s 2 c exp b fx a c end print format fx 0 引數序...

日期計算 計數

時間限制 1.0s 記憶體限制 256.0mb 問題描述 問題描述 給定乙個年份y和乙個整數d,問這一年的第d天是幾月幾日?注意閏年的2月有29天。滿足下面條件之一的是閏年 1 年份是4的整數倍,而且不是100的整數倍 2 年份是400的整數倍。輸入格式 輸入的第一行包含乙個整數y,表示年份,年份在...

計算計算機位元組順序

problem write a function that determines whether a computer is big endian or little endian solution include using namespace std int is little endian b...