File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #coding:utf-8
2+ background_image_filename = '1.jpg'
3+ sprite_image_filename = '2.jpg'
4+
5+ import pygame
6+ from pygame .locals import *
7+ from sys import exit
8+
9+ pygame .init ()
10+ screen = pygame .display .set_mode ((1024 ,800 ),0 ,32 )
11+
12+ background = pygame .image .load (background_image_filename ).convert ()
13+ sprite = pygame .image .load (sprite_image_filename )
14+
15+ clock = pygame .time .Clock ()
16+
17+ x = 0.0
18+ speed = 250
19+
20+ while True :
21+ for event in pygame .event .get ():
22+ if event .type == QUIT :
23+ exit ()
24+ screen .blit (background ,(0 ,0 ))
25+ screen .blit (sprite ,(x ,100 ))
26+
27+ time_passed = clock .tick ()
28+ time_passed_seconds = time_passed / 1000.0
29+
30+ distance_moved = time_passed_seconds * speed
31+ x += distance_moved
32+
33+ if x > 1024 :
34+ x -= 1024
35+
36+ pygame .display .update ()
Original file line number Diff line number Diff line change 1+ #coding:utf-8
2+ background_image_filename = '1.jpg'
3+ sprite_image_filename = '2.jpg'
4+
5+ import pygame
6+ from pygame .locals import *
7+ from sys import exit
8+
9+ pygame .init ()
10+ screen = pygame .display .set_mode ((1024 ,800 ),0 ,32 )
11+
12+ background = pygame .image .load (background_image_filename ).convert ()
13+ sprite = pygame .image .load (sprite_image_filename )
14+
15+ clock = pygame .time .Clock ()
16+
17+ x ,y = 100. ,100.
18+ speed_x ,speed_y = 133. ,170.
19+
20+ while True :
21+ for event in pygame .event .get ():
22+ if event .type == QUIT :
23+ exit ()
24+ screen .blit (background ,(0 ,0 ))
25+ screen .blit (sprite ,(x ,y ))
26+
27+ time_passed = clock .tick (30 )
28+ time_passed_seconds = time_passed / 1000.0
29+
30+ x += speed_x * time_passed_seconds
31+ y += speed_y * time_passed_seconds
32+
33+ if x > 1024 - sprite .get_width ():
34+ speed_x = - speed_x
35+ x = 1024 - sprite .get_width ()
36+ elif x < 0 :
37+ speed_x = - speed_x
38+ x = 0.
39+
40+ if y > 800 - sprite .get_height ():
41+ speed_y = - speed_y
42+ x = 800 - sprite .get_height ()
43+ elif y < 0 :
44+ speed_y = - speed_y
45+ y = 0.
46+
47+ pygame .display .update ()
You can’t perform that action at this time.
0 commit comments